Need some Help.

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of MJ MJ 2 months ago.

  • Author
    Posts
  • #71036
    Profile photo of Del
    Del
    Participant

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

  • #71044
    Profile photo of MJ
    MJ
    Participant

    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
    }
    • #71053
      Profile photo of Del
      Del
      Participant

      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?

  • #71068
    Profile photo of MJ
    MJ
    Participant

    Have you tried eliminating line 2 and simply using:

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

    trying to simplify the logic. 🙂

You must be logged in to reply to this topic.