0

how do I call a ps1 file that resides on a target machine? All tutorials mostly say that I run a local ps1 on a remote machine. I tried the following but it just does nothing :/

$username = "theusername" $password = "thepassword" $secpassword = ConvertTo-SecureString –String $password –AsPlainText -Force $credential = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $username, $secpassword $so = New-PSSessionOption -SkipCACheck $session = New-PSSession -ConnectionUri "https://servername:5986/WSMAN" -SessionOption $so -Credential $credential Invoke-Command -Session $session -ScriptBlock { "powershell E:\\Tools\Powershells\MyPowershell.ps1" } Exit-PSSession 
3
  • 1
    Invoke-Command -Session $session -ScriptBlock { & "local-path-to-my-script.ps1" } Commented Sep 6, 2017 at 13:52
  • This seems to do the trick - Thank you please add this as answer :) Commented Sep 6, 2017 at 13:55
  • 1
    Might as well. I expected a duplicate question to exist, but I can't find one after a cursory search. Commented Sep 6, 2017 at 14:07

1 Answer 1

2

Executing a script by path (possibly with spaces) is done with

& "path with spaces\script.ps1" 

This works just as well when remoted, so to execute a remote script stored remotely, use

Invoke-Command -Session $session -ScriptBlock { & "path with spaces\script.ps1" } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.