Frage

I have a powershell script that executes a custom executable file on a remote server. I have no problem getting the script to execute and to function correctly IF I first execute the program manually first on the server.

The reason for this is that the program dynamically creates assemblies and save these to file for later use. If these files exists the program tries to remove them and recreate or if this fails uses existing files.

The conclusion that I have is that when running the script the program doesn't have the rigth to handle the file system for some reason. Since if I run the program manually on the server the dynamic assembly files are created and subsequent executions of the script works as if the file was created, i.e. it reads the files created before. The files are created in the same directory as the program executes in.

How can I setup the powershell script so that files are allowed to be created on the remote server?

Below is "almost" my script

$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$password = ConvertTo-SecureString -String "myPass" -AsPlainText -Force
$credentials = New-Object pscredential("remoteUser", $password)
$servernamn = "myMachine"

Invoke-Command -ComputerName $servernamn -Credential $credentials -ArgumentList $path ScriptBlock { & 'D:\temp\custom.exe' }
War es hilfreich?

Lösung 2

I got this to work after a while, couldn't understand why the suggested try didn't.

At the end I got the following script

Invoke-Command -ComputerName $servernamn -Credential $credetials -Scriptblock { 
   cd "D:\temp\"
   ./custom.exe
}

I don't know why there is a difference in running the program AFTER i change directory but apparently there is. Anyway this works and if somebody have the same problem try this.

Andere Tipps

I would try running your Invoke-Command with a script block similar to:

`Invoke-Command` -ScriptBlock {Start-Process -FilePath D:\temp\custom.exe -runas}

The change here is using start-process and telling the remote machine to run the process with elevated privileges (-RunAs switch), you could also use -wait with start-process if you wanted to wait for the exe to finish before your script continues to execute.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top