Lots of loads on the poor virtual machine makes them unresponsive quite a few times and we face this issue of restarting a VM remotely.... good to write a blog about it and save time of some one :)
Lets make it more challenging by using Power Shell
Make sure that you'd already turn on the Power Shell Remoting feature on using cmdlet Enable-PSRemoting
To run the power shell commands from remote machine, first connect to that machine using power shell console cmdlet Enter-PSSession which requires a parameter credential, so its always better to store the credential in a variable
Store credential in a var
PS C:\Users\MyUserAdminAccount
> $cred = Get-Credential -Credential MyUserAccountDomain\MyUserAdminAccount
Now connect to that machine, but wait if you receive below error then you havent executed
Enable-PSRemoting on that server
PS C:\Users\MyUserAdminAccount>
Enter-PSSession -ComputerName MyDestinationServerName -Credential $cred
Enter-PSSession : Connecting to remote server failed with the following error message : The client cannot connect to th
e destination specified in the request. Verify that the service on the destination is running and is accepting requests
. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or Win
RM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the
WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:16
+ Enter-PSSession <<<< -ComputerName MyDestinationServerName -Credential $cred
+ CategoryInfo : InvalidArgument: (MyDestinationServerName:String) [Enter-PSSession], PSRemotingTransportExceptio
n
+ FullyQualifiedErrorId : CreateRemoteRunspaceFailed
This time clean attempt to connect to MyDestinationServerName--
PS C:\Users\MyUserAdminAccount>
Enter-PSSession -ComputerName MyDestinationServerName -Credential $cred
Now to find how many users are already connected to this VM, use standar query command
[MyDestinationServerName]: PS C:\Users\MyUserAdminAccount\Documents> query session /SERVER:MyDestinationServerName
SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
console 1 Conn
rdp-tcp#1 MyUserAdminAccount 45 Active rdpwd
rdp-tcp 65536 Listen
Now use the logoff command to logoff a particular session, wanna know what is it?
[MyDestinationServerName]: PS C:\Users\MyUserAdminAccount\Documents>
cmd /c logoff/?
cmd.exe : Terminates a session.
+ CategoryInfo : NotSpecified: (Terminates a session.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
LOGOFF [sessionname | sessionid] [/SERVER:servername] [/V] [/VM]
sessionname The name of the session.
sessionid The ID of the session.
/SERVER:servername Specifies the Remote Desktop server containing the user
session to log off (default is current).
/V Displays information about the actions performed.
/VM Logs off a session on server or within virtual machine. The unique ID of the session needs to be
specified.
[MyDestinationServerName]: PS C:\Users\MyUserAdminAccount\Documents>
logoff 45 /Server:MyDestinationServerName /v /vm
You are about to logoff MyUserAdminAccount (session 45) from machine ,
continue (n=no)?
Finally restart the virtual machine
[MyDestinationServerName]: PS C:\Users\MyUserAdminAccount\Documents>
restart-computer MyDestinationServerName
So easy isn't it?