Monthly Archive

Categories

PowerShell

PowerShell in Action–Deal of the Day-23 July 2016

Deal of the Day July 23: Half off my book Windows PowerShell in Action, Third Edition. Use code dotd072316au at http://bit.ly/2afnOPj

 

Deal of the Day details at https://www.manning.com/dotd

Biggest innovation in PowerShell is…

The imminent arrival of PowerShell 5.1 in the Windows 10 Anniversary update (assumption as the Windows 10 Insider previews have been showing PowerShell 5.1 for some time) and Windows Server 2016 (TP5 shows PowerShell 5.1) and the fact that PowerShell was officially released to the world 10 years ago come November started me thinking about what has been the biggest innovation in PowerShell over those 10 years.

 

Contenders that come to mind include:

- Remoting
- PowerShell modules
- CIM & CIM sessions
- Workflows
- Job engine
- Desired State Configuration
- PowerShell Gallery and package management
- PowerShell classes
- JSON support in PowerShell
- Internationalisation
- Error handling - try/catch
- Debugging enhancements
- REST API and Odata support
- PowerShell support for Microsoft products
- PowerShell support from third part vendors
- PowerShell community

 

Which do you think?

 

Or is it something else?

 

Leave a comment as I’m intrigued as to what people think has been the biggest PowerShell innovation

Some potentially useful modules

I’ve come across a number of modules over the last few months that may be of interest. In no particular order:

PSDEPLOY - https://github.com/RamblingCookieMonster/PSDeploy

A module to simplify PowerShell based deployments

 

PLASTER - https://github.com/PowerShell/Plaster

A template based artifact generator – streamlines creation of PowerShell module projects, pester tests, DSC configurations etc

 

OPERATION VALIDATION FRAMEWORK -https://github.com/PowerShell/Operation-Validation-Framework

Tools for executing validation of the operation of a system. Organises and executes Pester tests to validate operation

 

LABILITY - https://www.powershellgallery.com/packages/Lability/0.9.8

Provisioning Hyper-V test lab and development environments

 

PSAKE  - https://github.com/psake/psake

Build automation

 

POSHSPEC - https://github.com/Ticketmaster/poshspec

Infrastructure testing DSL. Expanding Pester to test infrastructure

 

Some of these projects are just starting while others are more mature.

 

They may not do exactly what you want but they may give you some ideas. All of these are well worth a look

First nail in PowerShell’s coffin?

The MSDN pages describing the installation and use of containers

https://msdn.microsoft.com/en-us/virtualization/windowscontainers/quick_start/quick_start_images

 

seems to have removed all of the PowerShell code and just supply examples in Docker.

 

After 10 years is this the first sign that PowerShell is going away?

Cleaning up entries on Remote Desktop Connection

I use RDP between my main laptop and the machine on which my lab resides. When I rebuild the lab the entries in the Remote Desktop Connection for the old machine still remain.

 

There isn’t a direct way to remove those entries. But you can remove them from the registry.

 

First view the entries

Get-ChildItem -Path 'HKCU:\SOFTWARE\Microsoft\Terminal Server Client\'

    Hive: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Terminal Server Client

Name                           Property
----                           --------
Default                        MRU0 : RSLaptop01
                               MRU1 : serverrs01
                               MRU2 : server02

 

The partial list above shows you that the property names are MRU* 

 

You can view individual entries

Get-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Terminal Server Client\Default\' -Name MRU2

MRU2         : server02
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Terminal
               Server Client\Default\
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Terminal
               Server Client
PSChildName  : Default
PSDrive      : HKCU
PSProvider   : Microsoft.PowerShell.Core\Registry

 

You can now delete as required – for instance

 

Remove-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Terminal Server Client\Default\' -Name MRU2

Still asking for topics

Earlier in the month I posted a request for topics on powershell.org

https://powershell.org/request-for-topics/

 

This is a request for YOU to tell us what topics you would like to see at the next PowerShell & DevOps Summit

 

We’ve had a handful of replies so far.

 

This is your chance to influence the content of the Summit. These topics will be included as suggestions in the Call for Proposals where we ask potential speakers to send us their session proposals. That will be going out next month.

 

if there is something in the PowerShell/DevOps world you want to know more about let us know.

PowerShell Summit & Conference videos 2016

 

If you didn’t get to any of these conferences this year the videos are now available.

 

PowerShell and DevOps Summit

https://www.youtube.com/playlist?list=PLfeA8kIs7Coc1Jn5hC4e_XgbFUaS5jY2i

 

European PowerShell Conference

https://www.youtube.com/channel/UCxgrI58XiKnDDByjhRJs5fg

 

You might find this interesting as well.

WinOps conference

https://www.youtube.com/channel/UCP1OgsLk-HkEdQyhjJX_5JQ

Parallel processing

One of the great features PowerShell brings is the ability to remotely administer your servers. Most people begin remote administration by processing the servers sequentially. Eventually, this process breaks down because you have too many servers and/or the processes you are running against each server are long running.

 

At this point you have to consider parallel processing.

 

The UK TechNet blog has recently published my article on the options for parallel processing using various PowerShell techniques

https://blogs.technet.microsoft.com/uktechnet/2016/06/20/parallel-processing-with-powershell/

Converting strings to dates

You’ll see many examples of this:

PS>  [datetime]'12/25/2016'

25 December 2016 00:00:00

 

This works great if the date is in US format – MM/DD/YYYY

 

For those of us who use different date formats – such as England DD/MM/YYYY – this approach won’t work

PS>  [datetime]'25/12/2016'
Cannot convert value "25/12/2016" to type "System.DateTime". Error: "String was not recognized as a
valid DateTime."
At line:1 char:1
+ [datetime]'25/12/2016'
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider

 

You need to rearrange the string  -  for example

$ds = '25/12/2016' -split '/'

PS>  [datetime]("{0}/{1}/{2}" -f $ds[1], $ds[0], $ds[2])

25 December 2016 00:00:00

 

PS>  Get-Date -Day $ds[0] -Month $ds[1] -Year $ds[2]

25 December 2016 10:44:45

 

or even simpler

PS>  Get-Date -Date '25/12/2016'

25 December 2016 00:00:00

 

Not sure when the last option came in. Its in PowerShell v5

Local Administrators

Finding the local administrators on a system is a not infrequent action.  There are a number of ways to do this.

 

The oldest method is to use the ADSI WinNT provider

$group =[ADSI]"WinNT://$($env:COMPUTERNAME)/Administrators, group"
$members = @($group.psbase.Invoke("Members"))
$members | Foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}

 

NOTE – this doesn’t work on my Windows 10 system – build 14352

I’d recommend avoiding the WinNT provider if you can

 

WMI provides this option

$group = Get-CimInstance -ClassName Win32_Group -Filter "Name='Administrators'"
Get-CimAssociatedInstance -InputObject $group -ResultClassName Win32_UserAccount

 

You can also use a .NET based approach with the System.DirectoryServices.AccountManagement  namespace

using assembly System.DirectoryServices.AccountManagement
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, $($env:COMPUTERNAME)

$idtype = [System.DirectoryServices.AccountManagement.IdentityType]::Name
$grp = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, $idtype, "Administrators")
$grp.Members | select SamAccountName

 

This is a bit more complicated as you have to load the assembly (using is new to PowerShell v5 – use Add-Type in earlier versions)

Set the context to the local machine and the identity type to Name

You can then use FindByIdentity() to get the local adminsitrators groups and look at the Members property to find the group members.

 

PowerShell v5 brings a Local Accounts module - Microsoft.PowerShell.LocalAccounts

Add-LocalGroupMember
Disable-LocalUser
Enable-LocalUser
Get-LocalGroup
Get-LocalGroupMember
Get-LocalUser
New-LocalGroup
New-LocalUser
Remove-LocalGroup
Remove-LocalGroupMember
Remove-LocalUser
Rename-LocalGroup
Rename-LocalUser
Set-LocalGroup
Set-LocalUser

 

NOTE – depending on your version of PowerShell v5 you may, or may not have this module. Its present in the later Windows 10 builds (on Insider Preview) and in Windows server 2016 TP 5. Eventually it’ll become available on all Windows 10 systems through Windows updates.

 

PS>  Get-LocalGroupMember -Group Administrators | select Name

Name
----
RSsurfacePro2\Administrator
RSSURFACEPRO2\Richard