Skip to main content

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
{
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

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.