This script will help you gather info on devices in an OU. This script will lookup the devices in the OU you define and then for each it will; Test-NetworkConnection to see if the device is online, If online get the model, current user and OS Version. It will store this info in an array that you can then display or export to csv.
Fill input array
There are a couple ways to get info ready for this script. One way is to create a CSV file with one column named name and add one server name per line. We can then fill our $servers array with this command:
$servers = Import-Csv -Path "C:\Path\To\CSV.csv
The other way is to get PC names from an OU in Active Directory you can do that with this command. (make sure you have a connection to AD, see this Post)
$servers = Get-ADComputer -Filter * -SearchBase "OU=Computers,DC=Domain,DC=com"
Script:
$result = @()
foreach ($server in $servers) {
$server = $server.name
try {
Test-NetConnection -ComputerName $server -ErrorAction Stop
$model = Invoke-Command -computername $server -scriptblock {WMIC CSPRODUCT GET NAME} -ErrorAction Stop
$username = Invoke-Command -computername $server -scriptblock {WMIC COMPUTERSYSTEM GET USERNAME} -ErrorAction Stop
$OS = Invoke-Command -computername $server -scriptblock {WMIC OS GET VERSION} -ErrorAction Stop
Write-Host $server "," $model "," $username "," $OS
$result += "$server,$model,$username,$OS"
}
catch {
Write-Host $server ",is,Off,line"
$result += "$server,is,Off,line"
}
}
Now we have an array of results you can display just by typing the $result and hit enter in PS. Or you can export it with this command:
$result | out-file C:\ps\output.txt