PowerShell for Windows Admins

June 26, 2017  1:43 PM

Passing parameters to a script block

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Passing parameters to a scriptblock seems to be an issue at the moment.

Consider a simple scriptblock

Invoke-Command -ScriptBlock {Get-Process}

How can you modify that to parameterise the processes that are returned.

Its a two step process. Add a parameter block to your script block and secondly pass the correct values to the scriptblock

Invoke-Command -ScriptBlock {
param ($proc )
Get-Process -Name $proc
} -ArgumentList “power*”

If you want to pass an array of values to the scriptblock you have 2 options. First abandon the named parameter

$p = (“power*”, “win*”)

Invoke-Command -ScriptBlock {

Get-Process -Name $args
} -ArgumentList $p

if you use $args then all arguments are available through the array

Scriptblocks unravel the array of arguments so if you want named parameters then you need to force the scriptblock to accept an array by using the unary comma operator

$p = (“power*”, “win*”)

Invoke-Command -ScriptBlock {
param ($proc )

Get-Process -Name $proc
} -ArgumentList (,$p)

June 26, 2017  10:10 AM

Generating passwords

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
"Computer security", Password, Powershell

Generating new passwords can be a painful business.  There are many ways of accomplishing  password generation –  depending on your needs.  One suggestion for generating passwords is to use a GUID as the basis of the password.

PS> New-Guid


You could then remove the hyphens and extract part of the guid

PS> (New-Guid).Tostring() -replace '-'

You need to decide the length of the password.  Guids are 32 characters so make sure you start your extraction in a position that gives room for the length you need

$length = 8

((New-Guid).Tostring() -replace '-').Substring((Get-Random -Minimum 0 -Maximum (31-$length)), $length)

Your results will look like these examples


The drawback is that you only have numbers and lower case characters

If you use the Membership class from System.Web you can do this

PS> Add-Type -AssemblyName System.Web
PS> [System.Web.Security.Membership]::GeneratePassword(8,2)

The GeneratePassword method takes 2 arguments – the first is the length of the password and the second is the number of non-alphanumeric characters

If you are running PowerShell v5 you can utilise the using keyword instead of Add-Type

PS> using assembly System.Web
PS> [System.Web.Security.Membership]::GeneratePassword(8,1)

June 24, 2017  11:07 AM

Nano server changes

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
containers, Windows Server 2016

Nano server is the small, really small, footprint install version of Windows Server that was introduced with Server 2016. Nano server changes are coming.

it has a limited number of roles available to to install – much like the original version of Server core.

Recent announcements – https://blogs.technet.microsoft.com/hybridcloud/2017/06/15/delivering-continuous-innovation-with-windows-server/


indicates that Nano server is going to become even smaller (by more than 50%) and dedicated to delivering Containers. The infrastructure related roles will be removed. Nano Server will ONLY be available as a container base OS image

In addition, starting this Autumn, Nano server and Server Core will getting 2 feature updates per year.

June 14, 2017  1:43 PM

Deal of the Day – 15 June 2017

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Books, Hyper-V, Powershell

My book is Manning’s Deal of the Day  – 15 June 2017:

Half off Learn Hyper-V in a Month of Lunches. Use code dotd061517au at http://bit.ly/2rZXI9x

Sign up for DoD notifications at https://www.manning.com/dotd

June 12, 2017  7:57 AM

Find the logged on user

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell, WMI

One method of finding the logged on users is to use CIM

$ComputerName = $env:COMPUTERNAME
Get-CimInstance -ClassName Win32_Process -ComputerName $ComputerName -Filter "Name = 'explorer.exe'" | 
foreach {
    $lguser = Invoke-CimMethod -InputObject $psitem -MethodName GetOwner
    $Properties = @{
       ComputerName = $ComputerName
       User = $lguser.User
       Domain = $lguser.Domain
       Time = $User.CreationDate 
    New-Object -TypeName PSObject -Property $Properties

Get the Win32_Process instances for explorer.exe and foreach of them use the GetOwner method  to get the owners names and domain. Create an object and ouput

May 31, 2017  4:42 PM

Get-PhysicalDisk options

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

These are the Get-PhysicalDisk options for identifying the disk you want

-UniqueId <string>

-ObjectId <string>

-FriendlyName <string>

-InputObject <CimInstance#MSFT_PhysicalDisk>

-StorageSubsystem <CimInstance#MSFT_StorageSubsystem>

-StorageEnclosure <CimInstance#MSFT_StorageEnclosure>

-StorageNode <CimInstance#MSFT_StorageNode>

-StoragePool <CimInstance#MSFT_StoragePool>

-VirtualDisk <CimInstance#MSFT_VirtualDisk>

When dealing with disks installed in the machine then the friendly names is the easiest to use

PS> Get-PhysicalDisk | Format-List UniqueId, ObjectId, FriendlyName
UniqueId     : 60022480233DF060FE631B8A4EDD93A0
ObjectId     : {1}\\W510W16\root/Microsoft/Windows/Storage/Providers_v2\SPACES_PhysicalDisk.ObjectId=”{1dab9cf6-a1b4-11e6-a890-806e6f6e6963}:PD:{12e941a8-6125-c008-8806-8868642331ef}”
FriendlyName : Msft Virtual Disk

UniqueId     : {d8e80f34-22bc-0a36-b302-d96abe30a6cc}
ObjectId     : {1}\\W510W16\root/Microsoft/Windows/Storage/Providers_v2\SPACES_PhysicalDisk.ObjectId=”{1dab9cf6-a1b4-11e6-a890-806e6f6e6963}:PD:{d8e80f34-22bc-0a36-b302-d96abe30a6cc}”
FriendlyName : Samsung SSD 840 PRO Series

May 30, 2017  1:55 PM

String casing

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

There are times when you may want to change string casing. You have a couple of options.

There are a couple of methods on the string class that you can use to modify the case of a string.

PS> 'aaa'.ToUpper()

PS> 'AAA'.ToLower()

Alternatively you can use the culture information

PS> (Get-Culture).TextInfo.ToLower('AAA')
 PS> (Get-Culture).TextInfo.ToUpper('aaa')
 PS> (Get-Culture).TextInfo.ToTitleCase('aaa')

The interesting one is To Titlecase which will capitalise the first letter and make the rest lower case

PS> (Get-Culture).TextInfo.ToTitleCase('aaBaaC')

At least it does for my culture settings

PS> Get-Culture

LCID             Name             DisplayName
 ----             ----             -----------
 2057             en-GB            English (United Kingdom)

You’ll need to test what it does if you have a different culture setting

May 29, 2017  12:51 PM

Build a better pull server

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Desired State Configuration, Powershell

DSC functions in 2 modes – push (most basic) and pull. Creating a pull server is a non-trivial task and the out-of-the-box pull server has some issues. Some of the folks at powershell.org have decided its time to build a better pull server.

There’s a project on github that supplies the code for the open source, cross platform, pull server project known as tug.  pull – tug – pull… you get the picture.

You can find the project at https://github.com/PowerShellOrg/tug

If you’re using DSC download it, give it a try and feed back to the project what you’ve discovered.

May 26, 2017  8:53 AM

Diskpart and PowerShell – part 6: Multiple partitions on a disk

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

So far we’ve looked at creating a single partition on a disk. This time we’ll look at how you can create multiple partitions on a disk. The are good reasons not to do this but its something I’ve seen done on a frequent basis.

Lets create a 20GB disk as an example and mount it

New-VHD -Path C:\test\Test1.vhdx -Dynamic -SizeBytes 20GB
Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

Initialise the disk

Initialize-Disk -Number 1

Now we can create some partitions

New-Partition -DiskNumber 1 -DriveLetter F -Size 5GB
New-Partition -DiskNumber 1 -DriveLetter G -Size 5GB
New-Partition -DiskNumber 1 -DriveLetter H -Size 5GB
New-Partition -DiskNumber 1 -DriveLetter I -Size 4.87GB

The reason that the last partition is only 4.87 G is that 128MB of disk space is reserved

PS> Get-Partition -DiskNumber 1 | Format-Table -AutoSize

   DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#2&1f4adffe&0&000003#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

PartitionNumber DriveLetter Offset         Size Type
--------------- ----------- ------         ---- ----
1                           17408        128 MB Reserved
2               F           135266304      5 GB Basic
3               G           5503975424     5 GB Basic
4               H           10872684544    5 GB Basic
5               I           16241393664 4.87 GB Basic

You can format the 4 new volumes in one pass

Get-Partition -DiskNumber 1 | 
where Type -ne 'Reserved' | 
Format-Volume -FileSystem NTFS -Confirm:$false –Force

PS> Get-Partition -DiskNumber 1 | Get-Volume | select DriveLetter, FileSystem, Size

DriveLetter FileSystem       Size
----------- ----------       ----
          H NTFS       5368705024
          G NTFS       5368705024
          I NTFS       5229244416
          F NTFS       5368705024

The Storage module can be used to simply and easily create multiple volumes on a disk

May 23, 2017  11:35 AM

PowerShell in Azure Cloud Shell

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Azure, Powershell

if you are an Azure user see this post from the PowerShell team about the Azure Cloud Shell – https://blogs.msdn.microsoft.com/powershell/2017/05/23/coming-soon-powershell-in-azure-cloud-shell/

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: