Powershell GUI - 不调用子批处理文件

Powershell GUI - 不调用子批处理文件

关于带有 GUI 的 Powershell 脚本:
当编译为.exe(没有Console 的PS2GUI)时它可以正常工作,但是当.exe从调用它时.bat,它不起作用:

  • 它不会启动Start-Process下面的批处理文件;问题是我无法.exe直接从基于 Eclipse 的启动器调用该文件,这就是为什么我们需要一个.bat来启动它。
  • 我曾尝试start script.exe.bat
  • 我尝试过直接调用.ps1,但是刷新计时器不起作用,并且检查应用程序和子批处理文件是否已在运行也不起作用。

我怎样才能使这些东西正常工作?

Powershell 脚本:

#Date format for Logfile
$heute        = Get-Date -UFormat %d_%m_%Y_%R
$LogFile      = "c:\temp\output.txt"
$TempLocation = "c:\temp\startscript"

#Kill already started startscript
Get-Process -Name noconsole_exe | where { $_.Id -ne $PID } | Stop-Process

#Kill already started project.bat
$fltr = "name like '%cmd.exe%' and commandline like '%project.bat%'"
(Get-WmiObject Win32_Process -Filter $fltr).Terminate()

# Percentage
Function get-count {
  $Percent.text = (gci $TempLocation).count * 10
  $form.refresh()
}

$timer1           = New-Object System.Windows.Forms.Timer
$timer1.Enabled   = $true
$timer1.Start()
$timer1.Interval  = 100

#form
$timer1.add_tick({get-count})

#GUI Form
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                           = New-Object system.Windows.Forms.Form
$Form.ClientSize                = '600,200'
$Form.text                      = "Start"
$Form.BackColor                 = "#ffffff"
$Form.TopMost                   = $false
$Form.startposition             = "centerscreen"
$Form.icon                      = "c:\startscripts\startscript.ico"

$Label1                         = New-Object system.Windows.Forms.Label
$Label1.text                    = "Program is starting, please wait."
$Label1.AutoSize                = $true
$Label1.width                   = 25
$Label1.height                  = 10
$Label1.location                = New-Object System.Drawing.Point(131,28)
$Label1.Font                    = 'Microsoft Sans Serif,16'

$Label2                         = New-Object system.Windows.Forms.Label
$Label2.text                    = "Please include the log below"
$Label2.AutoSize                = $true
$Label2.width                   = 25
$Label2.height                  = 10
$Label2.location                = New-Object System.Drawing.Point(50,109)
$Label2.Font                    = 'Microsoft Sans Serif,10'

$ToolTip1                       = New-Object system.Windows.Forms.ToolTip
$ToolTip1.ToolTipTitle          = "Log File Location: c:\temp\output.txt"

$Button2                        = New-Object system.Windows.Forms.Button
$Button2.text                   = "Send Log via E-Mail"
$Button2.width                  = 173
$Button2.height                 = 30
$Button2.location               = New-Object System.Drawing.Point(210,142)
$Button2.Font                   = 'Microsoft Sans Serif,10'

$ToolTip2                       = New-Object system.Windows.Forms.ToolTip
$ToolTip2.ToolTipTitle          = "Send Log via E-Mail"

$Label3                         = New-Object system.Windows.Forms.Label
$Label3.text                    = "%"
$Label3.AutoSize                = $true
$Label3.width                   = 25
$Label3.height                  = 10
$Label3.location                = New-Object System.Drawing.Point(317,61)
$Label3.Font                    = 'Microsoft Sans Serif,16'

$Percent                        = New-Object system.Windows.Forms.Label
$Percent.Text                   = (gci $TempLocation).count * 10
$Percent.AutoSize               = $true
$Percent.width                  = 30
$Percent.height                 = 12
$Percent.location               = New-Object System.Drawing.Point(276,61)
$Percent.Font                   = 'Microsoft Sans Serif,16'

$ToolTip2.SetToolTip($Button2,'Opens Outlook with the log file. Please include also a screenshot of an error message and send the e-mail.')
$Form.controls.AddRange(@($Label1,$Label2,$Button2,$Label3,$Percent))

$Button2.Add_Click({ OpenMailLog })

function OpenLog { Start-Process notepad $LogFile }

function append-text {
  process{
    foreach-object {$_ + "%0d%0a"}
  }
}

function OpenMailLog {
  Copy-Item "c:\temp\output.txt" -Destination "c:\temp\output_4_mail.txt"

  $MailText = Get-Content -path "c:\temp\output.txt"
  $MailText | append-text | out-file "c:\temp\output_4_mail.txt"
  $AntiText = Get-Content -path "c:\temp\output_4_mail.txt"

  $Output = @()

  #Add new text
  $Output += "Dear Mr.X,%0d%0ahere is the log file output.%0d%0a%0d%0a"

  #Append old text from content
  $Output += $AntiText
  $Output | Out-file "c:\temp\output_4_mail.txt"
  $BodyText = Get-Content -path "c:\temp\output_4_mail.txt"
  Start-Process "mailto:[email protected]?Subject=Log file&Body=$BodyText"
}

Start-Process -FilePath "c:\startscripts\project.bat" -RedirectStandardOutput "$LogFile" -WindowStyle Hidden
[void]$Form.ShowDialog()

答案1

这...

$heute = Get-Date -UFormat %d_%m_%Y_%R

... 在您的代码中从未在任何地方使用过,那么为什么要声明它。

通过 PowerShell 运行外部命令必须遵循规定的实现。如下所示:

使用 PowerShell 和外部命令及其参数或开关。

PowerShell:运行可执行文件

解决 PowerShell 中的外部命令行问题

在 Powershell 中运行外部命令的 5 大技巧

使用 Windows PowerShell 运行旧的命令行工具(及其最奇怪的参数)

• 正确执行 PowerShell 中的外部命令

https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right

https://mnaoumov.wordpress.com/2015/03/31/execution-of-external-commands-native-applications-in-powershell-done-right-part-2

https://mnaoumov.wordpress.com/2015/04/05/execution-of-external-commands-native-applications-in-powershell-done-right-part-3

http://edgylogic.com/blog/powershell-and-external-commands-done-right

• 引用具体内容

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules

https://trevorsullivan.net/2016/07/20/powershell-quoting

因此,这条线...

Start-Process -FilePath "c:\startscripts\project.bat" -RedirectStandardOutput "$LogFile" -WindowStyle Hidden

...应该是这样的...

Start-Process -FilePath powershell -ArgumentList '-NoProfile', '-NoExit', '-Command &{ d:\temp\abc.bat }' -RedirectStandardOutput $LogFile -WindowStyle Hidden

或者 LPChip 建议的,但这种方法要记录下来,您必须使用如下所示的重定向:

about_Redirection - PowerShell | Microsoft Docs

相关内容