hmrc87
hmrc87

Reputation: 354

Call a Powershell Scriptfile that resides on a remote server

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

Upvotes: 0

Views: 63

Answers (1)

Jeroen Mostert
Jeroen Mostert

Reputation: 28809

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" }

Upvotes: 2

Related Questions