Both of the follow scripts work. Script 1 will show me machines that are online in GREEN and it will show me the machines that can't ping in ORANGE. (why orange I don't know since I did not say too) But Script 2 only returns the machines that it can ping. if it can't ping then it does not show me that machine in the output. Script 1 returns 25 total machines, Script 2 returns only 22 machines (3 are off line). In Script 2 I have tried moving my closing bracket '}' to re position the ELSE and it still does not give the desired output. What am I missing here?
# script 1 ##################################
$PCName = (Get-ADComputer -Filter * -SearchBase "OU=1200 Baker,OU=CorpWorkstations,dc=Acme,dc=org").Name
foreach ($machine in $PCName) {if (test-Connection -ComputerName $machine -Count 2 -Quiet ) {write-Host "$machine is alive and Pinging " -ForegroundColor Green} else{ Write-Warning "$machine seems dead not pinging"}}
# Script 2 ##################################
$PCName = (Get-ADComputer -Filter * -SearchBase "OU=1200 Baker,OU=CorpWorkstations,dc=Acme,dc=org").Name
foreach($machine in $PCName) {if (test-Connection -ComputerName $machine -Count 2 -Quiet ) { Get-WmiObject win32_operatingsystem -ComputerName $machine | select csname, @{LABEL='LastBootUpTime' ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}}
else { Write-Warning "$machine seems dead not pinging" -ForegroundColor Green}}