0

I am trying to make a New-ScheduledJob to run a PowerShell script that checks for an app and pops up a notification GUI when a user logs in if it finds the app. This worked fine using New-ScheduledTask, but I am having trouble getting New-ScheduledJob to function the same way. Specifically with the credentials bit and making sure there is no password prompt when the script runs, here's how I did it using the New-ScheduledTask cmdlet.

The specific issues I am facing are

  1. I was able to run the task with the code below as the service account, and haven't found a method to do this with the scheduledjobs cmdlets.
  2. when I create the new scheduledjob via the script it does not pop the form that it should. I believe it is running in the background but do not know how to correct this.

I will include first the working script that was used with the schedulenewtask cmdlet and then the new script I have been thus far unsuccessful with.

$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
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • For help with code that isn't working: post the code that isn't working. You may want to add more details about how *exactly* you're "having trouble getting `New-ScheduledJob` to function the same way". Also, [related](https://stackoverflow.com/a/41635982/1630171). – Ansgar Wiechers Mar 04 '19 at 19:08
  • updated with more information – Clifford Munford Mar 04 '19 at 21:04
  • Adding a whole bunch of irrelevant code is not going to help troubleshoot the problem. The link in my previous comment details some troubleshooting steps. Follow those and update your question with the results. – Ansgar Wiechers Mar 05 '19 at 22:38

0 Answers0