2017-05-12 53 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

相關問題