Minor problem running script

I have a PS script which checks a collection of servers to determine if my admin account is logged in - I use Team Viewer RDP sessions for a lot of my work. When it finds a session, it logs my admin account out of that server. I use this at the end of the day so that I don’t leave my admin account logged in somewhere I overlooked.

If I open a PS window and launch the script, it appears to hit all the servers in my list but doesn’t do anything.

If I open PS “as administrator” and run the script, it logs me out of the sessions it finds. So far, so good.

Here’s my code - note that Write-Color is a function I did not include here.

$Username = "MyAdminAccountID"
$serverList = get-content c:\utils\ServersKLst.csv
$StartTime = $(get-date)
$LogCount = 0
foreach ($Server in $serverList) {
			$sessionID = ((cmd /c quser /server:$server "2>NUL"| ? { $_ -match $username }) -split ' +')[2]
			Write-Color -text "$sessionID"," $Server" -color yellow,yellow
				If ($sessionID)	{
					Write-color -text "    $($Username)"," logged ","OFF ","$($Server)"," session ID: ","$($sessionID)" -color blue,green,red,yellow,green,yellow
					Logoff $sessionID /server:$Server
					$LogCount++
				}
	}
	
$elapsedTime = $(get-date) - $StartTime
$totalTime = "{0:HH:mm:ss}" -f ([datetime]$elapsedTime.Ticks)
if ($LogCount -ne 1) {
	$Plural = "s"
	}
Write-Color -text "`n  Logoff completed.  Total time:"," $totalTime"," Logged off of ","$LogCount"," Server$Plural`n`n" -color cyan,yellow,green,yellow,green
Pause

So, to use this I have to:

  • right-click Powershell
  • select Run As Admin
  • Enter my Admin creds
  • Change directory to C:\Utils
  • Run .\logmeoff.ps1

What I’d like to do is have an icon on my desktop (or preferably task bar) which I can run at the end of the day as I’m packing up and have this script close any sessions I haven’t already closed. If I have to provide my admin creds when I launch the desktop icon that’s fine. I just don’t know how to do this.

Thanks for suggestions!

—K

You can create a new shortcut file, then fill in the details on the Shortcut tab like so:


The target should be something like:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -file "C:\Utils\logmeoff.ps1"

I don’t think it’s necessary to change the “Star in” field, but you can.
Then click the advanced button and check the box that says you want it to run as administrator. Finally, click the Change Icon button and select whatever icon you want. I went with the Powershell/UAC combo icon.

You’ll also need to go into Advanced and tick ‘Run as admin’ since your regular user doesn’t have permission to the quser command on the remote systems

Thanks, Grey! That worked perfectly.

Much appreciated

neemobeer, Grey mentioned that in the instructions

Thanks!

Heh heh, missed that :grimacing: