简体   繁体   中英

Passing a string array from powershell to C#

I would like to call a C# program from powershell and pass an array of parameters from powershell to the C# program. The C# program takes a file path, date, and file extension and manipulates those files.

In the powershell script I have, I store all the strings inside an array of strings and pass that string using the -argumentlist switch in start-process .

Since these file paths are at my work I am going to make up some file paths for an example. The name of this script is ProductionImportCaller

$ParametersToPass = "C:\my images,20130412,dat", "C:\my images\curiosity,20130412,bif"

Start-Process -FilePath C:\Users\ABetterProductionImport.exe 
-ArgumentList $ParametersToPass

The above does not have a new line in my script. I just make it a newline on here so it is easier to read.

When I call .\\ProductionImportCaller the array that is passed is:

c:\my
images,20130412,dat  

c:\my
images\curiosity,20130412,bif

which I do not want.

So here is my question: How do I write my powershell script so these parameters will pass correctly?

I have tried

  1. Replacing the double quotes with single quotes
  2. Surrounding each parameter with double quotes and surrounding the file path with singe quotes

This works if I call:

start-process .\ABetterProductionImport.exe "C:\my images,20130412,dat"

EDIT: But if I call the same command from a script that I wrote this does not work. So again, how do I write a powershell script to make this run correctly?

Found the answer. If I call

.\ABetterProductionImport.exe $ParametersToPass 

exactly with no start-process or anything it works. Which is good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM