简体   繁体   中英

Powershell: Windows Server 2008 folder browser dialog blank, but not in ISE

I was almost finished completely with my powershell script when after a windows update I believe, my dialog box just went grey. Ideally, it is meant to open a dialog box where the user selects a file path for mapping. It works fine on server 2012.

The weird thing is that it completely works in the powershell ISE, but when the script is simply executed via batch file, it no longer works. Here is the code chunk from the script:

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
}

Here is the execution code from the bat file:

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}"

I'm really not sure what is wrong, but I am leaning toward an argument in the execution of the powershell script by the batch file since it works in the ISE.

Depending on which version of Powershell you are running this could be caused by the fact that ISE runs in a single-threaded apartment while the standard powershell.exe defaults to a multi-threaded apartment, and the single-threaded apartment is required for many (all?) GUI tasks. Assuming that's the case you should be able to fix the problem by adding a -STA . Beyond that the code you are using to start the script looks much too complex, the below should work and be a lot simpler to read.

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

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