Try/Catch with Get-WmiObject

Welcome Forums General PowerShell Q&A Try/Catch with Get-WmiObject

This topic contains 9 replies, has 4 voices, and was last updated by

 
Participant
4 months ago.

  • Author
    Posts
  • #130778

    Participant
    Topics: 1
    Replies: 2
    Points: 41
    Rank: Member

    I have a very simple piece of code, It has to be simple as I am a beginner. I am trying to catch some errors coming out of Get-WmiObject. The idea being, that if I receive an error (presumably the $machine is not accessible) I wish to put the $machine in an array and print at the end,

    
    
    $errs;
    foreach($macine in get-content ./SERVS.TXT) {
    Try {
    $hrdsvrs = Get-WmiObject -class Win32_processor -computername $macine | ft systemname,Name,DeviceID,NumberOfCores,NumberOfLogicalProcessor;
    }
    Catch {
    $errs.add($macine)
    } # Write-Output $hrdsvrs;
    # Write-Output "\nDone!";
    foreach ($svr in $hrdsvrs) {
    Write-Output $svr;
    }
    #Set-Clipboard -Append -value $reply
    #Write-Output $reply;
    }
    if ($errs.Count -gt 0) {
    Write-Output "\n\n ERRORS\n"
    foreach ($err in $errs) {
    Write-Output "Unable to connect to: $machine";
    }
    }
    Sorry for the sloppy code, it doesn't want to stay formatted.
  • #131087

    Keymaster
    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    So... what's the problem?

  • #131106

    Participant
    Topics: 1
    Replies: 2
    Points: 41
    Rank: Member

    Although there are errors based on masculine in the text file, those machines are unreachable, still as I debug, the errors are not caught.

  • #131109

    Keymaster
    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    I'm not sure what “masculine “ means here, but the problem is you haven't enabled error trapping. I suggest reading “the big book of PowerShell error handling,” which is free on our ebooks page.

  • #131111

    Participant
    Topics: 4
    Replies: 76
    Points: 328
    Helping Hand
    Rank: Contributor

    You can only catch terminating errors. Try setting "-ErrorAction" to Stop

    Try {
    
    $hrdsvrs = Get-WmiObject -class Win32_processor -computername $macine -ErrorAction Stop | ft systemname,Name,DeviceID,NumberOfCores,NumberOfLogicalProcessor;
    }
    
    Catch {
    
    $errs.add($macine)
    
    } # Write-Output $hrdsvrs;
    

    Notice the difference in output between these two commands

    @('Realcomputer1','fakecomputer','Realcomputer1') | %{try{Get-WmiObject -Class Win32_OperatingSystem -ComputerName $PSItem}catch{'Something Happened'}}
    

    vs.

    @('Realcomputer1','fakecomputer','Realcomputer1') | %{try{Get-WmiObject -Class Win32_OperatingSystem -ComputerName $PSItem -ErrorAction Stop}catch{'Something Happened'}}
    
  • #131112

    Participant
    Topics: 1
    Replies: 2
    Points: 41
    Rank: Member

    I HATE SPELL CHECKER! yeah, that was supposed to say machines.

  • #131114

    Participant
    Topics: 4
    Replies: 76
    Points: 328
    Helping Hand
    Rank: Contributor

    forgive my typo!! (typed the same hostname twice in my example)

    @('Realcomputer','fakecomputer','Realcomputer1') | %{try{Get-WmiObject -Class Win32_OperatingSystem -ComputerName $PSItem}catch{'Something Happened'}}
    

    vs.

    @('Realcomputer','fakecomputer','Realcomputer1') | %{try{Get-WmiObject -Class Win32_OperatingSystem -ComputerName $PSItem -ErrorAction Stop}catch{'Something Happened'}}
    
  • #131147

    Participant
    Topics: 2
    Replies: 876
    Points: 1,354
    Helping Hand
    Rank: Community Hero

    Try I this way...

    Clear-Host
    
    $errs.ForEach{$errs.Clear()}
    
    $Servers = "$env:COMPUTERNAME",'Lab01','localhost','Lab02','127.0.0.1'
    
    $errs = $null
    $errs = @{}
    
    foreach($machine in $Servers) 
    {
        Try 
        {
            Get-WmiObject -class Win32_processor -computername $machine -ErrorAction Stop | 
            ft systemname,Name,DeviceID,NumberOfCores,NumberOfLogicalProcessor
        }
        Catch 
        { $errs.add("$machine",'Connection Failed') }
    }
    
    
    if ($errs.Count -gt 0) 
    {
        Write-Warning -Message 'ERRORS'
        $errs
    }
    
  • #131148

    Keymaster
    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    LOL SilentlyContinue means “do not trap errors.” Use Stop. And please, read the very concise ebook I referenced above. It would have made all that easier for you! That's why Dave wrote it!

  • #131171

    Participant
    Topics: 2
    Replies: 876
    Points: 1,354
    Helping Hand
    Rank: Community Hero

    Hey DonJ, I get that (I have read every book you have on this site – and regular recommend them to other – and more of course), and that is what I had, but I posted a previous version. It was late when I did this and I'm very old, so, half-timers kicks in and stuff happens. So, corrected.

The topic ‘Try/Catch with Get-WmiObject’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort