.NET Framework - Escape character question

Asked By lawndart
06-Aug-07 01:22 PM
I have a DOS command I'm trying to run from powershell, as part of an
automation script. The problem I'm having is an "Incomplete String
Token" error when I use Invoke-Expression on it. Here are the relevant
bits:

$pwdSetString = "bemcmd.exe -o213 -
i""`{10011001-1001-1001-0101-010101010101`}"" -pw:""$oldPwd"" -
pl:""$newPwd"" -cemslbk01"

$pwdSetString

$results = Invoke-Expression -command $pwdSetString

Output
-------------------------
PS - powershell> ./set-BUEServiceAccountPwd.ps1
bemcmd.exe -o213 -i"{10011001-1001-1001-0101-010101010101}" -pw:"asd" -
pl:"asd" -cmachineName
Invoke-Expression : Incomplete string token.
At \Path\set-BUEServiceAccountPwd.ps1:16 char:29
+ $results = Invoke-Expression  <<<< -command $pwdSetString
BUE Password set result:

I know that the {} characters are the issue, but they are pretty much
required in the command. This command works fine in DOS and I assumed
that escaping them would make PS happy as well. Any ideas on how to
run this?

Lawndart
CmachineName
(1)
NewPwd
(1)
OldPwd
(1)
Huh
(1)
Scriptblock
(1)
Cemslbk01
(1)
O213
(1)
Bits
(1)
  Oisin Grehan replied...
07-Aug-07 12:08 PM
Try:

PS 1> $pwdSetString = { bemcmd.exe `-o213 `-
i"`{10011001-1001-1001-0101-010101010101`}" `-pwd:"$oldPwd" `-
pl:"$newPwd" `-cemslbk01 }
PS 2> $oldPwd = "blah"
PS 3> $newPwd = "pox"
PS 4> & $pwdSetString

I found that the minus sign parameter prefixes were confusing the
powershell parser as it was treating them as operators; I also changed
it to use scriptblock syntax to get rid of one layer of quotes.

Hope this helps,

- Oisin
  lawndart replied...
07-Aug-07 02:43 PM
Huh, did not think of using a script block for that. That worked quite
well, thanks.

Lawndart
Create New Account
help
for instance my implementation of change password is public override bool ChangePassword(string username, string oldPwd, string newPwd) { return SqlProvider.ChangePassword(username, oldPwd, newPwd); } / / I have also implemented the initialize method as if (config = = null) throw new ArgumentNullException("config
web? Thank you. ASP Code Reference: Set UsrObj = GetObject("WinNT: / / " & ServerName & " / " & UserName , user) UsrObj.SetPassword NewPwd UsrObj.SetInfo If Err.Number = 0 Then OutMsg("The password of " & UserName & " was successfully changed using System.DirectoryServices; DirectoryEntry myDirectoryEntry; myDirectoryEntry = new DirectoryEntry(@"WinNT: / / " + ServerName + " / " + UserName + ", User"); myDirectoryEntry.Invoke("setPassword", NewPwd); myDirectoryEntry.CommitChanges(); May I know how to know the return code? Thank you You should catch an exception try { myDirectoryEntry = new DirectoryEntry(@"WinNT: / / " + ServerName + " / " + UserName + ", User"); myDirectoryEntry.Invoke("setPassword", NewPwd); myDirectoryEntry.CommitChanges(); } catch (Exception e) { OutMsg("Unexpected Error: " & e.ToString() & ", Please contact the webmaster."); return