1

Let me brief out what all methods I tried.

Here is the Start_TestTalk.ps1 script

$RN = $env:RName $TestV = "Local_Variable" Write-Host $TestV Write-Host $RName Write-Host $RN Write-Host $env:RName

I've declared the below variables

$Name="myname" $CPU= 100

First Method:

PS C:\Users\Administrator> Invoke-Command -Session $s -Scriptblock{ & "C:\Users\Administrator\Desktop\Start_TestTalk.ps1 " -RName $args[0] -RCPU $args[1]}-argumentlist $Name,$CPU Local_Variable

Second Method

PS C:\Users\Administrator> Invoke-Command -Session $s -Scriptblock{ & "C:\Users\Administrator\Desktop\Start_TestTalk.ps1 " -RName $using:Name -RCPU $using:CPU} Local_Variable

Third Method

PS C:\Users\Administrator> Invoke-Command -Session $s -Scriptblock{ Param($Name, $CPU) & "C:\Users\Administrator\Desktop \Start_TestTalk.ps1" -RName $Name -RCPU $CPU}-argumentlist $Name ,$CPU Local_Variable

All the above three methods just prints the 'Local_Variable' which is local to the remote machine and doesn't print the variable that I pass from my local machine(here $Name).

5
  • What is the contents of Test.ps1? Commented Nov 10, 2016 at 12:07
  • @MathiasR.Jessen: Thanks, there's nothing much as of now for testing purposes, I'm just trying to print the variable I pass from source machine. Here is the simple code $RN = $env:RName $TestV = "Local_Variable" Write-Host $TestV Write-Host $RName Write-Host $RN Write-Host $env:RName Commented Nov 14, 2016 at 11:17
  • The start_testtalk.ps1 doesn't use parameters.... Commented Nov 15, 2016 at 8:20
  • @MartinBrandl: Sorry, I didn't get what you meant by 'start_testtalk.ps1 doesn't use parameters'. Commented Nov 16, 2016 at 5:50
  • You are passing parameters to Start-TestTalk.ps1 but the script is missing the parameter section e. g. param($Name, $CPU) Commented Nov 16, 2016 at 7:11

2 Answers 2

1

You can use the :using variable prefix:

Invoke-Command -Session $s -Scriptblock{ & "C:\Users\Administrator\Desktop\Test.ps1" -RName $using:Name -RCPU $using:CPU}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, It's still the same even with 'using' variable prefix PS > Invoke-Command -Session $s -Scriptblock{ & "C:\Users\Administrator\Desktop\Test.ps1 " -RName $using:Name -RCPU $using:CPU} Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. + CategoryInfo : InvalidData: (:) [Get-Process], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetProcessCommand
Then you probably use PowerShell 2.0? Try this: Invoke-Command -Session $s -Scriptblock{ Param($Name, $CPU) & "C:\Users\Administrator\Desktop\Test.ps1" -RName $Name -RCPU $CPU}-argumentlist $Name ,$CPU
I'm using the Powershell version 4 on both the local and remote machines, I tried the method you suggested as well but still the same. I've also updated the question with more specific details to avoid confusion. Thanks.
0

I figured out the way on how this feature works with the help of a Senior Architect here in our company. The variables sent from the local(source machine) are not actually made local on the remote machine and these just get dumped as values on the remote machine and not as variables(you can't use variables). A simple example on how the above script which I had mentioned works

Start_TestTalk.ps1 script

$RN = $args[0] $CPU= $args[1] Write-Host $RN Write-Host $CPU

Now use the same old Invoke Command with slight changes removing the variables earlier used to hold the values from local machine

Invoking the remote script using argument list

I've declared the below variables $Name="myname" $CPU= 100 PS C:\Users\Administrator\ Invoke-Command -Session $s -Scriptblock{ & "C:\Users\Administrator\Desktop\Start_TestTalk.ps1" $args[0] $args[1] }- argumentlist $Name,$CPU myname 100

Now you see that you get the required output on the remote machine, so it conveys that the values are directly getting dumped and not with the variables and hence I was earlier not able to use those variables on the remote machine.

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.