I_N3RD
I_N3RD

Reputation: 33

Variables, strings and increasing frustration

I'm pretty new to C# and am having a mare trying to get what should be a simple task to work, in a nutshell I've written a PowerShell script to create VApps within a vSphere environment, the PoSh script works perfectly, next I have created (my first go) a Windows Console Application to run (initially) this script with user input, here's the problem, within my console app I'm using Process.Start to call my PoSh script and pass parameters, but, they come out joined up and completely missing the last parameter, here's the line in question:

Process.Start("Powershell.exe","-ExecutionPolicy bypass F:\\hello.ps1 -Location " + location + " -AppName" + appname);

AppName is completely ignored and Location tends to come out as -Locationanywhere instead of -Location Anywhere, I'm sure it's something basic and I've trawled the usual group and RTFM but no joy!

Hello.ps1 is a test script that just records the parameters passed to it so I can check the output before touching my real script.

Any help gratefully received.

Upvotes: 3

Views: 136

Answers (3)

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

You're lacking a space between -AppName and the double quotes.

string.Format is a useful method in .Net - it allows you to easily replace placeholders with dynamic content in a way that makes viewing the 'complete' string intuitive:

string parameters = string.Format("-ExecutionPolicy bypass F:\\hello.ps1 -Location {0} -AppName {1}", location, appName);
Process.Start("Powershell.exe", parameters);

Upvotes: 6

myermian
myermian

Reputation: 32515

Might I suggest using String.Format() instead of using the + operator?

String.Format("-ExecutionPolicy bypass F:\\hello.ps1 -Location {0} -AppName {1}", location, appname)

Upvotes: 2

Mario Corchero
Mario Corchero

Reputation: 5575

I'm not sure, but I think you need an space between -AppName and the appname

" -AppName " + appname

It's all I can help you :(

Upvotes: 3

Related Questions