PowerShell for Windows Admins


December 18, 2015  12:53 PM

Inputting computer names

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

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.

December 18, 2015  5:21 AM

WMF 5.0 now RTM

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

WMF 5.0 , including PowerShell 5.0, was released to RTM overnight – why does this lways happen when I’m asleep Smile

http://blogs.msdn.com/b/powershell/archive/2015/12/16/windows-management-framework-wmf-5-0-rtm-is-now-available.aspx

Versions are available for:

Windows Server 2012 R2

Windows Server 2012

Windows Server 2008 R2 SP1


December 17, 2015  1:31 PM

Outputting AD data to CSV

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Powershell

Back in this post https://richardspowershellblog.wordpress.com/2014/12/29/using-givenname-and-surname-instead-of-samaccountname/

I showed how to get AD information using afirst and last names rather than the samAccountName.

A question came up about reading from a CSV containing a list of names and outputting the results to another CSV.

My preference would be to create a single PowerShell pipeline.

Import-Csv -Path ./adtest.csv |
foreach  {
$fname = $psitem.GivenName
$lname = $psitem.Surname
Get-ADUser -Filter {GivenName -eq $fname -and Surname -eq $lname} -Properties * |
select SamAccountName, Division, Office, City
} |
Export-Csv aduserslist.csv –NoTypeInformation

Read the CSV and pipe to foreach. Get the data and push onto pipeline. Export to CSV.


December 15, 2015  2:33 PM

Recent Announcements from the PowerShell Team

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The PowerShell Team have made a number of interesting  announcements recently:

Feedback is moving from Connect to User Voice

http://blogs.msdn.com/b/powershell/archive/2015/12/14/improving-the-powershell-feedback-experience-with-uservoice.aspx

This is where you give your feedback and report bugs

For programmers the PowerShell reference assemblies are now available through Nuget.org

http://blogs.msdn.com/b/powershell/archive/2015/12/12/powershell-sdk-reference-assemblies-available-via-nuget-org.aspx

Meaning you can use Install-Package to get them.  Assemblies for PowerShell versions 3.0, 4.0 and 5.0 are available

The tests the PowerShell Team uses to test new PowerShell builds are available on GitHub

http://blogs.msdn.com/b/powershell/archive/2015/12/07/powershell-tests-released-on-github.aspx

Useful to understand how PowerShell is tested and to get a deeper insight into PowerShell


December 11, 2015  6:04 AM

Formating multiple outputs

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Using Get-WmiObject and Get-Service you can do this:

PS> Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode

StartMode
———
Auto
PS> Get-Service -Name BITS | select Status

Status
——
Running

If you try running the two commands in a script, in ISE or even like this:

PS> Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode; Get-Service -Name BITS | select Status

StartMode
———
Auto

You only get the first result.  If you reverse the order of the commands:

PS> Get-Service -Name BITS | select Status; Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode

Status
——
Running

You still get the first result.

When you run a pipeline the results are automatically piped to Out-Default. The formatting system then decides to use a table format because you have less than 5 properties.

When the commands are run individually each calls Out-Default individuallly. When the 2 commands are run in a script (or other option that causes them to execuate together) the formatting takes its direction from the first object it receives and because the second object doresn’t have matching properties nothing is displayed.

The answer is to manually force each command to pipe to out-default

PS> Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode | Out-Default; Get-Service -N
ame BITS | select Status | Out-Default

StartMode
———
Auto

 

Status
——
Running

OR

PS> Get-Service -Name BITS | select Status | Out-Default; Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’B
ITS’” | select StartMode | Out-Default

Status
——
Running

 

StartMode
———
Auto

This is a common issue that users trip over as they learn PowerShell.

 


December 8, 2015  11:34 AM

PowerShell Day at Manning

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Books, Powershell

Manning have a set of PowerShell books on their deal of the day for 9 December 2015. The deal includes:

Windows PowerShell in Action, Third Edition
PowerShell and WMI
PowerShell Deep Dives
Powershell in Depth, Second Edition
PowerShell in Practice
Learn Windows PowerShell in a Month of Lunches, Second Edition

The deal will go live at Midnight US ET and will stay active for ~48 hours.

Get 50% off these books with code dotd120915au at manning.com

 


November 27, 2015  8:53 AM

PowerShell Summit 2016–agenda complete

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The agenda is complete on the event web site. We still have to finalise the sessions from the PowerShell Team but they will be giving a number of sessions.


November 25, 2015  7:06 PM

A deal of the day you’ll not want to miss

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Books, Powershell

PowerShell in Action, Third Edition is part of Manning’s Deal of the Day on Friday 27 November – see www.manning.com for details on the day –

Half off my book Windows PowerShell in Action, Third Edition. Use code dotd112715au at https://www.manning.com/books/windows-powershell-in-action-third-edition


November 20, 2015  10:45 AM

Creating Registry Key

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

I had a question left on the blog asking how to create a registry key. My preferred method is to use the CIM class = StdRegProv. Its a static class so you don’t need to create an object

[uint32]$hklm = 2147483650
$newkey = ‘SOFTWARE\NewKey’

Invoke-CimMethod -ClassName StdRegProv -MethodName CreateKey -Arguments @{hDefKey = $hklm; sSubKeyName = $newkey}

Define the variables for the HIVE in this case HKLM – local machine . Notice that the value has to be an unsigned integer.

The key you want to create is just the path to the key. if you need to create multiple levels of subkeys they will all create from a single path.

Then use Invoke-CimMethod to call the CreateKey method on StdRegProv. The hash table in Arguments parameter has the method parameter names and appropriate values.

If everything works you’ll get a return value of 0.

How did I know which parameters the method took?

I used Get-CimClass but that’s a story for another post.


November 20, 2015  4:13 AM

PowerShell column

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The first in a regular(ish) series of articles has been published on the TechNet UK Blog.

http://blogs.technet.com/b/uktechnet/archive/2015/11/20/why-you-need-to-learn-powershell.aspx

Covering all things PowerShell related the articles will be appearing every 3-4 weeks.


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: