1

I am trying to run an executable with a config file as a parameter using invoke-command through a PowerShell script.

Here is what I have so far in my PowerShell script:

$config = 'D:\EmailLoader\App.config'
invoke-command -ComputerName SERVER-NAME -ScriptBlock {param($config) & 'D:\EmailLoader\GetMailAndAttachment.exe' $config} -ArgumentList $config

I then Execute the PowerShell script with the following command:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -File "D:\PowerShellScripts\Email.ps1"

And I get the following error:

Unexpected token 'config' in expression or statement.
At D:\PowerShellScripts\Email.ps1:3 char:121
+ invoke-command -ComputerName SERVER-NAME -ScriptBlock {param($config)     'D:\EmailLoader\GetMailAndAttachment.exe' $config <<<< } -ArgumentList $config
+ CategoryInfo          : ParserError: (config:String) [], ParentContainsE   rrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
AMouat
  • 685
  • 15
  • 27
  • 1
    So, what is the actual problem you're facing? You described what you're trying to do (which should work AFAICS), but not what's going wrong. – Ansgar Wiechers May 19 '15 at 11:10
  • @AnsgarWiechers I have edited the question to include the error. – AMouat May 19 '15 at 11:28
  • I would suggest check this question. http://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command – Roxx May 19 '15 at 11:39
  • I think I have got mixed up, the script at the top seems to run ok, the script I ran that returned the error message didn't have the & in front of the string in the ScriptBlock. Too many variations of scripts open! – AMouat May 19 '15 at 12:09

2 Answers2

1

The code looks like it should work. But the exception message is clearly missing the & symbol. I would check that first, because you are getting the exact same message I would anticipate to get when the & was missing. So maybe there is problem with the saved file, rather than with your code.

Side note: If you are on PowerShell 3.0 or newer you should consider using the $using: scope in the script block to avoid adding the param and -ArgumentList.

$config = 'D:\EmailLoader\App.config'
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock {
    &'D:\EmailLoader\GetMailAndAttachment.exe' $using:config
}
nohwnd
  • 826
  • 7
  • 13
  • I think I have got mixed up, the script at the top seems to run ok, the script I ran that returned the error message didn't have the & in front of the string in the ScriptBlock. Too many variations of scripts open! – AMouat May 19 '15 at 12:17
0

There is no need to pass $config as a parameter to a ScriptBlock. Also not needed to add the $config parameter twice. This should work:

$config = 'D:\EmailLoader\App.config'
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock {&('D:\EmailLoader\GetMailAndAttachment.exe')} -ArgumentList @($config)
MonkeyDreamzzz
  • 3,978
  • 1
  • 39
  • 36
  • Returns the following error:+ CategoryInfo : NotSpecified: (:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at GetMailAndAttachment.GMAA.ReadConfig(String ConfigFile, String Key) at GetMailAndAttachment.GMAA.Main(String[] args) – AMouat May 19 '15 at 12:00
  • I have made a change to make sure that the argumentlist is een array object. If that doesn't work then the issue is in the EmailLoader program – MonkeyDreamzzz May 19 '15 at 12:13
  • The error is in `GetMailAndAttachment.exe` program. You'll have to debug that program. The Powershell problem you had (calling a program) is fixed. At first glance I would say that your program expects 2 parameters (`ConfigFile` and `Key`)and you are only giving one. – MonkeyDreamzzz May 19 '15 at 12:30