Inputting computer names

Somehting I was writing yesterday started me thinking about the way you input a list of computer names to a cmdlet. Many cmdlets have a ComputerName parameter so knowing how to deal with this sort of input will help. There are a range of techniques.

One of the simplest approaches is to create your list as a variable and use the variable:

$servers = ‘SERVER02’, ‘W12R2SCDC01’, ‘W12R2SUS’, ‘W12R2DSC’, ‘W12R2TGT’, ‘W12R2WEB01’, ‘W12R2WEB02’, ‘W12R2OD01’
Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName $servers

If you don’t need the server list as a variable then input it directly to the cmdlet

Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName ‘SERVER02’, ‘W12R2SCDC01’, ‘W12R2SUS’, ‘W12R2DSC’, ‘W12R2TGT’, ‘W12R2WEB01’, ‘W12R2WEB02’, ‘W12R2OD01’

The drawback is that the list of machine names is embedded in your code. The variable approach above is easier to maintain

A common approach is to use Foreach-Object

$servers = ‘SERVER02’, ‘W12R2SCDC01’, ‘W12R2SUS’, ‘W12R2DSC’, ‘W12R2TGT’, ‘W12R2WEB01’, ‘W12R2WEB02’, ‘W12R2OD01’
$servers | foreach {Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName $psitem}

This works but adds unnecessary code. If you are performing other tasks in the foreach processing it may be worthwhile

Likewise using a foreach loop

$servers = ‘SERVER02’, ‘W12R2SCDC01’, ‘W12R2SUS’, ‘W12R2DSC’, ‘W12R2TGT’, ‘W12R2WEB01’, ‘W12R2WEB02’, ‘W12R2OD01’
foreach ($server in $servers) {
Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName $server

The foreach loop will be quicker but use more memory.

If you create a CSV file make sure you use ComputerName as the field header – then you can do this

Import-Csv .\computers.csv |
Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’

if the header is something else – eg computer – then you need to use foreach-object

Import-Csv .\computers.csv |
foreach {
Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName $_.Computer

Import-Csv .\computers.csv |
foreach {
Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName $psitem.Computer

If you have the names in a text file you may be tempted to do this

Get-Content .\computers.txt |
foreach {
Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName $_

You can also use $psitem instead of $_

A neater way is to do this

Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName (Get-Content .\computers.txt)

which gets us back to the PowerShell one liner solution.

If you’re reading the computernames from Active Directory you have to do a bit of work because the AD cmdlets don’t return a computerName property – they return name.

You can either

Get-ADComputer -Filter * |
foreach {
Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName $psitem.Name

or if you want to be a bit cleverer

Get-ADComputer -Filter * |
select @{N=’ComputerName’; E = {$_.Name}} |
Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’

Use select-object to create a computername property on the pipeline object.

You can of course revert to the one liner solution

Get-CimInstance -ClassName Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName (Get-ADComputer -Filter * | select -ExpandProperty name)

Use ExpandProperty on select-object to strip out the Name value and pass that to your cmdlet.

As you can see there are many ways to achieve the same goal – some easier than others. These examples aren’t necessarily complete.

Next time you need to pass a list of values to a cmdlet stop and think about the best way to do it. You might save some typing and more efficient code.

This entry was posted in Powershell Basics. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s