Why does my Screenshot Function not work as a Job
<p>I have a function inside a Job which takes a Screenshot. The Script itself gets executed by Taskscheduler as a domain admin. I also checked "Run with highest privileges".</p>

<p>The Job should do a Screenshot and then send an E-Mail, but both of those thing don't happen. I also don't see an error message (maybe because it's a background job?). Maybe the E-Mail doesn't get sent because it wants to attach the screenshot to the e-mail, but can't because the screenshot wasn't created.</p>

<p>Why does my function not Take the Screenshot? The domain admin has privileges to write to the destination. The screenshot gets created when I run the function outside the <code>start-job</code>. if i have the function inside <code>start-job</code> it doesn't get created, doesn't matter if the script is started via Taskscheduler or manually.</p>

<p>What am I missing?</p>

<p>The following Code starts the Job and takes the screenshot:</p>

<pre><code>Start-Job -Name LogikWebserverWatch {

    function Take-Screenshot([string]$outfile) 
    {
    [int]$PrtScrnWidth = (gwmi Win32_VideoController).CurrentHorizontalResolution
    [int]$PrtScrnHeight = (gwmi Win32_VideoController).CurrentVerticalResolution
    $bounds = [Drawing.Rectangle]::FromLTRB(0, 0, $PrtScrnWidth, $PrtScrnHeight)
    $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
    $graphics = [Drawing.Graphics]::FromImage($bmp)
    $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
    $bmp.Save($outfile)
    $graphics.Dispose()
    $bmp.Dispose()
    }

    while ((Get-Process LogikWebserver).Responding) {sleep -Milliseconds 50}

    if (!(Get-Process LogikWebserver).Responding) {
    Try{

        $utf8 = New-Object System.Text.utf8encoding
        $datetime = (get-date).ToString('yyyyMMdd-HHmmss')
        Take-Screenshot -outfile C:\Install\LogikWebserverErrorReporting\Screenshot-$datetime.png
        # some more code [...]
    } Catch {  some more code [...] }
}}
</code></pre>