Powershell 文件夹选择器未加载到ISE外部

Powershell 文件夹选择器未加载到ISE外部,powershell,Powershell,我有一个以文件夹选择器对话框开始的脚本,但是我知道POSH不能像那样在ISE之外执行脚本(STA和MTA),所以我有一个单独的脚本来点源它 如果用户按“取消”,我在第一个脚本中有错误处理: if ($Show -eq "OK") { return $objForm.SelectedPath } else { Write-Error "Operation cancelled by user." exit } 现在我需要第二个脚本(调用第一个脚本的脚本)来检测相同的取消 到

我有一个以文件夹选择器对话框开始的脚本,但是我知道POSH不能像那样在ISE之外执行脚本(STA和MTA),所以我有一个单独的脚本来点源它

如果用户按“取消”,我在第一个脚本中有错误处理:

if ($Show -eq "OK") {
    return $objForm.SelectedPath
} else {
    Write-Error "Operation cancelled by user."
    exit
}
现在我需要第二个脚本(调用第一个脚本的脚本)来检测相同的取消

到目前为止,我得到的是:

"Choose a folder containing the items to be moved..."
""
try {
    powershell -STA -File "C:\Test\Script.ps1"
    ""
    "Operation completed. An event log has been created:"
    (Resolve-Path .\).Path +"\log.txt"
    ""
    "Press any key to continue..."
    ""
    $x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
} catch {
    if ($LastExitCode -ne 0) { exit $LastExitCode }
    Write-Host "User cancelled the operation."
    ""
    "Press any key to continue..."
    ""
    $x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
这给了我一个看起来很糟糕的红色文本多行写入错误异常

At C:\Test\Script.ps1:27 char:30 + $folder = Select-FolderDialog <<<< #contains user's selected folder + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Select-FolderDialog
在第二个脚本中只保留函数,点源文件以加载函数,然后将
$folder=Select FolderDialog
调用放入主脚本,即la:

"Choose a folder containing the items to be moved..."
""
try {
    . "C:\Test\Script.ps1" # this dot sources the function from the second file (no need to call a separate Powershell process to do this, as data from that process won't be returned to the primary process calling it)

    $folder = Select-FolderDialog # this runs the function in your original script, storing the result in the $folder variable

    ""
    "Operation completed. An event log has been created:"
    (Resolve-Path .\).Path +"\log.txt"
    ""
    "Press any key to continue..."
    ""
    $x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
} catch {
    if ($LastExitCode -ne 0) { exit $LastExitCode }
    Write-Host "User cancelled the operation."
    ""
    "Press any key to continue..."
    ""
    $x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

在第二个脚本中只保留函数,点源文件以加载函数,然后将
$folder=Select FolderDialog
调用放入主脚本,即la:

"Choose a folder containing the items to be moved..."
""
try {
    . "C:\Test\Script.ps1" # this dot sources the function from the second file (no need to call a separate Powershell process to do this, as data from that process won't be returned to the primary process calling it)

    $folder = Select-FolderDialog # this runs the function in your original script, storing the result in the $folder variable

    ""
    "Operation completed. An event log has been created:"
    (Resolve-Path .\).Path +"\log.txt"
    ""
    "Press any key to continue..."
    ""
    $x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
} catch {
    if ($LastExitCode -ne 0) { exit $LastExitCode }
    Write-Host "User cancelled the operation."
    ""
    "Press any key to continue..."
    ""
    $x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

所以你叫脚本一(镜头一),那应该叫脚本二?把文件夹分割成不同的文件有什么意义?我同意你的观点,但不明白你为什么要把文件夹分割成自己的脚本。就我个人而言,我只是有一个函数,我把它放入脚本中,需要它来执行文件夹选择对话框,但它也可以是它自己的.ps1文件,我点源代码来加载该函数。我也很好奇,为什么你这样调用PowerShell并强迫STA加载脚本。我现在找不到该页面,但我看到了一个关于我遇到的问题的论坛。一切都很好,直到我意识到原来的脚本根本不会从explorer运行,只能从ISE内部运行。有人提到这是STA vs MTA的问题(没有线索),然后建议创建第二个脚本来点源代码。成功了。但我很快意识到它没有错误处理。我希望只有一个剧本。但我如何让它在ISE之外工作呢?我更新了我原来的帖子。请看上面写着“编辑”的地方。所以你叫脚本一(镜头一),那应该叫脚本二?把文件夹分割成不同的文件有什么意义?我同意你的观点,但不明白你为什么要把文件夹分割成自己的脚本。就我个人而言,我只是有一个函数,我把它放入脚本中,需要它来执行文件夹选择对话框,但它也可以是它自己的.ps1文件,我点源代码来加载该函数。我也很好奇,为什么你这样调用PowerShell并强迫STA加载脚本。我现在找不到该页面,但我看到了一个关于我遇到的问题的论坛。一切都很好,直到我意识到原来的脚本根本不会从explorer运行,只能从ISE内部运行。有人提到这是STA vs MTA的问题(没有线索),然后建议创建第二个脚本来点源代码。成功了。但我很快意识到它没有错误处理。我希望只有一个剧本。但我如何让它在ISE之外工作呢?我更新了我原来的帖子。请查看其中的“编辑”字样。这种调用脚本的方法似乎无法正常工作(至少在我的版本中是这样)。它只启动一个powershell.exe窗口,其中包含打开消息,但不包含文件夹选择器。我已经试过好几次了。看来try/catch是失败点。我注意到catch host消息从未出现。即使函数被取消,它也不会说“用户取消了操作”。所以我删除它只是为了看看会发生什么,我只留下了点源和主机消息,它的操作与try/catch完全相同。似乎这种调用脚本的方法无法正常工作(至少对于我的版本)。它只启动一个powershell.exe窗口,其中包含打开消息,但不包含文件夹选择器。我已经试过好几次了。看来try/catch是失败点。我注意到catch host消息从未出现。即使函数被取消,它也不会说“用户取消了操作”。所以我删除它只是为了看看会发生什么,我只留下了点源和主机消息,它的操作与try/catch完全相同。