1

I am running into a little issue, I have generated runbooks using powershell. One of them connects to Vmware using a build up similar to this

The code

TempFunction{
connects to vmware using VMuser1 credentials
runs code etc...
All is well
return $TempVariable
}
$TempVariable = Invoke-Command -ScriptBlock ${function:TempFunction} -ComputerName localhost

This all works fine.


The problem is that when I introduce a global array named InfoArray that stores information along the way and at the end wish to write it to a different server using other credentials it fails stating "Invoke-Command: The Using variable is not supported in the script function or filter"

Here is my introduced code:

#Append gathered log information to log
    $strScriptUser = "domain\FileUser"
    $strPass = "Password01"
    $PSS = ConvertTo-SecureString $strPass -AsPlainText -Force
    $cred = new-object system.management.automation.PSCredential $strScriptUser, $PSS
    Start-Job -ScriptBlock { Add-Content $using:Logfile $using:InfoArray } -Credential $cred
return $TempVariable
}

Any ideas as to how I could solve this issue? The following does not work

Start-Job -ScriptBlock { Add-Content $Logfile $InfoArray } -Credential $cred

Regards Akr

2 Answers 2

1

Pass them as arguments to this script block, since it doesn't support using.

Sample

Start-Job -ScriptBlock {
    param($logfile,$infoArray) 
    Add-Content $Logfile $InfoArray 
} -Credential $cred -ArgumentList @($logfile,$infoArray)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for you answer, I can confirm however that it does not trigger. When the sample you sent me is placed outside of a function it works, but when it is placed within a function, it fails.
By adding | wait-job | receive-job I receive the following error: "[localhost] An error occurred while starting the background process. Error reported: Access is denied." The user used to initiate the start-job -scriptblock has access to the file share where the add-content is performed.
It looks like this answered the question and now you have a new question? I would suggest marking this one as answered and asking a new question about why accessing the file share in the job is not working (which implies we have successfully passed the parameter) Additionally, I don't have enough of the script posted in this question to answer the next question at this point.
0

I attempted to Invoke without using -computername localhost, which resulted in it working via a PS script.

The challenge was that I cannot load PowerCLi pssnapin as a 64 bit directly in system center orchestrator, and must invoke for it to work.

Finally, what seemed to work was running the function using the server name rather than localhost.

$TempVariable = Invoke-Command -ScriptBlock ${function:TempFunction} -computerName *ServerName*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.