Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you do a </p> <pre><code>trace-command parameterbinding {"Test" | A-Function test -User test -Password test} -pshost </code></pre> <p>you will see that $credential, since it is not being passed as argument to the function, it is bound with the default value each time during <code>process</code>. Path, on the other hand, is bound only once since you pass it to the function and hence the change that you do in begin is available in process.</p> <p>This is definitely a bug / something not really optimal, as this is not reproduced in Powershell v3. In v3, you get the desired output of credential being test rather than anonymous.</p> <p>You can fix this for now, of course, by using a local variable in begin or scoping the existing ones with <code>$script:Path</code> etc:</p> <pre><code>function A-Function{ [CmdletBinding(SupportsShouldProcess=$True)] param ( [Parameter(Position=0, HelpMessage="A Test string", Mandatory=$true)] [string]$Path, [Parameter(Position=1, HelpMessage="The list of file names to download.", ValueFromPipeline=$True)] [string[]]$testVar, [Parameter(HelpMessage= "The username")] [string]$User, [Parameter(HelpMessage= "The password")] [string]$Password, [Parameter(HelpMessage= "The credentials used.")] [Net.NetworkCredential]$Credential = (New-Object Net.NetworkCredential("Anonymous", "")) ) Begin { $script:Path = "TEST_" + $Path if ($User) { if ($Password) { $script:Credential = New-Object Net.NetworkCredential($User, $Password) } } } Process { $script:Path $script:Credential } End { } } "Test" | A-Function test -User test -Password test </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload