Author Posts

May 18, 2017 at 8:59 pm

Working on a script that will go get all AD Computers, filter only the enabled ones, extend the name, loop through and tnc each one, and for only ping succeeded I want to go and check last boot time, and if it's greater than 7 days I want to send them "Send-NetMessage" to reboot their computers ...

$PCs = Get-adcomputer -Filter * | Where {$_.Enabled -eq $true} | Select -Expand Name
Foreach ($PC in $PCs) {
TNC $PC
}
Now I want to select only the PCs that I was able to PING and loop through them to go and Get
Get-CimInstance -ComputerName $PC -ClassName win32_operatingsystem | select csname, lastbootuptime

Select Dates (Get-Date).AddDays(-7)

Then pass the output to another Function Send-NetMessage

Is this possible ??

May 18, 2017 at 9:04 pm

Yes...should be. Where are you stuck?
Couldn't you do a:

If (Test-Connection $PC){
Get-CimInstance -ComputerName $PC -ClassName win32_operatingsystem | select csname, lastbootuptime 
Select Dates (Get-Date).AddDays(-7) | send-netmessage
}

May 18, 2017 at 10:11 pm

Tried this

Foreach($PC in $PCs){
$Online = Test-Connection -BufferSize 2 -ComputerName $PC -Count 1 -Quiet
If ($Online -eq $true) {Get-CimInstance -ComputerName $PC -ClassName win32_operatingsystem | select csname, lastbootuptime
}
}

But Still loops through all Computers, not just the online ones?

May 19, 2017 at 3:04 am

Have you tried eliminating line 2 and simply using:

If (test-connection -computername $PC -quiet) {
    Do-Something
}

trying to simplify the logic. 🙂