0

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!

Will
  • 25
  • 1
  • 4
  • Did you do any sort of web search for "run PowerShell on remote computer"? There's very clear examples. – mason Jul 24 '17 at 20:19
  • I want to run a script on the remote from a script on my pc. I did not find anything revelant. – Will Jul 24 '17 at 20:27
  • Really? I used Google, entered the same exact search terms as shown in my previous comment, and the very first hit explained everything you need to accomplish this. – mason Jul 24 '17 at 20:28
  • Use Invoke-Command -Computername $RemoteComputer -ScriptBlock { #put your code here that you want to run on remote computer} – Adam Mnich Jul 24 '17 at 20:33
  • Possible duplicate of [Run .ps1 on remote machine](https://stackoverflow.com/questions/43968015/run-ps1-on-remote-machine) – Adam Mnich Jul 24 '17 at 20:37
  • Why don't you just use PSSessions? – Maximilian Burszley Jul 24 '17 at 22:53

2 Answers2

0

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
SteloNLD
  • 541
  • 3
  • 12
  • See this question [connecting-to-remote-server-failed-using-winrm-from-powershell](https://stackoverflow.com/questions/16010091/connecting-to-remote-server-failed-using-winrm-from-powershell), it tells you what to do next || you are on the right track now, good luck! || – SteloNLD Jul 25 '17 at 13:26
  • And now my remote desktop tells me I have don't have an appropriate certificate. Anything I can change on my part because this message basically tells me this is a no-no. – Will Jul 25 '17 at 15:06
0

You have Three PC named PC1,PC2, PC3

Each PC has a script folder at C:\Temp\GetPCName.ps1 with below content

On PC1:

Write-Output("Script on PC1 Executed on $Env:COMPUTERNAME")

On PC2:

Write-Output("Script on PC2 Executed on $Env:COMPUTERNAME")

On PC3:

Write-Output("Script on PC3 Executed on $Env:COMPUTERNAME")

How to execute Local script on remote computer[s]

From PC1 execute below script

$S = New-PSSession PC1,PC2,PC3
Invoke-Command -Session $s -FilePath C:\Temp\GetPcName.ps1
Remove-PSSession $S

OR

Invoke-Command -ComputerName PC1,PC2,PC3  -FilePath C:\Temp\GetPcName.ps1

Output:

Script on PC1 Executed on PC2
Script on PC1 Executed on PC1
Script on PC1 Executed on PC3

How to execute remote script on remote computer[s]

From PC1 execute below script

$S = New-PSSession PC1,PC2,PC3
Invoke-Command -Session $s -ScriptBlock {. "c:\Temp\GetPcName.ps1"}
Remove-PSSession $S

Output:

Script on PC1 Executed on PC1
Script on PC2 Executed on PC2
Script on PC3 Executed on PC3
Hemendr
  • 673
  • 6
  • 12