locked
Invoke-Command vs Get-WBJob RRS feed

  • Question

  • Hi guys,

    So I've been slamming my head against this wall all day. I've got 4 servers that for some unknown reason are using Windows Server Backups and I'm trying to pull the info out of them to create a CSV to feed into a monitoring program we use.

    One of the servers that I need to get info from is the one that the script is running on and it's pulling all the data out no problem.

    Then I start remotely checking the other servers and I'm missing data.

    ForEach ($computer in $computers) {
        $backup = New-Object PSObject
            $backup | Add-Member -NotePropertyName Computer -NotePropertyValue $null
            $backup | Add-Member -NotePropertyName WBSummary -NotePropertyValue $null
            $backup | Add-Member -NotePropertyName WBLastJob -NotePropertyValue $null
    
        if($computer -eq "localhost")
        {
    		$backup.WBSummary = Get-WBSummary
    		$backup.WBLastJob = Get-WBJob -Previous 1
                    $backup.Computer = $computer
        }
        else
        {	
    		# Create PSRemoting Connection
    		$PSSesh = New-PSSession -ComputerName $computer
    		
    		# Run Remote PowerShell Commands - 2008 requires snapin
            if($computer -eq "remotehost2008"){Invoke-Command -Session $PSSesh -ScriptBlock {Add-PSSnapin Windows.ServerBackup}}
    		$backup.WBSummary = Invoke-Command -Session $PSSesh -ScriptBlock {Get-WBSummary}
    		$backup.WBLastJob = Invoke-Command -Session $PSSesh -ScriptBlock {Get-WBJob -Previous 1}
                    $backup.Computer = $computer
    		
        	# Remove PSRemoting Connection
    	    Remove-PSSession -Session $PSSesh
    	}
    
    	$results += $backup
    }

    For $WBLastJob.JobItems for the local host, I get:

    Name             : VolumeList
    Type             : VolumeList
    State            : Completed
    HResult          : 0
    DetailedHResult  : 0
    ErrorDescription : 
    BytesProcessed   : 97463435264
    TotalBytes       : 97463435264
    CurrentItem      : 
    SubItemProcessed : 2
    SubItemFailed    : 0
    TotalSubItem     : 2
    SubItemList      : {System Reserved, C:}
    
    Name             : SystemState
    Type             : SystemState
    State            : Completed
    HResult          : 0
    DetailedHResult  : 0
    ErrorDescription : 
    BytesProcessed   : 0
    TotalBytes       : 0
    CurrentItem      : 
    SubItemProcessed : 0
    SubItemFailed    : 0
    TotalSubItem     : 0
    SubItemList      : 
    
    Name             : BareMetalRecovery
    Type             : BareMetalRecovery
    State            : Completed
    HResult          : 0
    DetailedHResult  : 0
    ErrorDescription : 
    BytesProcessed   : 0
    TotalBytes       : 0
    CurrentItem      : 
    SubItemProcessed : 2
    SubItemFailed    : 0
    TotalSubItem     : 2
    SubItemList      : 

    For the remote hosts:

    VolumeList
    SystemState
    BareMetalRecovery
    Is there any way of fixing this?


    (Names have been changed)

    • Moved by Bill_Stewart Friday, March 15, 2019 6:09 PM This is not "fix/debug/rewrite my script for me" forum
    Thursday, January 17, 2019 6:53 AM

All replies

  • Your code is quite convoluted and has many logic errors.

    $sb = {
        Add-PSSnapin Windows.ServerBackup
        [pscustomobject]@{
            Computer  = $env:COMPUTERNAME
            WBSummary = Get-WBSummary
            WBLastJob = Get-WBJob -Previous 1
        }
        
    }
    $results = foreach($computer in $computers) {
        
        if ($computer -eq 'localhost') {
            [pscustomobject]@{
                Computer = $computer
                WBSummary = Get-WBSummary
                WBLastJob = Get-WBJob -Previous 1
            }
            
        } else {
            Invoke-Command -ComputerName $computer -ScriptBlock $sb 
        }
        
    }


    \_(ツ)_/


    • Edited by jrv Thursday, January 17, 2019 7:47 AM
    Thursday, January 17, 2019 7:46 AM
  • Unfortunately, it's still doing the same thing.

    I know the code isn't pretty but I'm trying to at least get the data before I pretty it up and get it clean.

    I'm getting the drill down on the localhost Get-WBJob.JobItems, but not on the remote ones.

    Thursday, January 17, 2019 7:58 AM
  • The snap-in won't work in a remote session. 

    The test is to just run one call.

    Just run the following against one remote server.

    $sb = {
        Add-PSSnapin Windows.ServerBackup
        [pscustomobject]@{
            Computer  = $env:COMPUTERNAME
            WBSummary = Get-WBSummary
            WBLastJob = Get-WBJob -Previous 1
        }
        
    }
    Invoke-Command -ComputerName <remotepc> -ScriptBlock $sb


    \_(ツ)_/

    Thursday, January 17, 2019 8:02 AM
  • I did so, leaving out the session. I replaced my code with yours - same result
    Thursday, January 17, 2019 8:05 AM
  • I did so, leaving out the session. I replaced my code with yours - same result

    I don't want you to replace any code or change anything.  Just copy and paste my last code example at a prompt but use the correct remote computer name.

    I just tested it on a few systems and it works as expected


    \_(ツ)_/

    Thursday, January 17, 2019 8:35 AM
  • I ran it separately and I still get the JobItems but not the additional information that goes with them. :/
    Friday, January 18, 2019 12:35 AM
  • You have to expand the items.


    \_(ツ)_/

    Friday, January 18, 2019 12:37 AM
  • This works for me with no issues.

    PS U:\> $j = Invoke-Command -ComputerName sbs01 -ScriptBlock $sb
    PS U:\> $j.WBLastJob
    
    
    JobType          : Backup
    StartTime        : 12/18/2016 1:18 PM
    EndTime          : 12/18/2016 6:41 PM
    JobState         : Completed
    CurrentOperation :
    HResult          : 0
    DetailedHResult  : 0
    ErrorDescription :
    JobItems         : {VolumeList, BareMetalRecovery}
    VersionId        : 12/18/2016-18:18
    SuccessLogPath   :
    FailureLogPath   :
    
    
    
    PS U:\>
    
    


    \_(ツ)_/

    Friday, January 18, 2019 12:40 AM