How can I pass a local variable to a script block executed on a remote machine with Invoke-Command?
up vote
7
down vote
favorite
I'm trying to retrieve the Filehash of a file, located in remote server using Invoke-Command
. I'm running my scripts on powershell version 4. It works fine, when i give the full path as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:testtestfile.zip -Algorithm SHA1
}
The above command works. But I need to pass the file name via a variable as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:test$dest.zip -Algorithm SHA1
}
I'm new to scripting and powershell. Please, help me with resolving this!
powershell powershell-remoting powershell-v4.0
add a comment |
up vote
7
down vote
favorite
I'm trying to retrieve the Filehash of a file, located in remote server using Invoke-Command
. I'm running my scripts on powershell version 4. It works fine, when i give the full path as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:testtestfile.zip -Algorithm SHA1
}
The above command works. But I need to pass the file name via a variable as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:test$dest.zip -Algorithm SHA1
}
I'm new to scripting and powershell. Please, help me with resolving this!
powershell powershell-remoting powershell-v4.0
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I'm trying to retrieve the Filehash of a file, located in remote server using Invoke-Command
. I'm running my scripts on powershell version 4. It works fine, when i give the full path as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:testtestfile.zip -Algorithm SHA1
}
The above command works. But I need to pass the file name via a variable as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:test$dest.zip -Algorithm SHA1
}
I'm new to scripting and powershell. Please, help me with resolving this!
powershell powershell-remoting powershell-v4.0
I'm trying to retrieve the Filehash of a file, located in remote server using Invoke-Command
. I'm running my scripts on powershell version 4. It works fine, when i give the full path as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:testtestfile.zip -Algorithm SHA1
}
The above command works. But I need to pass the file name via a variable as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:test$dest.zip -Algorithm SHA1
}
I'm new to scripting and powershell. Please, help me with resolving this!
powershell powershell-remoting powershell-v4.0
powershell powershell-remoting powershell-v4.0
edited Apr 15 '16 at 15:45
mklement0
122k20234265
122k20234265
asked Feb 18 '16 at 21:08
Bose
4817
4817
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
13
down vote
accepted
In PowerShell 4 (3+ actually) the easiest way is to use the Using
scope modifier:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
}
For a solution that works with all versions:
Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest)
Get-FileHash E:test$myDest.zip -Algorithm SHA1
} -ArgumentList $dest
add a comment |
up vote
6
down vote
To complement briantist's helpful answer:
The script block passed to Invoke-Command
is (as intended) executed on the remote machine, using the remote machine's variables by default.
Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest
):
PS v3+ offers the
using:
scope modifier for direct use of a local variable inside the script block - see briantist's first command.
- Note that
using:
only works whenInvoke-Command
actually targets a remote machine.
- Note that
The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.
For more information, refer to Get-Help about_Remote_Variables
or the docs online.
1
Thanks, I would usually add this stuff and didn't have time; +1
– briantist
Feb 18 '16 at 21:35
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
accepted
In PowerShell 4 (3+ actually) the easiest way is to use the Using
scope modifier:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
}
For a solution that works with all versions:
Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest)
Get-FileHash E:test$myDest.zip -Algorithm SHA1
} -ArgumentList $dest
add a comment |
up vote
13
down vote
accepted
In PowerShell 4 (3+ actually) the easiest way is to use the Using
scope modifier:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
}
For a solution that works with all versions:
Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest)
Get-FileHash E:test$myDest.zip -Algorithm SHA1
} -ArgumentList $dest
add a comment |
up vote
13
down vote
accepted
up vote
13
down vote
accepted
In PowerShell 4 (3+ actually) the easiest way is to use the Using
scope modifier:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
}
For a solution that works with all versions:
Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest)
Get-FileHash E:test$myDest.zip -Algorithm SHA1
} -ArgumentList $dest
In PowerShell 4 (3+ actually) the easiest way is to use the Using
scope modifier:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
}
For a solution that works with all versions:
Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest)
Get-FileHash E:test$myDest.zip -Algorithm SHA1
} -ArgumentList $dest
answered Feb 18 '16 at 21:11
briantist
30.3k34271
30.3k34271
add a comment |
add a comment |
up vote
6
down vote
To complement briantist's helpful answer:
The script block passed to Invoke-Command
is (as intended) executed on the remote machine, using the remote machine's variables by default.
Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest
):
PS v3+ offers the
using:
scope modifier for direct use of a local variable inside the script block - see briantist's first command.
- Note that
using:
only works whenInvoke-Command
actually targets a remote machine.
- Note that
The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.
For more information, refer to Get-Help about_Remote_Variables
or the docs online.
1
Thanks, I would usually add this stuff and didn't have time; +1
– briantist
Feb 18 '16 at 21:35
add a comment |
up vote
6
down vote
To complement briantist's helpful answer:
The script block passed to Invoke-Command
is (as intended) executed on the remote machine, using the remote machine's variables by default.
Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest
):
PS v3+ offers the
using:
scope modifier for direct use of a local variable inside the script block - see briantist's first command.
- Note that
using:
only works whenInvoke-Command
actually targets a remote machine.
- Note that
The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.
For more information, refer to Get-Help about_Remote_Variables
or the docs online.
1
Thanks, I would usually add this stuff and didn't have time; +1
– briantist
Feb 18 '16 at 21:35
add a comment |
up vote
6
down vote
up vote
6
down vote
To complement briantist's helpful answer:
The script block passed to Invoke-Command
is (as intended) executed on the remote machine, using the remote machine's variables by default.
Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest
):
PS v3+ offers the
using:
scope modifier for direct use of a local variable inside the script block - see briantist's first command.
- Note that
using:
only works whenInvoke-Command
actually targets a remote machine.
- Note that
The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.
For more information, refer to Get-Help about_Remote_Variables
or the docs online.
To complement briantist's helpful answer:
The script block passed to Invoke-Command
is (as intended) executed on the remote machine, using the remote machine's variables by default.
Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest
):
PS v3+ offers the
using:
scope modifier for direct use of a local variable inside the script block - see briantist's first command.
- Note that
using:
only works whenInvoke-Command
actually targets a remote machine.
- Note that
The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.
For more information, refer to Get-Help about_Remote_Variables
or the docs online.
edited Jul 11 '17 at 17:57
answered Feb 18 '16 at 21:19
mklement0
122k20234265
122k20234265
1
Thanks, I would usually add this stuff and didn't have time; +1
– briantist
Feb 18 '16 at 21:35
add a comment |
1
Thanks, I would usually add this stuff and didn't have time; +1
– briantist
Feb 18 '16 at 21:35
1
1
Thanks, I would usually add this stuff and didn't have time; +1
– briantist
Feb 18 '16 at 21:35
Thanks, I would usually add this stuff and didn't have time; +1
– briantist
Feb 18 '16 at 21:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35492437%2fhow-can-i-pass-a-local-variable-to-a-script-block-executed-on-a-remote-machine-w%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown