none
Powershell Script to find multiple computer times. RRS feed

  • Question

  • I have the below script to query multiple computers for there up time but I receive an error message if the computer is not contactable (The computer is switched off). Can you please advise how I can modify to my script so that it ignores the errors and not to display them.

    $Computer=Get-Content“Computer.txt”



    foreach

    ($Serverin$Computer)

    {



    $wmi

    =Get-WmiObject-classWin32_OperatingSystem-computer$server-EASilentlyContinue



    $LBTime

    =$wmi.ConvertToDateTime($wmi.Lastbootuptime)



    [

    TimeSpan]$uptime=New-TimeSpan$LBTime$(get-date)



    Write-host

    $server“Uptime: ”$uptime.days “Days”$uptime.hours “Hours”$uptime.minutes “Minutes”$uptime.seconds “Seconds”

    }

    Error message

    You cannot call a method on a null-valued expression.

    At C:\temp\uptime.ps1:9 char:1

    + $LBTime=$wmi.ConvertToDateTime($wmi.Lastbootuptime)

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

        + FullyQualifiedErrorId : InvokeMethodOnNull

    Maninder

    Friday, August 19, 2016 4:06 PM

Answers

  • Get-Content .\pcList.txt | ForEach-Object {
    
        If (Test-Connection -ComputerName $_ -Count 1 -Quiet) {
    
            # PC is alive
    
        } Else {
    
            # PC isn't alive
    
        }
    
    }


    Monday, August 22, 2016 4:08 PM

All replies

  • Hi,

    Drop the -ErrorAction SilentlyContinue and see what errors you're running into.


    Friday, August 19, 2016 4:19 PM
  • Use this script instead:

    Windows IT Pro: Getting Computer Uptime Using PowerShell


    -- Bill Stewart [Bill_Stewart]

    Friday, August 19, 2016 4:21 PM
    Moderator
  • If I drop  -EA SilentlyContinue, I receive the following error.

    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

    At C:\temp\uptime.ps1:7 char:6

    + $wmi=Get-WmiObject -class Win32_OperatingSystem -computer $server

    +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException

        + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    You cannot call a method on a null-valued expression.

    At C:\temp\uptime.ps1:9 char:1

    + $LBTime=$wmi.ConvertToDateTime($wmi.Lastbootuptime)

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

        + FullyQualifiedErrorId : InvokeMethodOnNull

    Friday, August 19, 2016 5:04 PM
  • What you are doing is assuming you can make a WMI connection to the remote computer.

    If the remote computer is not accepting incoming WMI connections (for example, firewall), then of course the connection will fail.


    -- Bill Stewart [Bill_Stewart]

    Friday, August 19, 2016 5:10 PM
    Moderator
  • Yes that is correct.  All computers are accessible via WMI, I only receive the error when a machine is turned off.  So I want the script not to through up an error when the machine is turned off or is not accessible.
    Monday, August 22, 2016 7:53 AM
  • Use Test-Connection before attempting to connect via WMI.

    Monday, August 22, 2016 12:36 PM
  • Can you advise how I can incorporate this into my script?
    Monday, August 22, 2016 4:06 PM
  • Get-Content .\pcList.txt | ForEach-Object {
    
        If (Test-Connection -ComputerName $_ -Count 1 -Quiet) {
    
            # PC is alive
    
        } Else {
    
            # PC isn't alive
    
        }
    
    }


    Monday, August 22, 2016 4:08 PM