locked
Get-wbsummary FOReach RRS feed

  • Question

  • Hi, please help! i'm sooo close!
    i run a FOREACH on the function below against 4 servers.  if one of the servers is offline i only get 3 results back, how do i make it spit out a row indicating 'Command Failed' or 'Server unavailable' for the 4th server? 

    what i get now is an html table with:
    PSComputerName  LastBackupTime           LastBackupResultDetailedHR
    server1                 6/18/2014 1:30:05 AM      0
    server2                 6/18/2014 1:00:14 AM      0
    server4                 6/17/2014 9:30:31 PM      0

    And what i want is an html table with:

    PSComputerName  LastBackupTime           LastBackupResultDetailedHR
    server1                 6/18/2014 1:30:05 AM      0
    server2                 6/18/2014 1:00:14 AM      0
    server3                 NO RESPONSE or Command Failed
    server4                 6/17/2014 9:30:31 PM      0

    _____________________________________________________________________
    function Get-WBStats

    {
    BEGIN {}
    Process
    {
    Invoke-Command -ScriptBlock {
                                    get-wbsummary
                                } -computername $Servername -credential $Cred | select PSComputerName,LastBackupTime,LastbackupResultDetailedHR | sort pscomputername                           
    }
    END {}
    }

    Thank you, any help is appreciated.

    MN

    Wednesday, June 18, 2014 10:57 PM

Answers

  • You can do that like this:

    if($result=Invoke-Command -ScriptBlock {get-wbsummary} -computername $Servername  -Ea 0){
         $result
    }else{
         $props=@{
              PSComputerName=$servername
              LastBackupTime=$null
              LastBackupResultDetailedHR=$null
         }
         New-Object PsObject -Property $props
    }


    ¯\_(ツ)_/¯

    • Marked as answer by MNone Thursday, June 19, 2014 4:58 PM
    Wednesday, June 18, 2014 11:14 PM

All replies

  • You can do that like this:

    if($result=Invoke-Command -ScriptBlock {get-wbsummary} -computername $Servername  -Ea 0){
         $result
    }else{
         $props=@{
              PSComputerName=$servername
              LastBackupTime=$null
              LastBackupResultDetailedHR=$null
         }
         New-Object PsObject -Property $props
    }


    ¯\_(ツ)_/¯

    • Marked as answer by MNone Thursday, June 19, 2014 4:58 PM
    Wednesday, June 18, 2014 11:14 PM
  • awesome i believe that worked.  i will test it more in the morning.  thank you very very much.

    MN

    Thursday, June 19, 2014 3:21 AM
  • That is generally how to build a parallel object structure.  As long as the objects have the same topology they will collect together and be reportable.  You can also add a status field to both.  One is defaulted to something like 'online' and the other to 'offline' or 'failed.  I prefer Booleans and a field called IsAlive set to $true or $false.  This can be more useful in aggregate reporting.


    ¯\_(ツ)_/¯

    Thursday, June 19, 2014 3:32 AM
  • it's all working like a charm.  i simply replaced my function with yours and it is good. 

    thanks again

    MN

    Thursday, June 19, 2014 4:59 PM
  • i'm new to powershell and haven't used Booleans yet. i understand the basics, but haven't used them.  if it's small/short paste me an example of what you mean by your comments above regarding 'IsAlive'.  :)

    MN

    Thursday, June 19, 2014 5:01 PM
  • With this method one size fits all.

    $props=@{
    PSComputerName=$servername LastBackupTime=$null LastBackupResultDetailedHR=$null IsAlive=$false } if($result=Invoke-Command -ScriptBlock {get-wbsummary} -computername $Servername -Ea 0){ $props.LastBackupTime=$result.LastBackupTime $props.LastBackupResultDetailHR=$result.LastBackupResultDetailedHR $prop.IsAlive=$true } New-Object PsObject -Property $props


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, June 19, 2014 5:12 PM
    Thursday, June 19, 2014 5:12 PM
  • not sure if you're still reading this but i ran into a small glitch.  when i use my Function it comes out like this:

    Computername -  Last Backup Date - Result

    when i use the IF/ELSE it comes out all mixed up order, and i don't know why.  i'm stumped.

    MN

    Sunday, June 22, 2014 1:15 AM
  • I think you are posting on the wrong thread. This one is closed and it is not about any function.  Please check the thread and post in the correct one or start a new question.

    ¯\_(ツ)_/¯

    Sunday, June 22, 2014 1:21 AM