# Need some Help.

**URL:** <https://forums.powershell.org/t/need-some-help/8658>\
**Category:** PowerShell Help\
**Created:** [May 18, 2017, 3:29pm UTC](https://forums.powershell.org/t/need-some-help/8658 "2017-05-18T15:29:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![del-mo](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@del-mo](https://forums.powershell.org/u/del-mo)\
**Post date:** [May 18, 2017, 3:29pm UTC](https://forums.powershell.org/t/need-some-help/8658/1 "2017-05-18T15:29:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![teskemj](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@teskemj](https://forums.powershell.org/u/teskemj)\
**Post date:** [May 18, 2017, 3:34pm UTC](https://forums.powershell.org/t/need-some-help/8658/2 "2017-05-18T15:34:11Z")

</div>

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
}
```

---

<div class="post-metadata">

**Author:** ![del-mo](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@del-mo](https://forums.powershell.org/u/del-mo)\
**Post date:** [May 18, 2017, 4:41pm UTC](https://forums.powershell.org/t/need-some-help/8658/3 "2017-05-18T16:41:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![teskemj](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@teskemj](https://forums.powershell.org/u/teskemj)\
**Post date:** [May 18, 2017, 9:34pm UTC](https://forums.powershell.org/t/need-some-help/8658/4 "2017-05-18T21:34:01Z")

</div>

Have you tried eliminating line 2 and simply using:

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

trying to simplify the logic. 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/need-some-help/8658/5 "2024-05-16T20:38:45Z")

</div>

Closed on May 16, 2024, 8:38pm UTC


