VMware Cloud Community
IKirill
Enthusiast
Enthusiast
Jump to solution

Invoke-VMScript escape a character in a variable

Hi!

One of our customers requires you to change your password, which contains the symbol -$. How can I escape a character in a variable?

$password = "www#$123"

$scriptText = @"

echo osadmin:$password | chpasswd

"@

Invoke-VMScript -VM myvm -ScriptType bash -ScriptText $script -GuestUser root -GuestPassword Zz123456

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Use single quotes instead of double quotes

Try like this

$password = 'www#$123'

$scriptText = @"

echo osadmin:$password | chpasswd 

"@

Invoke-VMScript -VM myvm -ScriptType bash -ScriptText $script -GuestUser root -GuestPassword Zz123456


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Use single quotes instead of double quotes

Try like this

$password = 'www#$123'

$scriptText = @"

echo osadmin:$password | chpasswd 

"@

Invoke-VMScript -VM myvm -ScriptType bash -ScriptText $script -GuestUser root -GuestPassword Zz123456


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

single quotes not working

i use

echo osadmin:$password > /tmp/result.txt

and see, that $ symbol is missing

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Where did you use single quotes (see my example above)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos