【问题标题】:Powershell: Windows Server 2008 folder browser dialog blank, but not in ISEPowershell:Windows Server 2008 文件夹浏览器对话框空白,但不在 ISE 中
【发布时间】:2016-07-12 14:21:17
【问题描述】:

当我相信在 Windows 更新后,我几乎完全完成了我的 powershell 脚本时,我的对话框只是变灰了。理想情况下,这意味着打开一个对话框,用户可以在其中选择文件路径进行映射。它在服务器 2012 上运行良好。

奇怪的是它完全可以在powershell ISE中运行,但是当脚本只是通过批处理文件执行时,它就不再起作用了。这是脚本中的代码块:

Function Get-Folder($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.rootfolder = "MyComputer"

    if($foldername.ShowDialog() -eq "OK")
    {
        $folder += $foldername.SelectedPath
    }
    return $folder
}

这是bat文件中的执行代码:

start "" /d "C:\ASD\scripts" PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\ASD\scripts\PBridge.ps1""' -Verb RunAs}"

我真的不确定出了什么问题,但我倾向于通过批处理文件执行 powershell 脚本的参数,因为它在 ISE 中工作。

【问题讨论】:

  • 在您的执行代码中,您似乎出于某种原因调用了 PowerShell 两次?这是故意的吗?
  • 它不是那样执行的,它执行一次迭代。第一个启动命令在与批处理文件分开的新窗口中启动 powershell,我相信该命令为 powershell 设置了参数,但不会再次启动它。

标签: windows powershell batch-file dialog


【解决方案1】:

根据您运行的 Powershell 版本,这可能是因为 ISE 在单线程单元中运行,而标准 powershell.exe 默认为多线程单元,并且需要单线程单元对于许多(全部?)GUI 任务。假设是这种情况,您应该能够通过添加-STA 来解决问题。除了您用于启动脚本的代码看起来过于复杂之外,下面的代码应该可以工作并且更易于阅读。

Powershell.exe -STA -NoProfile -ExecutionPolicy Bypass -File "C:\ASD\scripts\PBridge.ps1" -Verb RunAs

【讨论】:

  • 非常感谢!那行得通。我会做进一步的测试以确保现在一切正常,但对话框是固定的!
猜你喜欢
  • 2010-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
相关资源
最近更新 更多