Reputation: 1
I have the following code inside my console application to get a server information using PowerShell:
string PsCmd = "add-pssnapin VMware.VimAutomation.Core; $vCenterServer = '" + vCenterName.Trim() + "';$vCenterAdmin = '" + vCenterUsername.Trim() + "' ;$vCenterPassword = '" + vCenterPassword + "';" + System.Environment.NewLine;
PsCmd = PsCmd + "$VIServer = Connect-VIServer -Server $vCenterServer -User $vCenterAdmin -Password $vCenterPassword;" + System.Environment.NewLine;
PsCmd = PsCmd + "Get-VMHost " + System.Environment.NewLine;
Now the script is working well on the 150 servers we have, but on a single server I am getting this exception:
At line:1 char:131 + ... !*******(********6'*;*****'; + ~ Unexpected token 'h' in expression or statement. At line:1 char:135 + ... **(******';'; + ~~ The string is missing the terminator: '. | ABC
Which is raising an error on the password variable. And in my case I am getting the password from an external RESP API. Now the password have this format:
\"************
Where it starts with 2 reserved characters \"
, and the JSON object which I get will be as follow {"PASSWORD":"\"*******"}
.. so could the problem be that the password contains reserved set of characters \"
?? And how I can fix this?
Upvotes: 2
Views: 1351
Reputation: 440337
Now the password have this format:
\"*******
Where it starts with 2 reserved characters
\"
If your passwords really come from JSON such as {"PASSWORD":"\"*******"}
, your passwords do not start with \"
- they start with "
, because the \
is simply a JSON escape character needed to embed a literal "
in char. in an (always double-quoted) JSON string value.
In other words: If you parse your JSON correctly, the actual password value will be:
"*******
You can verify this as follows:
PS> ('{ "PASSWORD": "\"*******" }' | ConvertFrom-Json).PASSWORD
"*******
Your next problem is that you're embedding the password in a string representing PowerShell source code, and inside that string you're using (embedded) single-quoting ('...'
) to surround the value[1].
In order to do that, you must escape value-embedded '
instances as ''
so as not to cause a syntax error when PowerShell later parses the code:
// Note the .Replace() calls
string PsCmd = "add-pssnapin VMware.VimAutomation.Core; $vCenterServer = '" +
vCenterName.Trim().Replace("'", "''") + "';$vCenterAdmin = '" +
vCenterUsername.Trim().Replace("'", "''") +
"' ;$vCenterPassword = '" + vCenterPassword.Replace("'", "''") + "';" +
System.Environment.NewLine;
With a literal password value of "***'***
(and sample values for the center and username), C# variable PSCmd
will receive a string with the following literal contents:
add-pssnapin VMware.VimAutomation.Core; $vCenterServer = 'center1';$vCenterAdmin = 'jdoe' ;$vCenterPassword = '"***''***';
[1] And single quotes are the right choice, because using double quotes could result in unwanted interpretation (string interpolation) of the embedded value by PowerShell.
Upvotes: 1
Reputation: 11520
You need to escape the "
. You do this in PowerShell using the `
character, e.g. `"
Try this:
string PsCmd = "Add-PSSnapin VMware.VimAutomation.Core; $vCenterServer = '" + vCenterName.Trim() + "'; $vCenterAdmin = '" + vCenterUsername.Trim() + "'; $vCenterPassword = \"" + vCenterPassword.Replace("\"", "`\"") + "\";" + System.Environment.NewLine;
Upvotes: 1