Reputation: 117
I want to log off a user on a remote machine using the powershell code:
$session = ((quser /server:$system | Where-Object { $_ -match $user }) -split ' +')[2]
logoff $session /server:$system
Currently these commands are executed by account : NT AUTHORITY\SYSTEM but i want to execute the commands using another user.
I have tried using :
runas /user:administrator /savecred "quser /SERVER:$server"
but it prompts for password, I want to execute the script automatically . Is there any way I can switch the user without having to pass the password through terminal?
Upvotes: 0
Views: 2133
Reputation: 1869
First of all let me say that this is not a good idea for many security reasons, one of them being that if someone gets access to your script it will have access to your password. Anyway, what you want to do is not possible but you can implement a workaround.
Instead of "run a command without password prompt" if you know the password you can "fake" a user typing it into the prompt, this will not suppress the prompt but will write the password as if someone was actually typing it on a keyboard.
To achieve this you can use the SendKeys
function, it essentially fakes a key press.
The idea is to wait for the password prompt to show and then send the password character by character faking a keystroke.
As an example this will send the keys H
and i
and then press enter
as if someone physically typed them.
#Write some text
[System.Windows.Forms.SendKeys]::SendWait("Hi")
#Press on Enter
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Unfortunately my knowledge finishes here since I've never actually used it successfully but always relied on manual input.
Upvotes: 2