1
votes

I'm having trouble getting this script to run correctly in Windows Servers 2012 R2 Task Scheduler, when I run it from PowerShell ISE it runs fine and copies the files to all 3 server mapped network drives, however, from Task Scheduler it only copies files to the last mapped drive and the bkup folder. I'm hoping you guys can help me get the right configuration, here's the code:

    param (
$src = "C:\Users\user\Documents\SRC_FOLDER",
$bkup = "C:\Users\user\Documents\BKUP_FOLDER",
$SHARE1 = "\\SERVER1\SHARE\1",
$SHARE2 = "\\SERVER2\SHARE\2",
$SHARE3 = "\\SERVER3\SHARE\3"
)

$mappedUnits = $bkup, $SHARE1, $SHARE2, $SHARE3

foreach ($mappedUnit in $mappedUnits)
{
    Get-ChildItem $src | Copy-Item  -Destination $mappedUnit -Recurse -Force
}

#Get-ChildItem $src | Remove-Item

Task Scheduler is set to run as Domain Admin (also tried a local admin account) and as highest privileges, it runs every 10 minutes, it invokes powershell (with full path to .exe), with arguments: -ExecutionPolicy Unrestricted -File "MOVE-FILES.ps1" and the option Start in is set to the script's path.

I've tried many types of configuration and this is the latest one which, like I mentioned before, it works when executing manually but not from Task Scheduler.

2
So, I've removed the first 2 network drives (the one that works and one that doesn't) and left only SERVER1, it's not copying the files through Task Scheduler but it does manually. I've gone into SERVER3 and looked at the permissions for the share but they are similar to SERVER1. - hectormtnezg

2 Answers

1
votes

I found the solution to my issue, I had to invoke a New-PSDrive and assign it credentials. I'll post the answer for anyone looking for something similar:

[string][ValidateNotNullOrEmpty()] $userPassword = "password"
$username = "user"
$password = ConvertTo-SecureString -String "password" -AsPlainText -Force
$computers = gc "C:\path\to\file\machines.txt"
$source = "C:\path\to\source"
$destination = "path\to\destination\server"
$bkup = "path\to\backup\folder"
$creds = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password
foreach ($computer in $computers)
{
    if (test-Connection -Cn $computer -quiet)
    {
        New-PSDrive -Name Y -PSProvider filesystem -Root "\\$computer\$destination" -Credential $creds
        Copy-Item $source -Destination Y:\ -Recurse -ErrorAction SilentlyContinue -ErrorVariable A
        if($A) { write-Output "$computer - File copy Failed" | out-file "C:\File Transfer\BIOS_Copy.txt" -Append }
        Remove-PSDrive Y
    }
    else
    {
        Write-Output "$computer is offline" | Out-File "C:\File Transfer\BIOS_Copy_error.txt" -Append
    }
}

Get-ChildItem $source | Copy-Item -Destination $bkup -Force

Get-ChildItem $source | Remove-Item -Force

I'm pretty sure I can make do without the write to file in case of error but I'll leave it just in case.

Also, the downside is that the password is in plain text and anyone with access to the script will be able to see it.

0
votes
$SHARE1 = "\\SERVER1\SHARE\1",
$SHARE2 = "\\SERVER2\SHARE\2",

Missing double quotes at the end