Link to home
Start Free TrialLog in
Avatar of DarthElmo
DarthElmo

asked on

Powershell script to call and pass arguments to another Powershell script

Hi,

With the help of Experts here, I've written a Powershell script that remotely executes another Powershell script. The only problem is that the called Powershell script is executing without successfully accepting the argument I'm passing (which is a partial directory path).

Here's the command from the calling PS script:
Invoke-Command { powershell.exe <full path of PS script on remote server> } -ComputerName <server name> -Credential <authentication string> -ArgumentList <directory listing>

Here's the first line in the called PS script:
$<variable name> = Write-Host $args[0]

I'm writing the value of the variable to a log file and the value is empty. Any pointers for me?

Thanks!
Avatar of Jornak
Jornak
Flag of Canada image

You'll have to create a proxy cmdlet: see here.
What about this?

Invoke-Command {param($ScriptPath,$ArgPath) powershell.exe $ScriptPath $ArgPath} -ComputerName <server name> -Credential <authentication string> -ArgumentList <script path>,<directory listing>
Avatar of DarthElmo
DarthElmo

ASKER

Jornak, I tried reading the article you linked but am pretty overwhelmed by it. This is my first Powershell script. I'd really appreciate your help in applying the article to my specific problem if you have the time.

Thank you!
Vaulden, I tried your suggestion but am receiving the error that $ScriptPath and $ArgPath are "not recognized as the name of a cmdlet, function, script file, or operable program." The $ScriptPath should be the full path to the Powershell script on the remote server, yes? And the $ArgPath should be the argument I'm passing (which in my case is the partial path), yes?

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of Vaulden
Vaulden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It seems like we're really close, Vaulden. When executing the calling PS script, the PS Console now displays the passed argument - the partial directory - where it was not before. The very first line of the called PS script is:
$Dir = Write-Host $args[0]

Since the calling PS script does not echo that directory, it must be coming from the Write-Host command in the called PS script. However, as the called PS script continues, it behaves as if $Dir is an empty string and therefore ultimately fails. Is "Write-Host $args[0]" an incorrect way to assign a passed argument to a variable?
Well, I decided to simply try $Dir = $args[0] and it looks like the value was successfully assigned to $Dir. I've got a few other errors to follow up on but they do not appear to be related.
Write-Host sends it to the screen, I had meant to ask about that earlier. You are correct in that $Dir  = $args[0] is the correct way to assign the variable.
Well, the argument is definitely being passed successfully and as a result, the variables in the called PS script are correctly assigned. I would have thought I was home free. However, everything fails after that. It's got to be a permissions issue.

The script says it can't find files when in fact the directory is valid and the file exists, for example. I thought the called PS script would be executed by the same ID that is remotely connecting to the server but the called PS script is behaving as if the ID executing it has no permissions to do anything.

Now if I execute the same script from the actual server where the PS script resides and hard code the one parameter it would need if it were being called, then everything executes exactly as I would expect.

Should I go ahead and accept the above solution and create a new question for this latest development?
It's up to you, I'll be willing to help either way. However, if we continue on this ticket someone searching for an answer to the new issue may not find this ticket (since the original question does not pertain).
Thanks so much for your help, Vaulden! I'm opening a new question for this last development.