Glenda Gable
Glenda Gable

Reputation: 11

execute remote powershell script on remote server

I have looked for weeks, and had others research with/for me, but I haven't found the answer to this.

I have a script that lives on server 1, and needs to be executed on server 1 using certain credentials. The ultimate solution will help with decrypting files. The script must execute as a certain user on a certain server because the key pair is in the user's key ring on that server.

I want the script to be called from server 2 as part of a larger process. Server 2 has a sql job which has the following flow:

  1. Step 1 of the job uses a powershell script to download the file
  2. Step 2 of the job needs to execute the script on server 1 to decrypt the file (** this is the step I am talking about here)
  3. Step 3 of the job will run a powershell script to check the file attributes (last written date, file size as compared to yesterday, etc)
  4. Step 4 of the job will restore the log file

The script on server 1 runs locally without any issues, and produces the expected results. When attempting to have server 2 execute the script on server 1, I haven't had any success. In order to get as simple as possible for finding out what is actually not working, I came up with a set of scripts which are simple (see below).

I am not a powershell guru of any kind - so I apologize now if it is a very obvious thing. :)

#-------------------------------------
# script 1
# this script lives on server 1
# file name: c:\deleteme\helloworld2.ps1

$CMD = Invoke-Expression " `"hello world`" >> C:\deleteme\helloworld.txt"
Invoke-Command -ScriptBlock { $CMD }



#-------------------------------------
# script 2
# this script is executed on server 2 and attempts to execute the script on server 1

$session = New-PSSession -ComputerName "SERVER01.lahcg.com" #-Credential $username


$CMD = "Invoke-Command -ScriptBlock { powershell.exe `"C:\deleteme\helloworld2.ps1`" } "
#write-output $CMD
Invoke-Command -Session $session -ScriptBlock { $CMD }

Remove-PSSession $session  

Environment Info:

Extra Notes

Upvotes: 1

Views: 3590

Answers (1)

Michael Timmerman
Michael Timmerman

Reputation: 328

BenH is correct. You have two options. You can either use

  1. Invoke-Command to execute a {Code block}
  2. Connect-PSSession to connect to the computer as a given user

Invoke-Command is the faster way to do it since it automatically establishes a PSSession, executes the code, then tears it down. The issue with it is that it doesn't allow you to control which user establishes the session. For that you have to build the session yourself then tell Invoke-Command which session to use. So try something like this:

  1. From:Server2 $Credentials = Get-Credential #This can be scripted rather than prompted. Google it.
  2. From:Server2 New-PSSesssion -ComputerName Server1 -Credential $Credentials
  3. From:Server2 Now you can either connect to the session and launch your code, or use invoke-command to just push a single command to the session:
    1. Connect-PSSession -Session (Get-PSSession)
    2. Invoke-Command -Session (Get-Session) -ScriptBlock {C:/script1.ps1}

Upvotes: 1

Related Questions