Mitch
Mitch

Reputation: 118

Accessing Powershell Arguments in c#

I have a simple c# console application that i am running through a Powershell ps1 file. I would like to take the argument passed in through $args[0] when the script is called, and use that in a method i have in my c# application. I have found a lot for the reverse, but nothing to helpful for what i am trying to do. Any input would be much appreciated. This is roughly what i have tried recently. Thank you!

$filePath = $args[0]
$Check = New-Object -ComObject "Take c# file and try to make into    object"
$Check.hasFile($filePath)
Start-Process -FilePath "Path to c# exe"

.hasFile is the c# method that i am trying to pass the Powershell argument to.

Upvotes: 0

Views: 608

Answers (1)

AHowgego
AHowgego

Reputation: 614

If I'm understanding you correctly, you just want to pass your script variable as an argument into your C# application? If so, you can just make the Main method of your Program.cs accept an array of strings, and then pass your script variables to Start-Process when you launch the application:

$myargs = @($args[0],$myvariable, .... <whatever>)
Start-Process MyCSharpApplication.exe $myargs

Hope this helps!

EDIT:

To clarify, in the simple case that you just have one variable you want to pass in, you don't even need the separate array variable. You can just do:

Start-Process MyCSharpApplication.exe $myvariable

Upvotes: 1

Related Questions