从c#应用程序调用azure powershell cmdlet失败

从c#应用程序调用azure powershell cmdlet失败,c#,powershell,azure,cmdlets,C#,Powershell,Azure,Cmdlets,我正在尝试自动化部署到azure云的过程。我的powershell脚本就是这样做的,当从azure powershell命令行执行它时,它就像一个符咒。当我试图从c#应用程序调用相同的脚本时,它失败了 这是我的代码: internal void RunPowerShellScript(string scriptPath, Dictionary<string, string> arguments) { RunspaceConfiguration runsp

我正在尝试自动化部署到azure云的过程。我的powershell脚本就是这样做的,当从azure powershell命令行执行它时,它就像一个符咒。当我试图从c#应用程序调用相同的脚本时,它失败了

这是我的代码:

  internal  void RunPowerShellScript(string scriptPath, Dictionary<string, string> arguments)
    {
        RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
        runspace.Open();
        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        Pipeline pipeline = runspace.CreatePipeline();
        //Here's how you add a new script with arguments            
        Command myCommand = new Command(scriptPath, true);
      foreach (var argument in arguments)
        {
            myCommand.Parameters.Add(new CommandParameter(argument.Key, argument.Value));
        }            
        pipeline.Commands.Add(myCommand);
        var results = pipeline.Invoke();
        foreach (var psObject in results)
        {
            _view.PrintOutput(psObject.ToString());
        }
    }

我相信您传递publishsettings文件的方式是造成问题的原因。当传递publishsettings文件时,必须添加引号,如果没有引号,则会出现异常

以下是基于您的代码的代码,我通过使用(\”)传递带引号的publishsetting文件对其进行了测试,它运行良好:

private void button1_Click(object sender, EventArgs e)
{
  Dictionary<string, string> myDict= new Dictionary<string, string>();
  myDict.Add("-subscriptionDataFile", "\"C:\\InstallBox\\asc.publishsettings\"");
  RunPowerShellScript("C:\\InstallBox\\testcode.ps1", myDict);
}

internal void RunPowerShellScript(string scriptPath, Dictionary<string, string> arguments)
 {
   RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
   Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
   runspace.Open();
   RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
   Pipeline pipeline = runspace.CreatePipeline();
   //Here's how you add a new script with arguments            
   Command myCommand = new Command(scriptPath);
   foreach (var argument in arguments)
   {
      myCommand.Parameters.Add(new CommandParameter(argument.Key, argument.Value));
   }
   pipeline.Commands.Add(myCommand);
   var results = pipeline.Invoke();
   foreach (var psObject in results)
    {
      .........
    }
   }
为了确保我能看到您的问题,如果我将dictionary key-value对更改为如下(不带引号),我会得到与您描述的完全相同的错误:

myDict.Add("-subscriptionDataFile", "C:\\InstallBox\\asc.publishsettings");
因此,传递PublishSettings文件的正确方法如下:

Param(  [string]$subscriptionDataFile)
Import-Module Azure
Import-AzurePublishSettingsFile subscriptionDataFile
Get-AzureSubscription
myDict.Add("-subscriptionDataFile", "\"C:\\InstallBox\\asc.publishsettings\"");

你确定你在参数字典中传递了
subscriptionDataFile
,并且有这个参数的正确的键/值吗?是的,我确定,我从命令行运行了脚本并成功地进行了部署,同样的参数我传递到字典中…第二天在这个问题上储存了。谢谢你的回复,我更正了字典中的值没有如您所建议的那样,但仍然出现相同的错误。您的脚本在subscriptionDataFile前面缺少一个美元符号,实际上参数没有得到任何值。我尝试输出值,但没有得到任何结果:写入输出$subscriptionDataFile即使为脚本中的参数提供值,也会失败,并出现错误:“无法验证参数“Status”上的参数。”。参数为null或为空。请提供一个不为null或空的参数,然后重试该命令。“Ion,如果您在“command myCommand=new command(scriptPath)”中将第二个参数作为TRUE传递,您将看到写入输出将不起作用。因此,请尝试这样做。在我的机器上,上述脚本确实起作用。我不确定在您的情况下,它为什么不起作用。
myDict.Add("-subscriptionDataFile", "\"C:\\InstallBox\\asc.publishsettings\"");