1

after trying several possibilities I'am not sure how to continue.

I need to execute a script "C:\test1\script.ps1" And I need to pass some Arguments with this script too. These Arguments (-Hashtag $Hashtag -Name $Name and so on...) are stored in a Variable called $arguments.

So the final call should look like : C:\test1\script.ps1 $arguments

That is no problem, but now the thing I can't get managed to work.

My goal is to execute this as a different user. The Login credentials are stored in $cred

I already tried the following things compared to related topics here in the forums:

  1. Command

    Invoke-Command -Credential $cred -Authentication Credssp -ComputerName home -ScriptBlock {C:\test1\script.ps1 $arguments}
    

    Powershell Result: Some of the arguments (in this case the Hashtag) is empty. Im not sure why this happens cause I just filled the $arguments Variable with all the stuff.

  2. Command

    Start-Process -FilePath "C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe" -ArgumentList "C:\test1\script.ps1 $arguments -Credential $cred -WorkingDirectory 'C:\\Windows\System32'
    

    This results that the Script won't get executed.

Does anyone have some more info about handling that construction? Maybe there is another Command I can pass credentials to?

As another notice, I cant use Credential Prompts so any solutions where UAC get into consideration is not suitable for my problem.

Mel
  • 5,837
  • 10
  • 37
  • 42
  • `$arguments` would need to be [splatted](https://technet.microsoft.com/en-us/library/gg675931.aspx) eg. `@arguments` – BenH Jun 16 '17 at 13:37
  • I Followed the Instructions, but It won't work either. `$arguments= @{ Argument1 = $Argument1 Argument2 = $Argument2 }` – FGET Masters Jun 19 '17 at 07:53
  • I Followed the Instructions, but It won't work either. `$arguments= @{ Argument1 = $Argument1 Argument2 = $Argument2 }` The desired Script gets executed, thats fine. But throwing me an Error that the Arguments are empty... When I print the @arguments there are all the Data iI need. So the splatting should work proberly. But indeed these arguments son#t get passed to the script correctly. I call the script now like this: `-ScriptBlock {C:\test1\script.ps1 @ausfuehrung} ` Is that correct? – FGET Masters Jun 19 '17 at 08:00
  • If you pass it in like that, without a param block or utilizing argumentlist, then you would need to use the `using:` scope. See this [question](https://stackoverflow.com/questions/35492437/how-can-i-pass-a-local-variable-to-a-script-block-executed-on-a-remote-machine-w) – BenH Jun 19 '17 at 15:21
  • Thanks for the advises, but I got it running with: `start powershell.exe -Argumentlist "C:\test1\script.ps1 $arguments" -credential $credential ` Now just the return value is missing. But I am trying to solve this on my own. Thanks for the help at all :) I learned much about splatting, which I never heard of before. Thanks! – FGET Masters Jun 20 '17 at 09:29

1 Answers1

0
Invoke-Command -Credential $cred -Authentication Credssp -ComputerName home -ScriptBlock {C:\test1\script.ps1} -ArgumentList 'argument1','argument2','argument3'

Make sure in the script, you define positional parameters. This way argument 1 is assigned to your first parameter in the script and so on.

Jessie
  • 284
  • 1
  • 3
  • 12
  • Well, If I use this Syntax, Powershell telling me that there is no Parameter for the ParameterName "Arguments". I wrote it like this, because the arguments change dynamicly I need to use the Variables: `-ScriptBlock {C:\test1\script.ps1} -Arguments $Argument1, $Argument2... ` – FGET Masters Jun 19 '17 at 07:50
  • My mistake, the parameter should be -ArgumentList – Jessie Jun 19 '17 at 14:07
  • Thanks for the advises, but I got it running with: `start powershell.exe -Argumentlist "C:\test1\script.ps1 $arguments" -credential $credential ` Now just the return value is missing. But I am trying to solve this on my own. Thanks for the help at all :) – FGET Masters Jun 20 '17 at 09:28