尝试使用New-ScheduledJob代替New-ScheduledTask在Windows 7上弹出PowerShell GUI

时间:2019-03-04 18:36:33

标签: powershell windows-7 taskscheduler

我试图制作一个New-ScheduledJob来运行PowerShell脚本,该脚本检查应用程序,并在用户登录时找到该应用程序时弹出通知GUI。使用New-ScheduledTask可以很好地工作,但是我很难让New-ScheduledJob发挥相同的作用。具体来说,使用凭据位并确保在脚本运行时没有密码提示,这是我使用New-ScheduledTask cmdlet进行的操作。

我面临的具体问题是

  1. 我能够使用下面的代码作为服务帐户来运行任务,但没有找到使用Scheduledjobs cmdlet执行此任务的方法。
  2. 当我通过脚本创建新的Scheduledjob时,它不会弹出应有的表格。我相信它正在后台运行,但不知道如何解决此问题。

我将首先包括与schedulenewtask cmdlet一起使用的工作脚本,然后是到目前为止我一直不成功的新脚本。

$A = New-ScheduledTaskAction -Execute "office_check_funct_test.ps1" -WorkingDirectory "C:\Users\user\Desktop\notify"
$T = New-ScheduledTaskTrigger -AtLogOn
$S = New-ScheduledTaskSettingsSet -StartWhenAvailable
$P = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount
$D = New-ScheduledTask -Principal $P -Action $A -Trigger $T -Settings $S

function Check_Program_Installed($programName) {
    $x86_check = ((Get-ChildItem "C:\Program Files (x86)\Microsoft Office") |
        Where-Object { $_."Name" -like "*$programName*" }).Length -gt 0;

    if (Test-Path 'C:\Program Files (x86)\Microsoft Office') {
        $x64_check = ((Get-ChildItem "C:\Program Files (x86)\Microsoft Office") |
            Where-Object { $_."Name" -like "*$programName*" }).Length -gt 0;
    }
    return $x86_check -or $x64_check;
}


$check = Check_Program_Installed("Office12")
if ($check -eq $false) {
    try {
        Register-ScheduledTask -InputObject $D -TaskName T1 -ErrorAction SilentlyContinue
    } catch {
    }

    Enable-ScheduledTask -TaskName T1 -ErrorAction SilentlyContinue

    Add-Type -AssemblyName System.Windows.Forms
    $config = '.\config.txt'

    $values = Get-Content $config | Out-String | ConvertFrom-StringData
    $values.textAlign
    # hashtable, hence this variable
    $values1 = Get-Content $config
    $values1[3]
    $res = (Get-WmiObject -Class Win32_VideoController).VideoModeDescription -split " x "
    $displayWidth = $res[0]
    $displayHeight = $res[1]

    $formWidth = $values.formWidth
    $formHeight = $values.formHeight
    $bufferWidth = $values.bufferWidth
    $bufferHeight = $values.bufferHeight

    $locX = $displayWidth - $formWidth - $bufferWidth
    $locY = $displayHeight - $formHeight - $bufferHeight

    $image = [system.Drawing.Image]::FromFile('.\logo.png')

    $form = New-Object System.Windows.Forms.Form
    $form.StartPosition = 'Manual'
    $form.ClientSize = "$($formWidth),$($formHeight)"
    $form.Location = "$($locX),$($locY)"
    $form.FormBorderStyle = 'FixedDialog'
    $form.MinimizeBox = $false
    $form.MaximizeBox = $false
    $form.BackgroundImage = $image
    $form.BackgroundImageLayout = 'None'
    $form.BackColor = $values.formBackColor
    $form.Opacity = $values.formOpacity

    $button1 = New-Object System.Windows.Forms.Button
    $button1.Visible = $true
    $button1Width = $values.button1Width
    $button1Height = $values.button1Height
    $button1.DialogResult = 'OK'
    $button1.Size = "$($button1Width),$($button1Height)"
    $button1.Font = $values.buttonFont
    $button1.Text = $values.buttonText
    $buttonLocX = $values.buttonLocX
    $buttonLocY = $values.buttonLocY
    $button1.Location = "$($buttonLocX),$($buttonLocY)"
    $button1.BackColor = $values.buttonBackColor
    $button1.ForeColor = $values.buttonForeColor

    $text = Get-Content -PSPath '.\message.txt'
    $lableLocX = $values.textBoxLocX
    $lableLocY = $values.textBoxLocY
    $label2 = New-Object System.Windows.Forms.Label
    $label2.Text = $text
    $label2.Font = $values.font
    $label2.Size = $values.textBoxSize
    $label2.Location = "$($lableLocX),$($lableLocY)"
    $label2.BackColor = $values.textBoxBackColor
    $label2.ForeColor = $values.textColor

    $label2.TextAlign = $values1[3] -split 'textAlign= ' | Out-String

    $Icon = [system.drawing.icon]::ExtractAssociatedIcon(".\star_logo.ico")
    $form.Icon = $Icon

    $form.Controls.AddRange(@($button1, $label2))
    [void]$form.ShowDialog()
} elseif ($check -eq $true) {
     Disable-ScheduledTask -TaskName T1
}
function Check_Program_Installed($programName) {
    $x86_check = ((Get-ChildItem -ErrorAction SilentlyContinue "C:\Program Files (x86)\Microsoft Office\Office12") |
        Where-Object { $_."Name" -like "*$programName*" }).Length -gt 0;

    if (Test-Path -ErrorAction SilentlyContinue 'C:\Program Files (x86)\Microsoft Office\Office12') {
        $x64_check = ((Get-ChildItem -ErrorAction SilentlyContinue "C:\Program Files (x86)\Microsoft Office\Office12") |
            Where-Object -ErrorAction SilentlyContinue { $_."Name" -like "*$programName*" }).Length -gt 0;
    }
    return $x86_check -or $x64_check;
}

$check = Check_Program_Installed("excel")
if ($check -eq $false) {
    try {
        $S = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery
        $logon = New-JobTrigger -AtLogOn

        $account = New-Object System.Management.Automation.PSCredential "domain/user"
        Register-ScheduledJob -ErrorAction SilentlyContinue -FilePath C:\doucments\scripts\test_job.ps1 -argument form -Name TestJob4 -ScheduledJobOption $S -Trigger $logon -Credential $account
    } catch {
    }

    try {
        Enable-ScheduledJob -ErrorAction SilentlyContinue -Name TestJob1
    } catch {
    }
    Add-Type -AssemblyName System.Windows.Forms
    $config = '.\config.txt'

    $values = Get-Content $config | Out-String | ConvertFrom-StringData
    $values.textAlign

    $values1 = Get-Content $config
    $values1[3]
    $res = (Get-WmiObject -Class Win32_VideoController).VideoModeDescription -split " x "
    $displayWidth = $res[0]
    $displayHeight = $res[1]

    $formWidth = $values.formWidth
    $formHeight = $values.formHeight
    $bufferWidth = $values.bufferWidth
    $bufferHeight = $values.bufferHeight

    $locX = $displayWidth - $formWidth - $bufferWidth
    $locY = $displayHeight - $formHeight - $bufferHeight

    $image = [system.Drawing.Image]::FromFile('.\logo.png')

    $form = New-Object System.Windows.Forms.Form
    $form.StartPosition = 'Manual'
    $form.ClientSize = "$($formWidth),$($formHeight)"
    $form.Location = "$($locX),$($locY)"
    $form.FormBorderStyle = 'FixedDialog'
    $form.MinimizeBox = $false
    $form.MaximizeBox = $false
    $form.BackgroundImage = $image
    $form.BackgroundImageLayout = 'None'
    $form.BackColor = $values.formBackColor
    $form.Opacity = $values.formOpacity

    $button1 = New-Object System.Windows.Forms.Button
    $button1.Visible = $true
    $button1Width = $values.button1Width
    $button1Height = $values.button1Height
    $button1.DialogResult = 'OK'
    $button1.Size = "$($button1Width),$($button1Height)"
    $button1.Font = $values.buttonFont
    $button1.Text = $values.buttonText
    $buttonLocX = $values.buttonLocX
    $buttonLocY = $values.buttonLocY
    $button1.Location = "$($buttonLocX),$($buttonLocY)"
    $button1.BackColor = $values.buttonBackColor
    $button1.ForeColor = $values.buttonForeColor

    $text = Get-Content -PSPath '.\message.txt'
    $lableLocX = $values.textBoxLocX
    $lableLocY = $values.textBoxLocY
    $label2 = New-Object System.Windows.Forms.Label
    $label2.Text = $text
    $label2.Font = $values.font
    $label2.Size = $values.textBoxSize
    $label2.Location = "$($lableLocX),$($lableLocY)"
    $label2.BackColor = $values.textBoxBackColor
    $label2.ForeColor = $values.textColor

    $label2.TextAlign = $values1[3] -split 'textAlign= ' | Out-String

    $Icon = [system.drawing.icon]::ExtractAssociatedIcon(".\star_logo.ico")
    $form.Icon = $Icon

    $form.Controls.AddRange(@($button1, $label2))
    [void]$form.ShowDialog()
} elseif ($check -eq $true) {
    Disable-ScheduledJob -Name TestJob3
}

0 个答案:

没有答案