VMware Cloud Community
jspatz
Contributor
Contributor

Adding variables to invoke-vmscript

Trying to come up with a way to add users based on form input of requested users to be added to a linux deployment.  I was hoping to use the invoke-vmscript -scripttext $code

with 

$code= useradd -g users $requestedUseracct 

I haven't found documentation as to how to pass that variable to the $code (as it just ends up running in linux as $requestedUseracct or blank because that variable doesn't exist on the host.    I am assuming it's some kind escaping I need to do .. but I haven't had much luck!

0 Kudos
3 Replies
jbowdre3
Contributor
Contributor

Things get kind of messy when running a Linux shell script from a PowerShell wrapper. I'd start by wrapping your script in double-quotes. That should let you pass in $username without having to escape it.

Something like:

 

 

$userAccountScript = "useradd -s /bin/bash $username"

$runScript = Invoke-VMScript -VM $vm -ScriptText $userAccountScript -GuestUser "$templateUser" -GuestPassword "$templatePass"

 

 

 

If you need to pass any special characters into the Linux shell (like if you want to work with a variable inside the shell instead of replacing the contents at the PowerShell level), prefix them with a backtick (`).  

Like so:

 

 

$userAccountScript = "if [ `$(getent group wheel) ]; then adminGroup='-G wheel'; elif [ `$(getent group sudo) ]; then adminGroup='-G sudo'; fi; useradd -s /bin/bash `$adminGroup -m $username"

$runScript = Invoke-VMScript -VM $vm -ScriptText $userAccountScript -GuestUser "$templateUser" -GuestPassword "$templatePass"

 

 

 
jspatz
Contributor
Contributor

Good morning and thank you for your reply.

When I was putting the $username in the double quotes.. it was just passing the $username into the vm (and not the value coming from the blue print)

I ended up doing this which worked.. I just couldn't figure out how to get multiple lines or pass a carriage return

$code = "useradd -g users -G wheel " + $adminUser
$code2 = "echo " + $adminPass + " | passwd --stdin " + $adminUser

Write-Host "Attempting to add admin account..."
Write-Host "[$code]"
Write-Host "[$code2]"
$runAdminScript = Invoke-VMScript -VM $vm -ScriptText $code -GuestUser $osUser -GuestPassword $osPassword
$runAdminScript2 = Invoke-VMScript -VM $vm -ScriptText $code2 -GuestUser $osUser -GuestPassword $osPassword

Not as clean as I wanted but it's doing the job.  I will try the backtic though .. I don't think I tried that one.

 

0 Kudos
jbowdre3
Contributor
Contributor

You can use a semicolon to separate commands which would otherwise be entered followed by a carriage return.

 

$script = "echo 'here is task one'; echo 'here is task two'"

// or in your case

$script = "useradd -g users -G wheel $adminUser; echo `"$adminPass`" | passwd --stdin $adminUser"

 

 

(Those backticks escaping the double-quotes around $adminPass is needed so that the quotes will remain a part of the string which gets processed by Invoke-VMScript.)

0 Kudos