Windows: remote sessions, reboot and adding a local admin
PsExec, PowerShell and cmd commands to log off a session, restart and add a local administrator remotely.
Cheat sheet to administer a Windows machine remotely: open a session, log off a user session, restart, and add a local administrator.
Log off a user session with PsExec
psexec \\sqlserver01 -u CONTOSO\admin_user cmd
query session
logoff 3
query session lists the sessions and their ids; logoff 3 closes session number 3.
Restart
PowerShell (-WhatIf simulates the operation without running it):
Restart-Computer sqlserver01 -Force -WhatIf
cmd:
shutdown -r -m \\sqlserver01 -f -t 1
Open a remote PowerShell session
Enter-PSSession -ComputerName sqlserver01 -Credential CONTOSO\admin_user
Add a local administrator
On several servers with PsExec:
"sqlserver01","sqlserver02" | % { .\psexec.exe \\$_ net localgroup Administrators "CONTOSO\svc_automation" /add }
On the local machine, with PowerShell:
Add-LocalGroupMember -Group Administrators -Member 'CONTOSO\svc_automation'