powershell script.exe正在消失

时间:2016-04-14 11:42:04

标签: powershell

我有一些ps脚本可以通过PowerShell ISE env。

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

})

然后,我通过PowerGUI将其编译为.exe 我运行它,首先,它打开一个cmd,一秒钟后它就消失了,我甚至看不到我通过代码创建的powershell表单。 我通过PowerGui尝试了不同的选项(我希望不显示cmd窗口,只显示我的表单)。你知道如何将它编译成.exe并只看到它背后的形式和逻辑吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

在Powershell中将脚本编译为exe在新实例中运行它,您需要在其中加载表单组件。尝试将其添加到脚本的开头:

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