无法从 powershell 调用 dotnet 方法

Unable to call dotnet methods from powershell

我正在尝试调用 System.Diagnostics.Process 的 Start 方法。我已经在互联网上看到许多其他示例完全相同但是当我 运行 我的代码时:

$process = new-object System.Diagnostics.Process

$config.variables.properties | foreach {
  $process.StartInfo.EnvironmentVariables.Set_Item($_.name, $_.value)
}

$process.StartInfo.UseShellExecute = false;
$process.StartInfo.FileName = "C:\Program Files\IIS Express\iisexpress.exe"
$process.StartInfo.Arguments = "/config:$configPath${name}ApplicationHost.config \site:$name"

$process.Start() 

我收到这个毫无意义的错误:

Exception calling "Start" with "0" argument(s): "The parameter is incorrect"
At C:\Users\critc\Source\run-iisexpress.ps1:67 char:1
+ $started = $process.Start() | Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : Win32Exception

此方法有一个 0 参数重载。事实上,如果我从调用 powershell 中删除括号,它的无限智慧告诉我有一个零参数重载

OverloadDefinitions
-------------------
bool Start()

Powershell 在骗我!最初我正在创建一个 ProcessStartInfo 实例并试图将它传递给静态 Process.Start 方法并且我得到了相同的错误(除了它说 with "1" argument(s)

更新

这是我更新后的有效代码。

$process = new-object System.Diagnostics.Process

Get-Member -inputObject $config.variables -memberType Properties | foreach {
  $value = $config.variables | select -exp $_.name
  $process.StartInfo.EnvironmentVariables.Set_Item($_.name, $value)
}

$process.StartInfo.UseShellExecute = $false
$process.StartInfo.FileName = "C:\Program Files\IIS Express\iisexpress.exe"
$process.StartInfo.Arguments = "/config:`"$configPath${name}ApplicationHost.config`" /site:$name"

$started = $process.Start()
if ($started) {
  $process.WaitForExit()
}

有些东西告诉我你的参数不正确,你在 $configPath 中有一个 space。但这只是一种预感……如果您在问题中提供 $configPath$name 的值会更好。

如果你使用会发生什么:

$process.StartInfo.Arguments = "/config:`"$configPath${name}ApplicationHost.config`" /site:`"$name`""