<<< Back to Search

Scripting ::: Remote PowerShell Execution with PSSession - returning remote information - Function

This function allow you execute remotely commands that not have parameters for remote execution: -computerName / -cimSession

  • If the command has those parameters available execute using them instead of PSSession.

Core Snippet & Sample

Copy text below into a power shell session and execute to see the code in action.

# Scripting - Remote PowerShell Execution with PSSession - Capturing remote information - Function
# Core Snippet &amp; Sample
# www.PowerShellExamples.com

# Script to run in a remote computer using PSSession
#     returning info from remote execution
#     in case of a non-controlled exception script will return error details encapsulated in a result object

function Invoke-PSSesionRemote($computerName){

    $resultObj = New-Object -TypeName PSObject
    $resultObj | Add-Member -MemberType NoteProperty -Name TargetElement -Value $ComputerName -PassThru |
    Add-Member -MemberType NoteProperty -Name Return -Value $null -PassThru |
    Add-Member -MemberType NoteProperty -Name ExitCode -Value "" -PassThru |
    Add-Member -MemberType NoteProperty -Name ExitMessage -Value ""

    try{
        $s = New-PSSession -computerName $computerName # To use Custom Credential pass it here: -credential $credObj.Credential
        $invokeResult = Invoke-Command -Session $s -Scriptblock {

            $resultObjL = New-Object -TypeName PSObject

            $resultObjL |
            Add-Member -MemberType NoteProperty -Name Return -Value $null -PassThru |
            Add-Member -MemberType NoteProperty -Name ExitCode -Value "" -PassThru |
            Add-Member -MemberType NoteProperty -Name ExitMessage -Value ""

            $resultObjL.ExitCode = 0
            try{
                # TODO Update this section with commands to execute remotely
                # On this sample a list of 2 services on remote machine named C* is returned
                $x = @()
                $x += get-service -name "*" | select-object -last 2

                $resultObjL.Return = $x
            }catch{
                $resultObjL.ExitCode = 2
                $resultObjL.ExitMessage = "Error. NonControled exception.`r`n $_"
            }
            return $resultObjL
        }
        Remove-PSSession $s

        $resultObj.Return =  $invokeResult.Return
        $resultObj.ExitCode = $invokeResult.ExitCode
        $resultObj.ExitMessage = $invokeResult.ExitMessage
        #$resultObj.Return | out-string | write-host
    }catch{
        $resultObj.ExitCode = 2
        $resultObj.ExitMessage = "Error. NonControled exception.`r`n $_"
    }
    return $resultObj
}


$resultObj = Invoke-PSSesionRemote -Computername "ComputerNameXXX"

Write-Host "Result Object:" -ForegroundColor Magenta
$resultObj | out-string | Write-Host

Write-Host "Returned Information:" -ForegroundColor Magenta
$resultObj.Return | out-string | Write-Host






Result

PS C:\>

Result Object:

TargetElement    Return              ExitCode ExitMessage
-------------    ------              -------- -----------
CompunerNameXXX  {wuauserv, wudfsvc}        0



Returned Information:

Status   Name               DisplayName
------   ----               -----------
Stopped  wuauserv           Windows Update
Stopped  wudfsvc            Windows Driver Foundation - User-mo...