2016-04-14 20 views
0

Bazı ps komut dosyası PowerShell ISE env ile Tamam çalıştırıyor.powershell script.exe kayboluyor

$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.") 
} 

}) 

Sonra ben .exe olarak PowerGUI yoluyla derlemek Ve çalıştırmak, ilk, bir cmd açılır ve kaybolur bir saniye sonra, ben bile oluşturduk benim powershell formunu göremiyorum kod aracılığıyla. PowerGui aracılığıyla farklı seçenekler denedim (bir cmd penceresi, sadece formumun görüntülenmesini istemiyorum). Bunu .exe'ye nasıl derleyeceğimizi ve arkasında sadece form ve mantığı nasıl göreceğinizi biliyor musunuz? Teşekkürler!

cevap

1

Komut dosyasını Powershell'de exe olarak derlemek, form bileşenlerini yüklemeniz gereken yeni bir örnekte çalıştırır. Senaryonuzun başlamasından bu eklemeyi deneyin: Bir kenara

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

- PowerGUI bu gibi sorunları teşhis etmek, sadece "Harici PowerShell Pencerede Çalıştır" butonuna tıklayın - o zaman için hata kodları atacağım O pencerede sen. – Scepticalist