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?