passing parameters to powershell from c#

后端 未结 1 794
夕颜
夕颜 2021-01-15 03:09

I\'m trying to pass a parameter to PowerShell from C# web app, but keep getting an error:

Reason = {\"The term \'Param($ds)\\r\\n\\r\\n$ds\\r\\n\\r\\n

相关标签:
1条回答
  • 2021-01-15 03:21

    Only scriptblocks, ps1/psm1 files and functions/filters can have a param block. The script you should be adding with AddScript should be of the form:

    & { param($ds); $ds }
    

    & is the call operator. In your example, you are trying to execute param as a command.

    update

    You must pass the arguments to the scriptblock like so:

    & { param($ds); $ds } 42

    This results in 42 being passesd to the scriptblock. Sticking script in with AddScript doesn't implicitly create a ps1 file. It's akin to you typing:

    ps c:\> param($ds); $ds
    

    ...directly at the prompt; this is meaningless. Make sense?

    -Oisin

    0 讨论(0)
提交回复
热议问题