0

I am trying to execute a Powershell custom function from c# using Start.Proccess,

I know that we should use Powershell class in order to execute powershell scripts, but I am having issues with x86, x64 platforms because I am calling the Powershell script in a Custom Action that is used by a Setup project.

So Could you tell me how to call a Powershell custom function using Start.Process??

In powershell you need to load the script first where the function is placed with ". .[scriptPath]" and then call the function, but In C# I don't know how to do it.

Javier Hertfelder
  • 2,432
  • 4
  • 22
  • 36
  • 1
    What have you tried? What issue are you having? At the moment I can't see a question here. – Richard Jan 02 '13 at 11:27
  • Have you tried using `c:\windows\System32\WindowsPowerShell\v1.0\powershell.exe` with `-Command {Import-Module .\script.ps1; functionname;}` ? – Frode F. Jan 02 '13 at 13:40
  • You do know that PowerShell is not installed by default on XP and Vista and on Server 2008 it is optional? Usually you want to minimize pre-reqs (and PowerShell is big one) for the installer. Can we assume the installation requires PowerShell before it can proceed? – Keith Hill Jan 02 '13 at 23:10
  • Hi, the environment where I am going to execute this script has Powershell installed as a prerequisite so I don't have that problem. Thanks anyway. – Javier Hertfelder Jan 03 '13 at 08:58

1 Answers1

0

If you have control of the function script, just invoke the function from the script which makes it easier to invoke from PowerShell.exe e.g.:

-- script.ps1 --
function DoAction($p1, $p2) {
    ...
}

DoAction someArg anotherArg

Then invoke the script like so:

string sysdir = Environment.GetFolderPath(Environment.SpecialFolderPath.System);
Process.Start(sysdir + "\WindowsPowerShell\v1.0\Powershell.exe", "-Command \"& {" + <path-to-script> + "}\"");
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Thanks for the answer, but I have already tried that, the problem is that I get the values for the argument List of the function in my c# code, so I need to pass them from there.. – Javier Hertfelder Jan 03 '13 at 08:55
  • For help with that see http://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments – Keith Hill Jan 03 '13 at 17:07