So basically I have an already functioning script on my remote server. I'm trying to run a powershell script on my pc that would automatically go into remote desktop then run my script over there.
The simple script I'm using now isn't working :
mstsc /v:Server
Invoke-Expression c:\Scripts\script.ps1
$userName = 'administrator'
$sessionId = ((quser /server:server | Where-Object { $_ -match $userName }) -split ' +')[2]
logoff $sessionId /server:server
My script is at c:\Scripts as you can see. Thing is this is ignoring the remote desktop and opening my pc's c:\ instead.
How am I supposed to "stay" on the remote desktop to execute my script?
Also, I'm not sure if my logoff code is working. I found it somewhere in a thread, I would also like some clarifications if it is coded right. I know I can use scheduled tasks, but I am working on using this way instead.
Thanks a lot!
mstsc /v:Server might open the Remote Desktop for you, the console is still on your own machine, you need to relaunch te console remotely or use the options provided in the comments above by @AdamMnich for example.
I don't even want to know how you could think mstsc /v:Server could work this way in a script but it was a nice try ;)
The suggested solution looks like this:
$RemoteComputer = "ServerHostNameOrIPAdress"
Invoke-Command -Computername $RemoteComputer -ScriptBlock {."c:\Scripts\script.ps1"} #Assuming the script is on the C:\ Drive of the RemoteComputer