title: Powershell OnlyInJobError
body: <p>Weird problem I saw today and I don't understand.
  Is there a difference beetween running a script manually in the ISE or Pshell, and as a job?
  If I run it manually the code doesn't throw an error - runs smoothly:</p>
  
  <pre><code>Get-ChildItem "\\SERVER\S$\ROOT\DIR" -Recurse | Where {$_.creationtime -lt (Get-Date).AddDays(-35)} | Remove-Item -Force -Include *.conf
  </code></pre>
  
  <p>But if I run it via Job and let the it export the $error to a txtfile this happens:
  Are the rights of my running machine different to the rights of the scheduled job?</p>
  
  <pre><code>Get-ChildItem : Zugriff verweigert
  In Zeile:81 Zeichen:1
  + Get-ChildItem "\\SERVER\S$\ROOT\DIR" -Recurse | Where 
  {$_.creati ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], UnauthorizedA 
     ccessException
      + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.Pow 
     erShell.Commands.GetChildItemCommand
  </code></pre>
  
  <p>Zugriff verweigert = Access denied</p>
  
  <p>Oh, totally forgot to tell about my windows rights.
  Normally the Server I am connecting to is blocked for everybody - except for login with credentials ofc. But somehow my manual powershell script is able to delete and create files?
  In "job-mode" it loses it's abilities.</p>
  
  <p>Edit:
  Same for the Test-Path commandlet. Manually it shows me true or false. Via job it throws an error.</p>
  
  <p>EDIT - SAME PROBLEM COMPLETELY DIFFERENT Commandlets:</p>
  
  <pre><code>$username = "Administrator"
  $password = cat C:\securestring.txt | convertto-securestring
  $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
  
  New-PSDrive -Name Z -PSProvider FileSystem -Root \\Server\ROOT -Credential $cred -Persist
  
  test-path 'Z:'
  
  Remove-PSDrive -Name Z -PSProvider FileSystem
  </code></pre>
  
  <p>This works!</p>
  
  <p>This does not:</p>
  
  <pre><code>$jobname        = "Test5"
  $JobTrigger     = New-JobTrigger -Daily -At "00:18 PM"
  $MyOptions      = New-ScheduledJobOption -ContinueIfGoingOnBattery -HideInTaskScheduler -RunElevated
   Register-ScheduledJob -name "$jobname" -scriptblock  {
  
  
  $username = "Administrator"
  $password = cat C:\securestring.txt | convertto-securestring
  $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
  
  New-PSDrive -Name Z -PSProvider FileSystem -Root \\Server\ROOT -Credential $cred -Persist
  
  test-path 'Z:'
  
  Remove-PSDrive -Name Z -PSProvider FileSystem
  
  } -trigger $JobTrigger –ScheduledJobOption $MyOptions
  </code></pre>