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
{
Param
(
[Parameter(Mandatory=$True)]$OUName
)
# 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
{
Param
(
$ComputerList
)
$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
}
Else
{
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
{
param
(
$ComputerInfo
)
# 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.name " " $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
<#
.SYNOPSIS
Sends code to machine base on their OU and model number.
.DESCRIPTION
Sends different code to client in an Organizational Unit. The code sent is based on
the model number of the client.
.PARAMETER OU
The display name of the Organizational Unit that contains the clients that you want to send code to.
.EXAMPLE
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.
.NOTES
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
Comments