Wanted to share a script I wrote today, its very very helpful for us especially on our Terminal server, to quickly and easily and remotely logoff any user specified by “ID”. This helps me out so much in my job I am guessing it will help others too! Instead of the boring long one liners or actually logging into the server itself or the numerous other ways, I have found that this is MUCH faster and smoother.
This uses PSEXEC and the “QWINSTA” command to parse logged on users and display information. then you can input a “ID” to logoff that specified user, OR you can simply exit after viewing who is logged on.
I use it in my Powershell Profile and its very nice to just call it as a function and BAM! super quick information enjoy, and feel free to modify it and or give me some input as I am certainly no guru or pro. not even remotely just a script / PowerShell junkie trying to make my life easier and automate as much as i can!
Fixed, errors…forgot to add the Quotes in the psexec command, i have since updated the code below!
# This function will parse the specified computer / server for logged on users, and then ask if you want to log them off using the "ID" parameter of that user. if no input, then it will exit. function LoggedOn { param( [Parameter(Mandatory=$true)] [string[]] $ComputerName) $computersystem = Invoke-Command -ComputerName $ComputerName -ScriptBlock {cmd.exe /C qwinsta} write-host "" write-host "Gathering resources. Please wait..." Start-Sleep -Seconds 8 write-host "" foreach ($Computer in $ComputerName) { $computersystem $UserInput = Read-Host "Do you Want to logoff a user Session ID? `n[Y]es, [N]o or e[X]it" if (("Y","Yes","N","No","X","Exit") -notcontains $UserInput) { $UserInput = $null return Write-Warning "Please input a correct value" } if (("X","Exit","N","no","n") -contains $UserInput) { Write-Host "No changes made, exiting..." Break } if (("Y","Yes","y") -contains $UserInput) { $sessionnumber = Read-Host "Session ID" if ($sessionnumber -notcontains $null) { $logoff = (psexec \$ComputerName cmd.exe /C logoff $sessionnumber) $logoff Write-Host "The User Logged on under ID $sessionnumber Has been Logged Off Successfully." Write-Host "Exiting..." } }
} }
Edit: was completely not thinking, updated script to use invoke-command. everything works good now.
thanks,
submitted by /u/ex0s
[link] [comments]
The post [Script Sharing!] Remote Logged On User Script with logoff capability :) appeared first on How to Code .NET.