Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, March 14, 2012

Send code to machines based on their OU and Model number with PowerShell

This was the final project of my March PowerShell class in Portland Maine.  This is the “Real World” task that the class brought in.  They needed to be able to specify OU in which code would be sent to each client based on the model number of that client.  Below is the code.


To run it, dot source it into your PowerShell session and to a Get-Help Send-Code –Full command to learn how to use it.  I left a final challenge in the NOTES filed for the class.

Function Get-ComputerList

# Get the target OU Object from AD.
$OUName = "*" + $OUName + "*"
$TargetOU = Get-ADObject -filter 'Name -like $OUName'

# Build a list of clients to target.
$TargetObject = @()

$Computers = Get-ADComputer -filter * -SearchBase $TargetOU

ForEach ($Computer in $Computers)
$Obj = New-Object PSObject
$Obj | Add-Member NoteProperty -Name "ComputerName" -Value $Computer.Name

$TargetObject += $Obj

# End: ForEach ($Computer in $Computers)

Write-Output $TargetObject

Function Get-WMIInfo


$Object = @()

ForEach ($Computer in $ComputerList)
Write-Host "Attempting to connect to "$Computer.ComputerName -ForegroundColor Green
# Test to make sure the computer is online.

If (Test-Connection $Computer.ComputerName -Count 1 -ea "SilentlyContinue")
Write-Host "Collection Data" `
-ForegroundColor Cyan
$Obj = New-Object PSObject
$obj | Add-Member NoteProperty -Name "ComputerName" -Value $Computer.ComputerName
$obj | Add-Member NoteProperty -Name "Model" -Value (Get-WmiObject Win32_ComputerSystem -ComputerName $Computer.ComputerName).Model
$Object += $Obj


Write-Host "$Computer.ComputerName is Offline" -ForegroundColor White -BackgroundColor Red
# End: ForEach ($Computer in $ComputerList)

Write-Output $Object
# End: Function Get-WMIInfo

Function Invoke-ScriptCommands

# List the block of codes that you wish to execute here.
# Be sure to label the variables something that will help
# you associate the code with the model number.

$VM = {
$Services = Get-Service
ForEach ($Service in $Services)
Write-host $ " " $Service.Status

# End: $VM ScriptBlock

# Here is where you execute the code against multiple computers.
ForEach ($Computer in $ComputerInfo)
Write-Host "Sending code to " $Computer.ComputerName $Computer.Model

Switch ($Computer.Model)
"Virtual Machine" {Invoke-Command -ComputerName $Computer.ComputerName -ScriptBlock $VM}

# End: Switch ($ComputerInfo.Model)
} # End: ForEach ($Computer in $ComputerInfo)
} # End:Function Invoke-ScriptCommands

Sends code to machine base on their OU and model number.

Sends different code to client in an Organizational Unit. The code sent is based on
the model number of the client.

The display name of the Organizational Unit that contains the clients that you want to send code to.

Send-Code clients

Extracts the clients in the organizational unit "clients". If code is present for th
model of client, then that code is send and executed.

I left a challenge for you guys. In a large OU, a client may go offline before the code is sent.
See if you can modify this code so that it executes against each client as they are
found to be online.

Function Send-Code
Param ([Parameter(Position=0,Mandatory=$True)]$OU)

# Import the Active Directory module.
Import-Module ActiveDirectory

# Get a list of computer names from a specific OU.
Store an object that in $ComputerList that contains the client name.
$ComputerList = Get-ComputerList $OU

# Get the WMI information and store it in an object with the client name.
Save this object in $ComputerInfo.
$ComputerInfo = Get-WMIInfo $ComputerList

# Code to send to clients of different model numbers.
Invoke-ScriptCommands $ComputerInfo

# End: Function Send-Code

Monday, March 12, 2012

Problem using Invoke-Command with Windows 7 client

While testing some code that used PowerShells Invoke-Command to send code to both Windows Server 2008 R2 and Windows 7, I discovered an inconsistency in the way Windows Remoting is applied to both platforms. The servers worked fine.  Even though I disabled to firewall for testing, I received this error when sending commands to the Windows 7 client:


[ClientName] Connecting to remote server failed with the following error message : The client cannot connect to the destingatoin specified in the request.  Verify that the service on the destination is running and is accepting requests.  Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM.  If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service” winrm quickconfig”.  For more information, see the about_Remote_Troubleshooting Help topic.

+ CategoryInfo       : OpenError: (:) [], PSRemotingTransportException

+ FullyQualifiedErrorId : PSSessionStateBroken

The fix that I came up with was to deploy and Group Policy that allowed remoting and scope it to the client.

1. Open Group Policy Management console
2. Select the domain, and right-click the Default Domain Policy to edit it
3. Computer Configuration > Policies > Administrative Templates > Windows Components > Windows Remote Management > WinRM Service
4. Allow automatic configuration of listeners, put * in the IPv4 and IPv6 filter text boxes
5. Computer Configuration > Policies > Administrative Templates > Windows Components > Windows Remote Shell
6. Allow Remote Shell Access, Enabled
7. Save everything. Run Gpupdate on all servers that are currently running.