Home > other >  Powershell get files from machine doesn't work when executed by Taskscheduler
Powershell get files from machine doesn't work when executed by Taskscheduler

Time:02-03

I have a machine that I should read logdata from. The data is located at 192.168.1.151/traceability. I wrote a little Script that should be able to return the the files. It does work when I manualy start it. But when I execute it with Windows Task Scheduler it is not able to get the files:

$WinAPI = @"
   public class WinAPI
   {
      [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = 
      System.Runtime.InteropServices.CharSet.Auto)]
      public static extern void OutputDebugString(string message);
   }
"@
$Path = "\\192.168.1.151\traceability"
$items = get-childitem -Path $Path
[WinAPI]::OutputDebugString($items.Length);

The $items.Length is zero when it is executed by Task Scheduler and 3 if it is executed manualy. The task is set to run with full priviledge on the Admin Account (same one that successfully tested the script). I also tryed to map the path to Z: and use $Path = "Z:\" instead but that didn't help to solve the Problem.

Am I doing something wrong or is it not possible to get the files with Powershell.

CodePudding user response:

I solved the Problem by doing the mapping inside the Powershellscript:

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("p:", $Path, $false, "user", "password")

That way the Admin user is able to access the files.

  •  Tags:  
  • Related