Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

4
Trying to learn powershell for the first time, And I am having trouble creating a command for querying a RDP server for a session ID and then killing it
Author Summary
SparkStorm is in Idaho
Post Body

It almost all works great. When I have the server written in, it works properly, but when I switch it to a variable so I don't have to have a company specific PS command it gives me weird error.

Image version This first one said access denied because I'm just logged in as a regular domain user instead of domain admin so that's good that it worked. Second command said my syntax is wrong and i don't know why https://i.imgur.com/lY6Vhz9.png

Text versions: Working:

$UserTSO=Read-Host -Prompt 'Input the user logon name';#USERTSO=User to sign off

$Server1=Read-Host -Prompt 'Input the hostname or IP address of the server';#Server1= Server the user has an RDP Session to

$users = query user $USERTSO /server:192.168.1.11;#Command to determine which user and server's session to kill

$IDs = @() ;

for ($i=1; $i -lt $users.Count;$i ) { $temp = [string]($users[$i] | Select-String -pattern "\s\d \s").Matches ; $temp = $temp.Trim() ; $IDs = $temp ; }

logoff $IDs /server:192.168.1.11;

Input the user logon name: administrator Input the hostname or IP address of the server: 192.168.1.11 Could not logoff session ID 1, Error code 5 Error [5]:Access is denied.

Not working at all:

$UserTSO=Read-Host -Prompt 'Input the user logon name';#USERTSO=User to sign off

$Server1=Read-Host -Prompt 'Input the hostname or IP address of the server';#Server1= Server the user has an RDP Session to

$users = query user $USERTSO /server:($Server1);#Command to determine which user and server's session to kill

$IDs = @() ;

for ($i=1; $i -lt $users.Count;$i ) { $temp = [string]($users[$i] | Select-String -pattern "\s\d \s").Matches ; $temp = $temp.Trim() ; $IDs = $temp ; }

logoff $IDs /server:($Server1);

Input the user logon name: administrator

Input the hostname or IP address of the server: 192.168.1.11

Invalid parameter(s)

Display information about users logged on to the system.

QUERY USER [username | sessionname | sessionid] [/SERVER:servername]

username Identifies the username.

sessionname Identifies the session named sessionname.

sessionid Identifies the session with ID sessionid.

/SERVER:servername The server to be queried (default is current).

Invalid session ID 192.168.1.11

Author
Account Strength
100%
Account Age
12 years
Verified Email
Yes
Verified Flair
No
Total Karma
53,312
Link Karma
4,624
Comment Karma
48,662
Profile updated: 3 days ago
Posts updated: 10 months ago
Sysadmin

Subreddit

Post Details

Location
We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago