How to work with errors in RestrictedLanguage mode of remote session?

Welcome Forums General PowerShell Q&A How to work with errors in RestrictedLanguage mode of remote session?

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

GS
 
Participant
2 years, 10 months ago.

  • Author
    Posts
  • #38178
    GS

    Participant
    Points: 12
    Rank: Member

    Hello,

    I have a module which is imported into WinRM session which is in RestrictedLanguage mode. I need to get ErrorOutput stream from following command

    $output = Receive-job * -ErrorVariable RemoteErr
    

    This does not work since in RestrictedLanguage mode this variable is not allowed and fails with error below

    A variable that cannot be referenced in restricted language mode or a Data section is being referenced. Variables that can be referenced include the following: $PSCulture, $PSUICulture, $true, 
    $false, and  $null.
    

    Short of switching Remote Session configuration to ConstrainedLanguage what are my options?

  • #38301

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    You don't have any. What it's doing is exactly what it's supposed to do, and it's kinda not designed to provide a workaround.

  • #38303
    GS

    Participant
    Points: 12
    Rank: Member

    Well I need to get all the errors which happened on remote jobs, be it on job itself (like it was unable to connect to remote computer or something happened within job itself while it was running and -ErrorVariable provided handy access to this.
    Job istself is like this

    $job = Invoke-Command -ComputerName $computerNames -ScriptBlock $scriptBlock -AsJob 

    Now to use JEA in the way it was intended (actually in NoLanguage mode) I have to figure out how to properly let function know that there were exceptions down the line. It seems to be workaround is below but I don't think it's very good, what are suggestions?

    foreach ($childJob in $job.ChildJobs) {
    			if ($childJob.State -eq "Failed") {
    			$script:Globalerror += @{ Name = $childJob.Location; Error = $childjob.JobStateInfo.Reason}
    			}
    			elseif ($childJob.Error -ne $null) {
    			$script:Globalerror += @{ Name = $childJob.Location; Error = $childjob.Error}
    			} 
    		}
    
    

The topic ‘How to work with errors in RestrictedLanguage mode of remote session?’ is closed to new replies.

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