Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to modify function argument in Powershell
    primarykey
    data
    text
    <p>I have a function that is similar to this:</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 { $Path = "TEST_" + $Path if ($User) { if ($Password) { $Credential = New-Object Net.NetworkCredential($User, $Password) } } } Process { $Path $Credential } End { } </code></pre> <p>}</p> <p>If I run "A-Function test -User test -Password -test", I get the output :</p> <p>TEST_test</p> <p>UserName Password SecurePassword Domain<br> -------- -------- -------------- ------<br> test test System.Security.SecureString </p> <p>This is what I expected the output to be. However, if I run this command instead:</p> <blockquote> <p>"Test" | A-Function test -User test -Password -test</p> </blockquote> <p>I get this output instead:</p> <p>TEST_test</p> <p>UserName Password SecurePassword Domain<br> -------- -------- -------------- ------<br> Anonymous System.Security.SecureString</p> <p>In other words, in the second scenario, it hasn't changed the value of the Credential argument in the Begin section, but in the first, it has. I don't understand why this is, can someone explain it?</p> <p>Regards</p>
    singulars
    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.
 

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