用于将文件移动到多个网络驱动器的PowerShell脚本

用于将文件移动到多个网络驱动器的PowerShell脚本,powershell,scheduled-tasks,Powershell,Scheduled Tasks,我无法在Windows server 2012 R2任务计划程序中正确运行此脚本,当我从PowerShell ISE运行它时,它运行良好并将文件复制到所有3个服务器映射的网络驱动器,但是,从任务计划程序它只将文件复制到最后一个映射的驱动器和bkup文件夹。我希望你们能帮我找到正确的配置,下面是代码: param ( $src = "C:\Users\user\Documents\SRC_FOLDER", $bkup = "C:\Users\user\Documents\BKUP_FOLD

我无法在Windows server 2012 R2任务计划程序中正确运行此脚本,当我从PowerShell ISE运行它时,它运行良好并将文件复制到所有3个服务器映射的网络驱动器,但是,从任务计划程序它只将文件复制到最后一个映射的驱动器和bkup文件夹。我希望你们能帮我找到正确的配置,下面是代码:

    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
任务计划程序设置为以域管理员身份运行(也尝试了本地管理员帐户),并且作为最高权限,它每10分钟运行一次,调用powershell(具有.exe的完整路径),参数为:
-ExecutionPolicy Unrestricted-File“MOVE-FILES.ps1”
,并且将选项在中启动设置为脚本的路径

我已经尝试了许多类型的配置,这是最新的配置,正如我前面提到的,它在手动执行时有效,但在任务调度器中无效

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

结尾缺少双引号

我找到了问题的解决方案,我不得不调用一个新的PSDrive并为其分配凭据。我将为寻找类似答案的任何人发布答案:

[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
我很确定,如果出现错误,我可以不写文件,但我会留着以防万一


另外,缺点是密码是纯文本的,任何有权访问脚本的人都可以看到它。

对不起,这是我的输入错误。因此,我删除了前两个网络驱动器(一个可用,一个不可用),只留下了服务器1,它没有通过任务计划程序复制文件,而是手动复制。我进入了SERVER3,查看了共享的权限,但它们与SERVER1类似。