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

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of GS GS 7 months, 2 weeks ago.

  • Author
    Posts
  • #38178
    Profile photo of GS
    GS
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of GS
    GS
    Participant

    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}
    			} 
    		}
    
    

You must be logged in to reply to this topic.