1

I have a trouble.. Can someone help me? Here is my Code :

$A = '123'
$servers = 'computer1'
$Properties = [ordered]@{A = $A
                          servers = $servers     
                        }

$MyObject = New-Object -TypeName PSObject -Property $Properties

$MyObjec
...

$result = Invoke-Command -ComputerName Machine -UseSSL -InDisconnectedSession -ScriptBlock {
$MyObject.A
$MyObject.servers
$env:computername
}`
–InputObject $MyObjec -port 5986 -ConfigurationName myEndpoint -SessionOption @{OutputBufferingMode="Drop"} -Credential $credential | Receive-PSSession

$result

Question: Why the $result doesn't show anything about $MyObject?

It only show $MyObjec (not in the invoke-command) and $env:computername (in the invoke-command)

How can I fix it?

P.S this is really what I want to make, I want to get into multiple machine which in 6 different AD in the same time, but they should use different username, and I need $A in the remote machine to deal another thing.

$A = '123'
    $servers = 'computer1'
    $Properties = [ordered]@{A = $A
                              servers = $servers     
                            }

    $MyObject = New-Object -TypeName PSObject -Property $Properties

    $MyObjec
    ...

#Add
$servers@{'Machine1','Machine2','Machine3'}

Foreach ($servers in $servers) {

Star-Job {

$username = $servers+'account'

$password = $password

$credential = ....($username,$password)

    $result = Invoke-Command -ComputerName $servers -UseSSL -InDisconnectedSession -ScriptBlock {
    $MyObject.A
    $MyObject.servers
    $env:computername
    }`
    –InputObject $MyObjec -port 5986 -ConfigurationName myEndpoint -SessionOption @{OutputBufferingMode="Drop"} -Credential $credential | Receive-PSSession

    $result
}

}

I will try -Argument-List and param{} Beacase I try Start-Job with -Argument-List and $Using, there have an error. Thank u for your reply!

3
  • 1
    It's unclear to me what you want to achieve with your script. But I would always use $MyObject with a t at the end. Commented Nov 21, 2018 at 8:00
  • Thank you,In fact,my achievement is to run the invoke-command parallel in the same time.I edit my code clean! Commented Nov 21, 2018 at 11:11
  • today I test that, It can work! Thank you! Commented Nov 22, 2018 at 8:40

1 Answer 1

2

Because the part within -Scriptblock { ... } gets executed on the remote system and has therefore no access to the variables on the local system (different scope).

You can change that by passing the variables to the remote system using the -Argument-List parameter like this:

Invoke-Command -ComputerName Machine -ArgumentList $MyObject -ScriptBlock { 
    param($MyObject)

    $MyObject.A
}

Or use $using: to get access to the locally defined variables like that:

Invoke-Command -ComputerName Machine -ScriptBlock { 

    $using:MyObject.A
}
Sign up to request clarification or add additional context in comments.

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.