之外我有一个文件夹选择对话框启动脚本,但是据我所知,POSH不能ISE像(STA与MTA)外执行脚本,所以我有一个单独的脚本点源它。文件夹选取器不加载ISE
我在的情况下,用户按下取消的第一个脚本错误处理:
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
我不知道为什么它生成错误消息引用其他脚本,因为其他脚本运行良好(从ISE当然)。
所需的输出:
如果用户取消文件夹选择器,我只是想要一个干净的错误消息显示:
用户取消了操作。
按任意键继续。
编辑
这里是文件夹选择器脚本我有。它在ISE中工作正常,但是当您右键单击并选择Run with Powershell时,它只会启动一个空白提示窗口。为防止最终用户意外编辑脚本,我希望它能从ISE外部运行。顺便说一句,我使用POSH 2.
# Function for folder picker dialog
Function Select-FolderDialog
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$objForm = New-Object System.Windows.Forms.FolderBrowserDialog
# Default location is script's location
$objForm.SelectedPath = (Resolve-Path .\).Path
$objForm.Description = "Select Folder"
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{Return $objForm.SelectedPath}
Else
{
Write-Error "Operation cancelled by user."
Exit
}
}
$folder = Select-FolderDialog #contains user's selected folder
所以你调用一个脚本(拍摄的)并应该调用脚本二号呢?分割成单独的文件有什么意义? – restless1987
我同意restless1987,不明白为什么要进行拆分文件夹选择到它自己的脚本。个人而言,我只是有我放到需要它来执行文件夹选择对话框中的脚本功能,但它可以很容易被我点源加载功能,其自身的.ps1文件。我也很好奇你为什么要这样调用PowerShell并强制STA加载脚本。 – TheMadTechnician
我现在无法找到该网页,但我看到关于我是有问题的论坛。一切都很好,直到我意识到原始脚本根本不会从探险家那里运行,只能从ISE内部运行。有人提到这是STA vs MTA(无头绪)的问题,然后建议创建第二个脚本来点源。这工作。除了我很快意识到它没有错误处理。我很想只有一个脚本。但是我怎样才能从ISE以外的地方开展工作呢? – OatMaGoat