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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...