Avatar of DarthElmo
 asked on

Powershell script to call and pass arguments to another Powershell script


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?


Avatar of undefined
Last Comment

8/22/2022 - Mon

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>

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!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

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!

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

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).
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

Thanks so much for your help, Vaulden! I'm opening a new question for this last development.