PowerShell for Windows Admins


May 3, 2017  10:07 AM

OpenSSH

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

PowerShell v6 enables remoting over SSH between Linux and Windows machines. SSH connectivity is established using OpenSSH. You can use remoting over SSH in any of these scenarios:

Windows – Linux
Linux – Windows
Windows – Windows
Linux – Linux

You can also establish traditional WinRM remoting sessions and send commands to a mixture of WinRM and SSH based sessions.

A couple of new initiatives around OpenSSH need widespread participation.

First off – OpenSSH security testing

The PowerShell Team is getting OpenSSH production ready and as part of that are arranging for security testing. Details of how you can be involved are available from:

OpenSSH Security Testing Kick Off

Secondly – installing OpenSSH involves a significant number of manual steps. An OpenSSH Universal Installer is available for testing

https://cloudywindows.com/post/unveiling-the-openssh-universal-automated-installer/

May 1, 2017  3:33 PM

Positional parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Positional parameters have been around since the beginning of PowerShell. Positional parameters assume the parameter you mean from the position of the value in the command you supply. For instance you can do this:

Get-ChildItem -Path C:\test\ -Filter *.xml

Path and filter are the 2 positional parameters for Get-ChildItem (cmdlets can have 0, 1 or many positional parameters) so you can also do this:

Get-ChildItem C:\test\  *.xml

If you reverse the values you get an error

PS> Get-ChildItem *.xml  C:\test\
Get-ChildItem : Second path fragment must not be a drive or UNC name.
Parameter name: path2
At line:1 char:1
+ Get-ChildItem *.xml  C:\test\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\Scripts:String) [Get-ChildItem], ArgumentException
    + FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand

You can find the position parameters by looking in the help file

PS> Get-Help Get-ChildItem -Parameter path

-Path <String[]>
    Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory
    (.).

    Required?                    false
    Position?                    0
    Default value                Current directory
    Accept pipeline input?       True (ByPropertyName, ByValue)
    Accept wildcard characters?  false

PS> Get-Help Get-ChildItem -Parameter filter

-Filter <String>
    Specifies a filter in the provider's format or language. The value of this parameter qualifies the Path parameter.
    The syntax of the filter, including the use of wildcards, depends on the provider. Filters are more efficient than
    other parameters, because the provider applies them when retrieving the objects, rather than having Windows
    PowerShell filter the objects after they are retrieved.

    Required?                    false
    Position?                    1
    Default value                None
    Accept pipeline input?       False
    Accept wildcard characters?  false

Notice the position values. A parameter that has to be named i.e. non-positional will have Named as the value for position:

PS> Get-Help Get-ChildItem -Parameter include

-Include <String[]>
    Specifies, as a string array, an item or items that this cmdlet includes in the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcards are permitted.

    The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the  C:\Windows directory.

    Required?                    false
    Position?                    named
    Default value                None
    Accept pipeline input?       False
    Accept wildcard characters?  false

You can also identify positional parameters from the syntax listing

Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Attributes {ReadOnly | Hidden | System | Directory | Archive | Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypted | IntegrityStream | NoScrubData}] [-Depth <UInt32>] [-Directory] [-Exclude <String[]>] [-File] [-Force]
[-Hidden] [-Include <String[]>] [-Name] [-ReadOnly] [-Recurse] [-System] [-UseTransaction] [<CommonParameters>]

The name of positional parameters are surrounded by square brackets

A question on the forum noted that the help file about_parameters states that positional parameters are numbers 1,2,3… and named parameters are effectively numbered 0 BUT the help file for Get-ChildItem numbers the positional parameters as 0 & 1

What’s happening?

If I remember correctly originally positional parameters were numbered from 1 but that was changed so 0 didn’t mean named. Looks like the about file didn’t get updated.

We can test this this with a function:

function test-pp {
 [CmdletBinding()]
 param (
   [Parameter(Position=0)]
   [int]$x,

   [Parameter(Position=1)]
   [string]$y
 )

 "$x is a number"
 "$y is a string"
}

PS> test-pp -x 2 -y abc
2 is a number
abc is a string

PS> test-pp 2 abc
2 is a number
abc is a string

So using position 0 works for positional parameters. What if we number them 1 & 2

function test-pp {
 [CmdletBinding()]
 param (
   [Parameter(Position=1)]
   [int]$x,

   [Parameter(Position=2)]
   [string]$y
 )

 "$x is a number"
 "$y is a string"
}

PS> test-pp -x 2 -y abc
2 is a number
abc is a string

PS> test-pp 2 abc
2 is a number
abc is a string

Works as well.

I’d recommend starting your positional parameter numbering from 0 and assume that about_parameters needs updating


April 30, 2017  1:26 PM

ISE or VS code?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

When PowerShell v2 shipped with the ISE it was seen as a great step forward. We now had a decent editor for creating PowerShell code and running that code. You could also invoke the debugger. Some extensions to ISE have occurred, most notably  Show-Command, but its essentially the same editor as in PowerShell v2

Visual Studio Code – now at version 1.11.2 – offers an interesting alternative. It manages a host of other languages as well as PowerShell. I currently have the extensions for Docker, Markdown, SQL, PowerShell, JSON and XML loaded. Many others are available as open source projects.

You can also open a terminal window which can be a command prompt, PowerShell or WSL bash – you could have all 3 open simultaneously if required.

The other big plus is that VS Code is cross platform so I can use the same editor on Windows and Linux. A big plus in these days of heterogeneous environments.

I’m going to try using VS Code instead of ISE for a while to see if it suits the way I work. If so it’ll become my default editor


April 30, 2017  9:58 AM

IT Professional?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Opinion

Way back when we were known as Administrators. Then the term IT Professional (often irritatingly shortened to IT Pro) appeared. We’re doing the same job but have a fancy new title.

Are System Administrators really professionals in the true sense of the word.

I would argue no.

– We don’t have universally recognised certification/qualification requirement

– We don’t have a professional body

– We don’t have an continuous learning requirement to maintain the title

– We don’t have a recognised body of knowledge that accurately defines how and what we should do

You may argue with these points and say for instance that you do keep learning – congratulations – you’re in the minority that does.

We have partial answers to my points for instance vendor certifications and best practice documentation but at the moment its all very piecemeal.

If IT wants to be treated as a profession its practitioners have to behave as professionals and at the moment I don’t think that happens in the vast majority of cases. There are exceptions and hopefully over time that behaviour will become the norm. Until then I’m going to stick with Administrators.


April 29, 2017  1:24 PM

DevOps ebook

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DevOps

DevOps is the latest “big thing” in IT. Whether it will make a difference or be dropped as everyone rushes to embrace the next “big thing” only time will tell.

For now, there’s a free ebook from the DevOps Collective (the people who bring you the PowerShell Summit) that looks at DevOps from the OPs perspective:

https://www.gitbook.com/book/devopscollective/devops-the-ops-perspective/details


April 29, 2017  12:31 PM

DSC Configuring Sharing

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Desired State Configuration

A new set of repositories on Github document a process for sharing end-to-end scenario based DSC configurations

https://blogs.msdn.microsoft.com/powershell/2017/04/28/dsc-configuration-sharing/

These are open to community involvement


April 28, 2017  2:22 PM

Mass dismount VHDs

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell, Virtual Disk

I’m going to be creating, using and discarding a number of VHDs for my diskpart and PowerShell series. When I have a number of them mounted I want a quick way to dismount them. Assuming I consistently keep them in the same folder then this very nicely does the job

Get-ChildItem -Path C:\test\ -Filter *.vhdx | Dismount-VHD

Why does it work?

Because Get-ChildItem emits System.IO.FileInfo objects that have a Path property and Dismount-VHD accepts pipeline input for the Path of the VHD to dismount:

-Path <String[]>
Specifies one or more virtual hard disk files for which the corresponding virtual hard disks are to be dismounted.

Required?                    true
Position?                    1
Default value                none
Accept pipeline input?       true (ByValue, ByPropertyName)
Accept wildcard characters?  false


April 28, 2017  1:20 PM

Diskpart and PowerShell–part 3: Initialize disk and create volume

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Disk storage, Powershell

Last time we created a virtual disk and mounted it. In this post we’ll initialize the disk and create a volume.

Start by remounting the disk

Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

You can now initialize the disk:

Initialize-Disk -Number 1

Create a partition:

New-Partition -DiskNumber 1 -DriveLetter F –UseMaximumSize

Ignore the message about formatting as you want to control that:

Format-Volume -DriveLetter F -FileSystem NTFS -Confirm:$false –Force

Your new disk is ready to use.

The diskpart equivalents can be found here: https://technet.microsoft.com/en-us/library/cc766465(v=ws.10).aspx

You can perform the creation and formatting of the disk in one pass:

New-VHD -Path C:\test\Test2.vhdx -Dynamic -SizeBytes 10GB |
Mount-VHD -Passthru |
Initialize-Disk -PassThru |
New-Partition -DriveLetter G -UseMaximumSize |
Format-Volume -FileSystem NTFS -Confirm:$false –Force

Parameterize the path, size and drive letter and you have a handy function to set up disks


April 27, 2017  1:44 PM

Diskpart and PowerShell–part 2: Create a virtual disk

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Disk storage, Powershell

Before we start digging into the diskpart/Storage module functionality we need a disk to practice on. I don’t recommend using your machine’s system disk – bad things will happen.

The Hyper-V module has  a New-VHD cmdlet so lets use that to create a disk to play with. The great thing about virtual disks is that you can delete then if everything goes horribly wrong.

There is a New-VirtualDisk cmdlet in the storage module but that works with storage pools. We’ll cover that later in the series.

Lets create a virtual disk:

New-VHD -Path C:\test\Test1.vhdx -Dynamic -SizeBytes 10GB

You can access the virtual disk using the path

Get-VHD -Path C:\test\Test1.vhdx

You need to mount the virtual disk before you can work with it

Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

Once mounted you can use get-Disk to identify the virtual disk

PS> Get-Disk | select Number, FriendlyName, PartitionStyle

Number FriendlyName               PartitionStyle
------ ------------               --------------
     1 Msft Virtual Disk          RAW
     0 Samsung SSD 840 PRO Series MBR

The clue is in the friendly name and that the partition style is RAW.

Next time we’ll look at formatting and partitioning the new drive. For now we’ll just dismount the virtual disk

Dismount-VHD -DiskNumber 1


April 26, 2017  1:41 PM

DiskPart and PowerShell–part 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Disk storage, Powershell

An attendee at the Summit made the statement that the DiskPart utility didn’t have any equivalent in PowerShell. That’s not strictly true as the storage module provides a lot of functionality that maps to diskpart functionality.

The module contents include:

PS> Get-Command -Module Storage | select name

Name
—-
Disable-PhysicalDiskIndication
Disable-StorageDiagnosticLog
Enable-PhysicalDiskIndication
Enable-StorageDiagnosticLog
Flush-Volume
Get-DiskSNV
Get-PhysicalDiskSNV
Get-StorageEnclosureSNV
Initialize-Volume
Write-FileSystemCache
Add-InitiatorIdToMaskingSet
Add-PartitionAccessPath
Add-PhysicalDisk
Add-TargetPortToMaskingSet
Add-VirtualDiskToMaskingSet
Block-FileShareAccess
Clear-Disk
Clear-FileStorageTier
Clear-StorageDiagnosticInfo
Connect-VirtualDisk
Debug-FileShare
Debug-StorageSubSystem
Debug-Volume
Disable-PhysicalDiskIdentification
Disable-StorageEnclosureIdentification
Disable-StorageHighAvailability
Disable-StorageMaintenanceMode
Disconnect-VirtualDisk
Dismount-DiskImage
Enable-PhysicalDiskIdentification
Enable-StorageEnclosureIdentification
Enable-StorageHighAvailability
Enable-StorageMaintenanceMode
Format-Volume
Get-DedupProperties
Get-Disk
Get-DiskImage
Get-DiskStorageNodeView
Get-FileIntegrity
Get-FileShare
Get-FileShareAccessControlEntry
Get-FileStorageTier
Get-InitiatorId
Get-InitiatorPort
Get-MaskingSet
Get-OffloadDataTransferSetting
Get-Partition
Get-PartitionSupportedSize
Get-PhysicalDisk
Get-PhysicalDiskStorageNodeView
Get-PhysicalExtent
Get-PhysicalExtentAssociation
Get-ResiliencySetting
Get-StorageAdvancedProperty
Get-StorageDiagnosticInfo
Get-StorageEnclosure
Get-StorageEnclosureStorageNodeView
Get-StorageEnclosureVendorData
Get-StorageFaultDomain
Get-StorageFileServer
Get-StorageFirmwareInformation
Get-StorageHealthAction
Get-StorageHealthReport
Get-StorageHealthSetting
Get-StorageJob
Get-StorageNode
Get-StoragePool
Get-StorageProvider
Get-StorageReliabilityCounter
Get-StorageSetting
Get-StorageSubSystem
Get-StorageTier
Get-StorageTierSupportedSize
Get-SupportedClusterSizes
Get-SupportedFileSystems
Get-TargetPort
Get-TargetPortal
Get-VirtualDisk
Get-VirtualDiskSupportedSize
Get-Volume
Get-VolumeCorruptionCount
Get-VolumeScrubPolicy
Grant-FileShareAccess
Hide-VirtualDisk
Initialize-Disk
Mount-DiskImage
New-FileShare
New-MaskingSet
New-Partition
New-StorageFileServer
New-StoragePool
New-StorageSubsystemVirtualDisk
New-StorageTier
New-VirtualDisk
New-VirtualDiskClone
New-VirtualDiskSnapshot
New-Volume
Optimize-StoragePool
Optimize-Volume
Register-StorageSubsystem
Remove-FileShare
Remove-InitiatorId
Remove-InitiatorIdFromMaskingSet
Remove-MaskingSet
Remove-Partition
Remove-PartitionAccessPath
Remove-PhysicalDisk
Remove-StorageFileServer
Remove-StorageHealthSetting
Remove-StoragePool
Remove-StorageTier
Remove-TargetPortFromMaskingSet
Remove-VirtualDisk
Remove-VirtualDiskFromMaskingSet
Rename-MaskingSet
Repair-FileIntegrity
Repair-VirtualDisk
Repair-Volume
Reset-PhysicalDisk
Reset-StorageReliabilityCounter
Resize-Partition
Resize-StorageTier
Resize-VirtualDisk
Revoke-FileShareAccess
Set-Disk
Set-FileIntegrity
Set-FileShare
Set-FileStorageTier
Set-InitiatorPort
Set-Partition
Set-PhysicalDisk
Set-ResiliencySetting
Set-StorageFileServer
Set-StorageHealthSetting
Set-StoragePool
Set-StorageProvider
Set-StorageSetting
Set-StorageSubSystem
Set-StorageTier
Set-VirtualDisk
Set-Volume
Set-VolumeScrubPolicy
Show-VirtualDisk
Start-StorageDiagnosticLog
Stop-StorageDiagnosticLog
Stop-StorageJob
Unblock-FileShareAccess
Unregister-StorageSubsystem
Update-Disk
Update-HostStorageCache
Update-StorageFirmware
Update-StoragePool
Update-StorageProviderCache
Write-VolumeCache

In this mini series I’m going to go through a number of the diskpart options and show you how to do the same with the Storage module cmdlets.

I’m not sure if all diskpart options are available but it’ll be fun to find out.

The Storage module was introduced with Windows 8/Server 2012.

ls $pshome\modules\storage

or

Get-ChildItem $pshome\modules\storage

if you prefer shows a number of cdxml files. This means that the cmdlets are based on CIM classes which you can see at

Get-CimClass -Namespace ROOT/Microsoft/Windows/Storage

These classes aren’t available on versions of Windows prior to Windows 8/Server 2012.

I’ll also have a look at some of these classes to see if there’s anything we can do that isn’t covered directly by the storage module cmdlets.


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: