I recently needed to write a script to copy and execute some files on some local machines, I was able to use Invoke-VMscript, and it worked just fine. Now, I want to use it to only log off a user. I will only put up the necessary line, since everything else I have tried has been working...here it is:
Invoke-VMScript -VM $vm -GuestUser 'Someone' -GuestPassword 'Password' -ScriptText "c:\Windows\system32\Shutdown.exe /l /f"
I just can't get it to only log the person off. I can easily log onto the VM click start and type shutdown /l /f, and it works, but since that didn't work in the script, I used the full path. I've tried single and double quotes, and I tried putting it into parenthesis. Any ideas?
The Guest OS is Windows 7.
Could this be the same "issue" that is reported in Re: Some Windows commands that need elevation normally do not with Invoke-VMScript?
Can you reach the guest OS from outside the VM ?
Then you could try to use
qwinsta /server:<name>
with the returned session IDs you cna then select the one you want and close it remotely.
rwinsta <session-id> /server:<name>
You could of course also run these inside the guest OS (with Invoke-VMScript), then you wouldn't need the server parameter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks, I can run both of those commands and log off the user, but since this is going to be multiple VMs, I'm not sure how to do this. I see the ID changes, and if it were strictly powershell, I could try to rwinsta where { $_.State -eq "Active" }, or something like that, but this is a windows command, so I'm not sure what to do. .
You can use a RegEx expression to find the ID needed on the rwinsta command.
Suppose you want to logoff the "console" session, then you could do
$consoleID = qwinsta |
Select-String -Pattern ">console\s*\w*\s*(?<ID>\d*)\s*" |
Select -ExpandProperty Matches |
%{$_.Groups["ID"].Value}
rwinsta $consoleID
If you need to do this for a number of VMs, you can use a loop.
Something like this for example
$cmd = @"
$consoleID = qwinsta |
Select-String -Pattern ">console\s*\w*\s*(?<ID>\d*)\s*" |
Select -ExpandProperty Matches |
%{$_.Groups["ID"].Value}
rwinsta $consoleID
"@
Get-VM | %{
Invoke-VMScript -VM $ -ScriptText $cmd
}
You specify the little script in a here-string, and then you run that script in each VM through the Invoke-VMScript cmdlet.
Let me know if that works ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks again, I have not had a chance to try that yet, I copied and executed the files, so the main portion of what I needed was accomplished. I'll try the log off part on some test VMs. Thanks again for your help, I'll make sure to reply back when I try it.