2017-05-12 57 views
1

之外我有一个文件夹选择对话框启动脚本,但是据我所知,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 
+1

所以你调用一个脚本(拍摄的)并应该调用脚本二号呢?分割成单独的文件有什么意义? – restless1987

+1

我同意restless1987,不明白为什么要进行拆分文件夹选择到它自己的脚本。个人而言,我只是有我放到需要它来执行文件夹选择对话框中的脚本功能,但它可以很容易被我点源加载功能,其自身的.ps1文件。我也很好奇你为什么要这样调用PowerShell并强制STA加载脚本。 – TheMadTechnician

+0

我现在无法找到该网页,但我看到关于我是有问题的论坛。一切都很好,直到我意识到原始脚本根本不会从探险家那里运行,只能从ISE内部运行。有人提到这是STA vs MTA(无头绪)的问题,然后建议创建第二个脚本来点源。这工作。除了我很快意识到它没有错误处理。我很想只有一个脚本。但是我怎样才能从ISE以外的地方开展工作呢? – OatMaGoat

回答

0

保持只是你在你的第二个脚本函数,点源文件来加载功能,然后将$folder = Select-FolderDialog呼叫你的主脚本,一拉:

"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") 
} 
+0

看来,这种调用脚本的方法无法正常工作(至少对于我的版本)。它只是启动一个powershell.exe窗口与开幕消息,但不是文件夹选择器。我已经尝试过好几次了。 – OatMaGoat

+0

它看起来像try/catch是失败点。我注意到catch主机消息从不出现。即使该功能被取消,也不会说“用户取消了操作”。所以我删除它只是为了看看会发生什么,我只是离开了点源和主机消息,它的运行方式与try/catch完全相同。 – OatMaGoat

相关问题