2016-04-14 3 views
0

Ich habe einige PS-Skript, das läuft OK über PowerShell ISE env.Powershell script.exe verschwindet

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Data Entry Form" 
$objForm.Size = New-Object System.Drawing.Size(600,400) 
$objForm.StartPosition = "CenterScreen" 
$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(50,50) 
$objTextBox.Size = New-Object System.Drawing.Size(150,20) 
$findLabel = New-Object System.Windows.Forms.Label 
$findLabel.Location = New-Object System.Drawing.Size(50,100) 
$findLabel.Size = New-Object System.Drawing.Size(300,20) 
$findLabel.Text = "Nađi:" 
$findBox = New-Object System.Windows.Forms.TextBox 
$findBox.Location = New-Object System.Drawing.Size(50,120) 
$findBox.Size = New-Object System.Drawing.Size(260,20) 
$replaceLabel = New-Object System.Windows.Forms.Label 
$replaceLabel.Location = New-Object System.Drawing.Size(50,180) 
$replaceLabel.Size = New-Object System.Drawing.Size(300,20) 
$replaceLabel.Text = "Zameni sa:" 
$replaceBox = New-Object System.Windows.Forms.TextBox 
$replaceBox.Location = New-Object System.Drawing.Size(50,200) 
$replaceBox.Size = New-Object System.Drawing.Size(260,20) 


$objForm.Controls.Add($startButton) 
$objForm.Controls.Add($findBox) 
$objForm.Controls.Add($replaceBox) 
$objForm.Controls.Add($objTextBox) 
$objForm.Controls.Add($beginScriptButton) 
$objForm.Controls.Add($findLabel) 
$objForm.Controls.Add($replaceLabel) 
$objForm.Topmost = $True 
$objForm.Add_Shown({$objForm.Activate()}) 
[void] $objForm.ShowDialog() 

$startButton = New-Object System.Windows.Forms.Button 
$startButton.Location = New-Object System.Drawing.Size(220,50) 
$startButton.Size = New-Object System.Drawing.Size(75,23) 
$startButton.Text = "Browse!" 
$startButton.Add_Click({ 
$browser = New-Object System.Windows.Forms.FolderBrowserDialog 
$browser.Location = New-Object System.Drawing.Size(60,60) 
$null = $browser.ShowDialog() 
$path = $browser.SelectedPath 
$objTextBox.Text = $path 
}) 

$beginScriptButton = New-Object System.Windows.Forms.Button 
$beginScriptButton.Location = New-Object System.Drawing.Size(350,130) 
$beginScriptButton.Size = New-Object System.Drawing.Size(350,180) 
$beginScriptButton.Text = "Begin" 
$beginScriptButton.Add_Click({ 
$a = $objTextBox.Text 
if(($a) -and ($findBox.Text) -and ($replaceBox.Text)){ 
$objWord = New-Object -comobject Word.Application 
$objWord.Visible = $false 

$list = Get-ChildItem "c:\users\stefan\test\*.*" -Include *.doc* 
foreach($item in $list){ 
$objDoc = $objWord.Documents.Open($item.FullName,$true) 

$objSelection = $objWord.Selection 
$wdFindContinue = 1 
$FindText = $findBox.Text 
$MatchCase = $false 
$MatchWholeWord = $true 
$MatchWildcards = $False 
$MatchSoundsLike = $False 
$MatchAllWordForms = $False 
$Forward = $True 
$Wrap = $wdFindContinue 
$Format = $False 
$wdReplaceNone = 0 
$ReplaceWith = $replaceBox.Text 
$wdFindContinue = 1 
$ReplaceAll = 2 

$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ` 
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,` 
$Wrap,$Format,$ReplaceWith,$ReplaceAll) 
$objDoc.Save() 
$objDoc.Close() 
} 
$objWord.Quit() 
[System.Windows.Forms.MessageBox]::Show("Uspešno ste izvršili izmenu!") 
} 
else{ 
[System.Windows.Forms.MessageBox]::Show("Please fill in all fields.") 
} 

}) 

Dann habe ich es über PowerGUI kompilieren als .exe Und ich laufe es, zuerst, öffnet sich ein cmd und nach einer Sekunde verschwinden, kann ich nicht einmal meine Powershell-Form sehen, die ich erstellt habe durch den Code. Ich habe verschiedene Möglichkeiten über PowerGui versucht (ich möchte nicht ein cmd-Fenster angezeigt werden, nur mein Formular). Weißt du, wie man es zu .exe kompiliert und nur Form und Logik dahinter sieht? Danke!

Antwort

1

Wenn Sie das Skript als eine Exe in Powershell kompilieren, wird es in einer neuen Instanz ausgeführt, in der Sie die Formularkomponenten laden müssen. Versuchen Sie, dies zu Beginn des Skriptes:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
+0

Als Nebenwirkung - zu diagnostizieren Fragen wie diese auf PowerGUI, klicken Sie einfach auf die Schaltfläche „Ausführen in einem externen Powershell-Fenster“ - es wird dann die Fehlercodes wirft für du in diesem Fenster. – Scepticalist