Why do I get different output when entering the same string in a prompt vs passing it as a variable? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Why won't this powershell script accept parameters?Invoke powershell cmdlet dynamically with object parameter valuespowershell script reading parameters from txtPowershell execution in OctopusPassing string $variable to invoke-command scriptblock parameter -namepowershell customize param promptHow to keep string parameter as null when not supplied when running scriptHow to get powershell to throw exception instead of block when missing parameters to cmdletConsume $args while also using parameter set namesExecute external command with single-hypen parameter with equals and period

How does the math work when buying airline miles?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Why wasn't DOSKEY integrated with COMMAND.COM?

What causes the direction of lightning flashes?

Is there such thing as an Availability Group failover trigger?

Closed form of recurrent arithmetic series summation

Is the Standard Deduction better than Itemized when both are the same amount?

Using et al. for a last / senior author rather than for a first author

If a VARCHAR(MAX) column is included in an index, is the entire value always stored in the index page(s)?

What font is "z" in "z-score"?

If my PI received research grants from a company to be able to pay my postdoc salary, did I have a potential conflict interest too?

Amount of permutations on an NxNxN Rubik's Cube

What are the out-of-universe reasons for the references to Toby Maguire-era Spider-Man in ITSV

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

How to compare two different files line by line in unix?

Is it cost-effective to upgrade an old-ish Giant Escape R3 commuter bike with entry-level branded parts (wheels, drivetrain)?

How come Sam didn't become Lord of Horn Hill?

old style "caution" boxes

Compare a given version number in the form major.minor.build.patch and see if one is less than the other

For a new assistant professor in CS, how to build/manage a publication pipeline

Why are there no cargo aircraft with "flying wing" design?

Can anything be seen from the center of the Boötes void? How dark would it be?

Do jazz musicians improvise on the parent scale in addition to the chord-scales?

Does classifying an integer as a discrete log require it be part of a multiplicative group?



Why do I get different output when entering the same string in a prompt vs passing it as a variable?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Why won't this powershell script accept parameters?Invoke powershell cmdlet dynamically with object parameter valuespowershell script reading parameters from txtPowershell execution in OctopusPassing string $variable to invoke-command scriptblock parameter -namepowershell customize param promptHow to keep string parameter as null when not supplied when running scriptHow to get powershell to throw exception instead of block when missing parameters to cmdletConsume $args while also using parameter set namesExecute external command with single-hypen parameter with equals and period



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Can someone explain to me why I am seeing different output when I enter the same string one entered when prompted by powershell and the other when I pass by a variable. I would like to know how I need to format the string when entering it in the prompt to produce the same output that I get when I pass it as a variable.



test.ps1



param (
[Parameter(Mandatory=$true)]
[String]
$NewArgumentString
)

$NewArguments = (Write-Output $NewArgumentString | ConvertFrom-StringData)
$NewArguments



Run:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> .test.ps1 -NewArgumentString $testString

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


PS> .test.ps1

cmdlet a.ps1 at command pipeline position 1
Supply values for the following parameters:
NewArgumentString: HOSTNAME = jburns-test`nIP = 127.0.0.1

Name Value
---- -----
HOSTNAME jburns-test`nIP = 127.0.0.1



enter image description here










share|improve this question
























  • my understanding is that - unless you provide a template - each key = value pair must be on a line of it's own. it looks like that escaped newline aint being expanded as such.

    – Lee_Dailey
    Mar 8 at 18:59












  • That is true but in my local testing I was able to achieve this with an escaped new line, I will edit the question to show that.

    – Buster
    Mar 8 at 19:00











  • how odd. i tested a stripped down function with your code and it works in both the ISE and the console. my environment is win7x64, ps5.1. ///// i recommend you post a sanitized example that shows the problem. ///// it works properly when i call it from a script, also.

    – Lee_Dailey
    Mar 8 at 19:09







  • 1





    Did you accidentally write "`n" as "``n"?

    – rokumaru
    Mar 8 at 19:40











  • or have you enclosed the string in single quotes?

    – rokumaru
    Mar 8 at 20:08


















1















Can someone explain to me why I am seeing different output when I enter the same string one entered when prompted by powershell and the other when I pass by a variable. I would like to know how I need to format the string when entering it in the prompt to produce the same output that I get when I pass it as a variable.



test.ps1



param (
[Parameter(Mandatory=$true)]
[String]
$NewArgumentString
)

$NewArguments = (Write-Output $NewArgumentString | ConvertFrom-StringData)
$NewArguments



Run:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> .test.ps1 -NewArgumentString $testString

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


PS> .test.ps1

cmdlet a.ps1 at command pipeline position 1
Supply values for the following parameters:
NewArgumentString: HOSTNAME = jburns-test`nIP = 127.0.0.1

Name Value
---- -----
HOSTNAME jburns-test`nIP = 127.0.0.1



enter image description here










share|improve this question
























  • my understanding is that - unless you provide a template - each key = value pair must be on a line of it's own. it looks like that escaped newline aint being expanded as such.

    – Lee_Dailey
    Mar 8 at 18:59












  • That is true but in my local testing I was able to achieve this with an escaped new line, I will edit the question to show that.

    – Buster
    Mar 8 at 19:00











  • how odd. i tested a stripped down function with your code and it works in both the ISE and the console. my environment is win7x64, ps5.1. ///// i recommend you post a sanitized example that shows the problem. ///// it works properly when i call it from a script, also.

    – Lee_Dailey
    Mar 8 at 19:09







  • 1





    Did you accidentally write "`n" as "``n"?

    – rokumaru
    Mar 8 at 19:40











  • or have you enclosed the string in single quotes?

    – rokumaru
    Mar 8 at 20:08














1












1








1








Can someone explain to me why I am seeing different output when I enter the same string one entered when prompted by powershell and the other when I pass by a variable. I would like to know how I need to format the string when entering it in the prompt to produce the same output that I get when I pass it as a variable.



test.ps1



param (
[Parameter(Mandatory=$true)]
[String]
$NewArgumentString
)

$NewArguments = (Write-Output $NewArgumentString | ConvertFrom-StringData)
$NewArguments



Run:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> .test.ps1 -NewArgumentString $testString

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


PS> .test.ps1

cmdlet a.ps1 at command pipeline position 1
Supply values for the following parameters:
NewArgumentString: HOSTNAME = jburns-test`nIP = 127.0.0.1

Name Value
---- -----
HOSTNAME jburns-test`nIP = 127.0.0.1



enter image description here










share|improve this question
















Can someone explain to me why I am seeing different output when I enter the same string one entered when prompted by powershell and the other when I pass by a variable. I would like to know how I need to format the string when entering it in the prompt to produce the same output that I get when I pass it as a variable.



test.ps1



param (
[Parameter(Mandatory=$true)]
[String]
$NewArgumentString
)

$NewArguments = (Write-Output $NewArgumentString | ConvertFrom-StringData)
$NewArguments



Run:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> .test.ps1 -NewArgumentString $testString

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


PS> .test.ps1

cmdlet a.ps1 at command pipeline position 1
Supply values for the following parameters:
NewArgumentString: HOSTNAME = jburns-test`nIP = 127.0.0.1

Name Value
---- -----
HOSTNAME jburns-test`nIP = 127.0.0.1



enter image description here







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 23:32









HAL9256

4,0831330




4,0831330










asked Mar 8 at 18:57









BusterBuster

362316




362316












  • my understanding is that - unless you provide a template - each key = value pair must be on a line of it's own. it looks like that escaped newline aint being expanded as such.

    – Lee_Dailey
    Mar 8 at 18:59












  • That is true but in my local testing I was able to achieve this with an escaped new line, I will edit the question to show that.

    – Buster
    Mar 8 at 19:00











  • how odd. i tested a stripped down function with your code and it works in both the ISE and the console. my environment is win7x64, ps5.1. ///// i recommend you post a sanitized example that shows the problem. ///// it works properly when i call it from a script, also.

    – Lee_Dailey
    Mar 8 at 19:09







  • 1





    Did you accidentally write "`n" as "``n"?

    – rokumaru
    Mar 8 at 19:40











  • or have you enclosed the string in single quotes?

    – rokumaru
    Mar 8 at 20:08


















  • my understanding is that - unless you provide a template - each key = value pair must be on a line of it's own. it looks like that escaped newline aint being expanded as such.

    – Lee_Dailey
    Mar 8 at 18:59












  • That is true but in my local testing I was able to achieve this with an escaped new line, I will edit the question to show that.

    – Buster
    Mar 8 at 19:00











  • how odd. i tested a stripped down function with your code and it works in both the ISE and the console. my environment is win7x64, ps5.1. ///// i recommend you post a sanitized example that shows the problem. ///// it works properly when i call it from a script, also.

    – Lee_Dailey
    Mar 8 at 19:09







  • 1





    Did you accidentally write "`n" as "``n"?

    – rokumaru
    Mar 8 at 19:40











  • or have you enclosed the string in single quotes?

    – rokumaru
    Mar 8 at 20:08

















my understanding is that - unless you provide a template - each key = value pair must be on a line of it's own. it looks like that escaped newline aint being expanded as such.

– Lee_Dailey
Mar 8 at 18:59






my understanding is that - unless you provide a template - each key = value pair must be on a line of it's own. it looks like that escaped newline aint being expanded as such.

– Lee_Dailey
Mar 8 at 18:59














That is true but in my local testing I was able to achieve this with an escaped new line, I will edit the question to show that.

– Buster
Mar 8 at 19:00





That is true but in my local testing I was able to achieve this with an escaped new line, I will edit the question to show that.

– Buster
Mar 8 at 19:00













how odd. i tested a stripped down function with your code and it works in both the ISE and the console. my environment is win7x64, ps5.1. ///// i recommend you post a sanitized example that shows the problem. ///// it works properly when i call it from a script, also.

– Lee_Dailey
Mar 8 at 19:09






how odd. i tested a stripped down function with your code and it works in both the ISE and the console. my environment is win7x64, ps5.1. ///// i recommend you post a sanitized example that shows the problem. ///// it works properly when i call it from a script, also.

– Lee_Dailey
Mar 8 at 19:09





1




1





Did you accidentally write "`n" as "``n"?

– rokumaru
Mar 8 at 19:40





Did you accidentally write "`n" as "``n"?

– rokumaru
Mar 8 at 19:40













or have you enclosed the string in single quotes?

– rokumaru
Mar 8 at 20:08






or have you enclosed the string in single quotes?

– rokumaru
Mar 8 at 20:08













1 Answer
1






active

oldest

votes


















1














The reason that this code responds differently to the prompt vs passing in a variable is because we are dealing with the difference between interpreted strings vs. literal strings. a.k.a. single quotes vs. double quotes.



Strings at the prompt are treated like literal strings, and not interpreted strings. That's why at the prompt you can't reference variables. To demonstrate the different effect this has with the example, let's run through it where we swap out the double quotes for single quotes in the passed variable, to emulate writing in the string at the prompt:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'

PS> .test.ps1 -NewArgumentString $testString

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


PS> .test.ps1 -NewArgumentString $testString2

Name Value
---- -----
HOSTNAME jburns-test`nIP = 127.0.0.1


Here we see confirmation that the literal single quoted string is interpreted the same as the prompt is. What Really is going on is that the two strings are not the same. For the first double quoted string, immediately after the assignment, PowerShell converts the two character combination "`n" (0x96 + 0x6E) into the single Line Feed "LF" (0x0A) character. We can see this by comparing the string lengths:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString.Length
37

PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'
PS> $testString2.Length
38


To truly replicate the results of our theory in the example, let's enter into the prompt the -same- "bitwise" string that is stored in the variable by replacing `n with the non printable LF character (only possible by using the ALT + numpad method) ALT + 0010



PS> .test.ps1

cmdlet a.ps1 at command pipeline position 1
Supply values for the following parameters:
NewArgumentString: HOSTNAME = jburns-test
IP = 127.0.0.1

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


And voila! We have replicated the results. PowerShell is not broken. Our faith in functions and parameter passing is restored!




As an aside, it's probably not recommended to pass strings into your functions and rely on the function to convert it into a hash table, as that can give, as you have demonstrated, unpredictable results. I would instead directly pass in a Hash Table (Type [hashtable]) right into the function:



testHashTable.ps1



param (
[Parameter(Mandatory=$true)]
[hashtable]
$MyHashTable
)

Write-Output $MyHashTable


PS> $testHashTable = @ "HOSTNAME" = "jburns-test" ; "IP" = "127.0.0.1" 
PS> .test.ps1 -MyHashTable $testHashTable

Name Value
---- -----
IP 127.0.0.1
HOSTNAME jburns-test





share|improve this answer























  • Thank you for the complete, clear response this makes sense. After talking with a coworker we came to the similar conclusion, thinking that a user would expect what they typed in a prompt to be treated literally and not accidentally interpolated. And yes I think passing by a hashtable is smarter I thought at the time that I would have to deal with a string from the caller side but there is another way.

    – Buster
    Mar 9 at 3:16











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069375%2fwhy-do-i-get-different-output-when-entering-the-same-string-in-a-prompt-vs-passi%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The reason that this code responds differently to the prompt vs passing in a variable is because we are dealing with the difference between interpreted strings vs. literal strings. a.k.a. single quotes vs. double quotes.



Strings at the prompt are treated like literal strings, and not interpreted strings. That's why at the prompt you can't reference variables. To demonstrate the different effect this has with the example, let's run through it where we swap out the double quotes for single quotes in the passed variable, to emulate writing in the string at the prompt:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'

PS> .test.ps1 -NewArgumentString $testString

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


PS> .test.ps1 -NewArgumentString $testString2

Name Value
---- -----
HOSTNAME jburns-test`nIP = 127.0.0.1


Here we see confirmation that the literal single quoted string is interpreted the same as the prompt is. What Really is going on is that the two strings are not the same. For the first double quoted string, immediately after the assignment, PowerShell converts the two character combination "`n" (0x96 + 0x6E) into the single Line Feed "LF" (0x0A) character. We can see this by comparing the string lengths:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString.Length
37

PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'
PS> $testString2.Length
38


To truly replicate the results of our theory in the example, let's enter into the prompt the -same- "bitwise" string that is stored in the variable by replacing `n with the non printable LF character (only possible by using the ALT + numpad method) ALT + 0010



PS> .test.ps1

cmdlet a.ps1 at command pipeline position 1
Supply values for the following parameters:
NewArgumentString: HOSTNAME = jburns-test
IP = 127.0.0.1

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


And voila! We have replicated the results. PowerShell is not broken. Our faith in functions and parameter passing is restored!




As an aside, it's probably not recommended to pass strings into your functions and rely on the function to convert it into a hash table, as that can give, as you have demonstrated, unpredictable results. I would instead directly pass in a Hash Table (Type [hashtable]) right into the function:



testHashTable.ps1



param (
[Parameter(Mandatory=$true)]
[hashtable]
$MyHashTable
)

Write-Output $MyHashTable


PS> $testHashTable = @ "HOSTNAME" = "jburns-test" ; "IP" = "127.0.0.1" 
PS> .test.ps1 -MyHashTable $testHashTable

Name Value
---- -----
IP 127.0.0.1
HOSTNAME jburns-test





share|improve this answer























  • Thank you for the complete, clear response this makes sense. After talking with a coworker we came to the similar conclusion, thinking that a user would expect what they typed in a prompt to be treated literally and not accidentally interpolated. And yes I think passing by a hashtable is smarter I thought at the time that I would have to deal with a string from the caller side but there is another way.

    – Buster
    Mar 9 at 3:16















1














The reason that this code responds differently to the prompt vs passing in a variable is because we are dealing with the difference between interpreted strings vs. literal strings. a.k.a. single quotes vs. double quotes.



Strings at the prompt are treated like literal strings, and not interpreted strings. That's why at the prompt you can't reference variables. To demonstrate the different effect this has with the example, let's run through it where we swap out the double quotes for single quotes in the passed variable, to emulate writing in the string at the prompt:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'

PS> .test.ps1 -NewArgumentString $testString

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


PS> .test.ps1 -NewArgumentString $testString2

Name Value
---- -----
HOSTNAME jburns-test`nIP = 127.0.0.1


Here we see confirmation that the literal single quoted string is interpreted the same as the prompt is. What Really is going on is that the two strings are not the same. For the first double quoted string, immediately after the assignment, PowerShell converts the two character combination "`n" (0x96 + 0x6E) into the single Line Feed "LF" (0x0A) character. We can see this by comparing the string lengths:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString.Length
37

PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'
PS> $testString2.Length
38


To truly replicate the results of our theory in the example, let's enter into the prompt the -same- "bitwise" string that is stored in the variable by replacing `n with the non printable LF character (only possible by using the ALT + numpad method) ALT + 0010



PS> .test.ps1

cmdlet a.ps1 at command pipeline position 1
Supply values for the following parameters:
NewArgumentString: HOSTNAME = jburns-test
IP = 127.0.0.1

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


And voila! We have replicated the results. PowerShell is not broken. Our faith in functions and parameter passing is restored!




As an aside, it's probably not recommended to pass strings into your functions and rely on the function to convert it into a hash table, as that can give, as you have demonstrated, unpredictable results. I would instead directly pass in a Hash Table (Type [hashtable]) right into the function:



testHashTable.ps1



param (
[Parameter(Mandatory=$true)]
[hashtable]
$MyHashTable
)

Write-Output $MyHashTable


PS> $testHashTable = @ "HOSTNAME" = "jburns-test" ; "IP" = "127.0.0.1" 
PS> .test.ps1 -MyHashTable $testHashTable

Name Value
---- -----
IP 127.0.0.1
HOSTNAME jburns-test





share|improve this answer























  • Thank you for the complete, clear response this makes sense. After talking with a coworker we came to the similar conclusion, thinking that a user would expect what they typed in a prompt to be treated literally and not accidentally interpolated. And yes I think passing by a hashtable is smarter I thought at the time that I would have to deal with a string from the caller side but there is another way.

    – Buster
    Mar 9 at 3:16













1












1








1







The reason that this code responds differently to the prompt vs passing in a variable is because we are dealing with the difference between interpreted strings vs. literal strings. a.k.a. single quotes vs. double quotes.



Strings at the prompt are treated like literal strings, and not interpreted strings. That's why at the prompt you can't reference variables. To demonstrate the different effect this has with the example, let's run through it where we swap out the double quotes for single quotes in the passed variable, to emulate writing in the string at the prompt:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'

PS> .test.ps1 -NewArgumentString $testString

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


PS> .test.ps1 -NewArgumentString $testString2

Name Value
---- -----
HOSTNAME jburns-test`nIP = 127.0.0.1


Here we see confirmation that the literal single quoted string is interpreted the same as the prompt is. What Really is going on is that the two strings are not the same. For the first double quoted string, immediately after the assignment, PowerShell converts the two character combination "`n" (0x96 + 0x6E) into the single Line Feed "LF" (0x0A) character. We can see this by comparing the string lengths:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString.Length
37

PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'
PS> $testString2.Length
38


To truly replicate the results of our theory in the example, let's enter into the prompt the -same- "bitwise" string that is stored in the variable by replacing `n with the non printable LF character (only possible by using the ALT + numpad method) ALT + 0010



PS> .test.ps1

cmdlet a.ps1 at command pipeline position 1
Supply values for the following parameters:
NewArgumentString: HOSTNAME = jburns-test
IP = 127.0.0.1

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


And voila! We have replicated the results. PowerShell is not broken. Our faith in functions and parameter passing is restored!




As an aside, it's probably not recommended to pass strings into your functions and rely on the function to convert it into a hash table, as that can give, as you have demonstrated, unpredictable results. I would instead directly pass in a Hash Table (Type [hashtable]) right into the function:



testHashTable.ps1



param (
[Parameter(Mandatory=$true)]
[hashtable]
$MyHashTable
)

Write-Output $MyHashTable


PS> $testHashTable = @ "HOSTNAME" = "jburns-test" ; "IP" = "127.0.0.1" 
PS> .test.ps1 -MyHashTable $testHashTable

Name Value
---- -----
IP 127.0.0.1
HOSTNAME jburns-test





share|improve this answer













The reason that this code responds differently to the prompt vs passing in a variable is because we are dealing with the difference between interpreted strings vs. literal strings. a.k.a. single quotes vs. double quotes.



Strings at the prompt are treated like literal strings, and not interpreted strings. That's why at the prompt you can't reference variables. To demonstrate the different effect this has with the example, let's run through it where we swap out the double quotes for single quotes in the passed variable, to emulate writing in the string at the prompt:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'

PS> .test.ps1 -NewArgumentString $testString

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


PS> .test.ps1 -NewArgumentString $testString2

Name Value
---- -----
HOSTNAME jburns-test`nIP = 127.0.0.1


Here we see confirmation that the literal single quoted string is interpreted the same as the prompt is. What Really is going on is that the two strings are not the same. For the first double quoted string, immediately after the assignment, PowerShell converts the two character combination "`n" (0x96 + 0x6E) into the single Line Feed "LF" (0x0A) character. We can see this by comparing the string lengths:



PS> $testString = "HOSTNAME = jburns-test`nIP = 127.0.0.1"
PS> $testString.Length
37

PS> $testString2 = 'HOSTNAME = jburns-test`nIP = 127.0.0.1'
PS> $testString2.Length
38


To truly replicate the results of our theory in the example, let's enter into the prompt the -same- "bitwise" string that is stored in the variable by replacing `n with the non printable LF character (only possible by using the ALT + numpad method) ALT + 0010



PS> .test.ps1

cmdlet a.ps1 at command pipeline position 1
Supply values for the following parameters:
NewArgumentString: HOSTNAME = jburns-test
IP = 127.0.0.1

Name Value
---- -----
HOSTNAME jburns-test
IP 127.0.0.1


And voila! We have replicated the results. PowerShell is not broken. Our faith in functions and parameter passing is restored!




As an aside, it's probably not recommended to pass strings into your functions and rely on the function to convert it into a hash table, as that can give, as you have demonstrated, unpredictable results. I would instead directly pass in a Hash Table (Type [hashtable]) right into the function:



testHashTable.ps1



param (
[Parameter(Mandatory=$true)]
[hashtable]
$MyHashTable
)

Write-Output $MyHashTable


PS> $testHashTable = @ "HOSTNAME" = "jburns-test" ; "IP" = "127.0.0.1" 
PS> .test.ps1 -MyHashTable $testHashTable

Name Value
---- -----
IP 127.0.0.1
HOSTNAME jburns-test






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 9 at 0:52









HAL9256HAL9256

4,0831330




4,0831330












  • Thank you for the complete, clear response this makes sense. After talking with a coworker we came to the similar conclusion, thinking that a user would expect what they typed in a prompt to be treated literally and not accidentally interpolated. And yes I think passing by a hashtable is smarter I thought at the time that I would have to deal with a string from the caller side but there is another way.

    – Buster
    Mar 9 at 3:16

















  • Thank you for the complete, clear response this makes sense. After talking with a coworker we came to the similar conclusion, thinking that a user would expect what they typed in a prompt to be treated literally and not accidentally interpolated. And yes I think passing by a hashtable is smarter I thought at the time that I would have to deal with a string from the caller side but there is another way.

    – Buster
    Mar 9 at 3:16
















Thank you for the complete, clear response this makes sense. After talking with a coworker we came to the similar conclusion, thinking that a user would expect what they typed in a prompt to be treated literally and not accidentally interpolated. And yes I think passing by a hashtable is smarter I thought at the time that I would have to deal with a string from the caller side but there is another way.

– Buster
Mar 9 at 3:16





Thank you for the complete, clear response this makes sense. After talking with a coworker we came to the similar conclusion, thinking that a user would expect what they typed in a prompt to be treated literally and not accidentally interpolated. And yes I think passing by a hashtable is smarter I thought at the time that I would have to deal with a string from the caller side but there is another way.

– Buster
Mar 9 at 3:16



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069375%2fwhy-do-i-get-different-output-when-entering-the-same-string-in-a-prompt-vs-passi%23new-answer', 'question_page');

);

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







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко