简体   繁体   English

将字符串数组从Powershell传递到C#

[英]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. 我想从powershell调用C#程序,并将参数数组从powershell传递到C#程序。 The C# program takes a file path, date, and file extension and manipulates those files. C#程序采用文件路径,日期和文件扩展名并处理这些文件。

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 . 在我拥有的powershell脚本中,我将所有字符串存储在字符串数组中,并在start-process使用-argumentlist开关传递该字符串。

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 该脚本的名称是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: 当我调用.\\ProductionImportCaller ,传递的数组为:

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? 所以这是我的问题:如何编写Powershell脚本,以便这些参数正确传递?

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? 再说一遍,如何编写Powershell脚本以使其正确运行?

Found the answer. 找到了答案。 If I call 如果我打电话

.\ABetterProductionImport.exe $ParametersToPass 

exactly with no start-process or anything it works. 完全没有启动过程或任何有效的方法。 Which is good. 哪个好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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