{"id":3261,"date":"2009-12-29T12:57:56","date_gmt":"2009-12-29T12:57:56","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/12\/29\/how-to-pass-arguments-for-remote-commands\/"},"modified":"2019-02-18T13:06:03","modified_gmt":"2019-02-18T20:06:03","slug":"how-to-pass-arguments-for-remote-commands","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/how-to-pass-arguments-for-remote-commands\/","title":{"rendered":"How to pass arguments for remote commands"},"content":{"rendered":"<p>When invoking remote commands, one often wants to use local values as arguments to the remote command. This post explains how to do that. <\/p>\n<h3>Hardcoded values<\/h3>\n<p>Let&#8217;s start with a simple example. Below you can see how Get-Process is invoked in a remote session when the Id parameter is set to 0:<\/p>\n<pre>PS&gt; $s = New-PSSession -Computer localhost\nPS&gt; Invoke-Command -Session $s -Script { Get-Process <strong>-Id 0<\/strong> }\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName     PSComputerName\n-------  ------    -----      ----- -----   ------     -- -----------     --------------\n      0       0        0         24     0               <strong>0<\/strong> Idle            localhost<\/pre>\n<h3>Passing local values<\/h3>\n<p>What if we want to calculate the value of the Id parameter programmatically, instead of hard-coding it to a particular integer? Well, script blocks can declare parameters and the param block can be used to pass a value to the command that is being invoked. In the following example, we declare the $processId parameter in the script block and then use the Args parameter of Invoke-Command to set the value of $processId to $pid, which is the process ID of the current Windows PowerShell process.<\/p>\n<pre>PS&gt; $s = New-PSSession -ComputerName localhost\nPS&gt; Invoke-Command -Session $s -Script { <strong>param($processId)<\/strong> Get-Process <strong>-Id $processId<\/strong> } <strong>-Args $pid<\/strong>\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName     PSComputerName\n-------  ------    -----      ----- -----   ------     -- -----------     --------------\n    533      13    46160      37456   198     6.07   <strong>9488<\/strong> <strong>powershell<\/strong>      localhost<\/pre>\n<p>The example above demonstrates that Get-Process was invoked with a process id of the local Windows PowerShell console.&#160; Invoke-Command takes whatever you pass in the Args parameter and binds it to script block&#8217;s parameters in the remote session. Note that above I simply passed in a value of a local $pid variable, but any local expression (not just a simple variable dereference) can be used instead.<\/p>\n<h3>Referring to remote variables<\/h3>\n<p>A different behavior takes place when you don&#8217;t declare script block parameters using the &quot;param&quot; keyword:<\/p>\n<pre>PS&gt; $s = New-PSSession -ComputerName localhost\nPS&gt; Invoke-Command -Session $s -Script { Get-Process <strong>-Id $pid<\/strong> }\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     <strong>Id<\/strong> ProcessName     PSComputerName\n-------  ------    -----      ----- -----   ------     -- -----------     --------------\n    329      10    21216      33524   154     7.63   <strong>6596 wsmprovhost<\/strong>     localhost<\/pre>\n<p>In this case $pid is an unbound variable and Windows PowerShell assumes that it refers to the $pid variable in the remote session. Therefore, as you can see Get-Process returns the process that hosts the remote session. <\/p>\n<h3>Magic $args variable<\/h3>\n<p>What happens when you use the Args parameter of Invoke-Command cmdlet, but don&#8217;t declare any parameters in the script block? Local values are still passed to the remote session and in this case are bound to the <a href=\"http:\/\/blogs.msdn.com\/powershell\/attachment\/1525634.ashx\">$args variable<\/a>. This can save some typing:<\/p>\n<pre>PS&gt; $s = New-PSSession -ComputerName localhost\nPS&gt; Invoke-Command -Session $s -Script { Get-Process <strong>-Id $args[0]<\/strong> } <strong>-Args $pid<\/strong>\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                    PSComputerName\n-------  ------    -----      ----- -----   ------     -- -----------                    --------------\n    808      46    76840      81876   596     5.05   7824 powershell                     localhost<\/pre>\n<p>Thanks,<\/p>\n<p>Lukasz Anforowicz [MSFT]\n  <br \/>Windows PowerShell Developer\n  <br \/>Microsoft Corporation<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When invoking remote commands, one often wants to use local values as arguments to the remote command. This post explains how to do that. Hardcoded values Let&#8217;s start with a simple example. Below you can see how Get-Process is invoked in a remote session when the Id parameter is set to 0: PS&gt; $s = [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[8],"class_list":["post-3261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-remoting"],"acf":[],"blog_post_summary":"<p>When invoking remote commands, one often wants to use local values as arguments to the remote command. This post explains how to do that. Hardcoded values Let&#8217;s start with a simple example. Below you can see how Get-Process is invoked in a remote session when the Id parameter is set to 0: PS&gt; $s = [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3261"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3261\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}