在PowerShell中运行PowerShell脚本可正确运行,但从调度程序运行时会出错

时间:2020-09-29 13:53:47

标签: powershell

我有一个PowerShell脚本,该脚本调用带有一组变量和命令行开关的可执行EMCopy64.exe,这是脚本:

<# 
This script will take values from a CSV file that has a list of items broken into three columns
EXAMPLE
Folder,Source,Destination  
parapps,\\eme-fs01\c$\paraps\paraps,\\eur-pdfs01\c$\vol_paraps   
#>
$copyFiles = Get-ChildItem -Path D:\netapp_copy\csv -Name *.csv
$fileCount = $copyFiles.count

# This is to send the alert emails
$anonUsername = "anonymous"
$anonPassword = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force
$anonCredentials = New-Object System.Management.Automation.PSCredential($anonUsername,$anonPassword)
$fileNameDateTime = Get-Date -Format "MM-dd-yyyy HH-mm-ss"
$startDateTime = Get-Date -Format "MM-dd-yyyy HH:mm:ss"
$scriptLog = New-Item -Path D:\netapp_copy\csv\scriptlog\$($fileNameDateTime).log
Add-Content -Path $scriptLog -Value "Starting the script at $startDateTime"
Add-Content -Path $scriptLog -Value "Found $($copyFiles.count) files in the directory"
foreach ($cf in $copyFiles) {
    $copyValues = Import-Csv -Path D:\netapp_copy\csv\$cf 
    $DateTime = Get-Date -Format "yyyy-dd-MM-HH-mm-ss"
    $folder = $($copyValues.folder)
    $sour = $($copyValues.Source)
    $dest = $($copyValues.Destination)
    Add-Content -Path $scriptLog -Value "$DateTime Starting copy of folder $folder"
    $log = "D:\netapp_copy\log\$($folder)-$($dateTime).txt"
    $stdout = "D:\netapp_copy\log\$($folder)-$($dateTime).out"
    Add-Content -Path $scriptLog -Value "Log file is $log"
    Add-Content -Path $scriptLog -Value "Error log file is $stdout"
    $copyCMD = "D:\netapp_copy\emcopy64.exe $sour $dest *.* /s /o /de /r:1 /w:1 /c /th 128 /log:$log >>$stdout"
    Add-Content -Path $scriptLog -Value "Command is $copyCMD"
    D:\netapp_copy\emcopy64.exe $sour $dest *.* /s /o /de /r:1 /w:1 /c /th 128 /log:$log >>$stdout
    $erred = get-content $log
    $CheckIfBad = $erred | %{$_ -match " ERROR "}
    if ($CheckIfBad -contains $true) {
        $emailParms = @{
            From = 'Script@file.copy.com'
            To = 'toaddress'
            credential = $anonCredentials
            SmtpServer = 'SMTPServer'
            Subject = "Error in EMCopy64 copy process of $folder"
            Body = "Check the log file $log"
        }
        Send-MailMessage @emailParms -WarningAction SilentlyContinue
    }
}
$endDateTime = Get-Date -Format "MM-dd-yyyy HH:mm:ss"
Add-Content -Path $scriptLog -Value "Script finished at $endDateTime"

关键命令是:

D:\netapp_copy\emcopy64.exe $sour $dest *.* /s /o /de /r:1 /w:1 /c /th 128 /log:$log >>$stdout

完整的命令解析为: D:\netapp_copy\emcopy64.exe \\ame-eds02\c$\AME_EDS02_AF001\q_AME_EDS02_AF001 \\nam-pdeds02\c$\AF001 *.* /s /o /de /r:1 /w:1 /c /th 128 /log:D:\netapp_copy\log\AF001-2020-29-09-08-49-31.txt >>D:\netapp_copy\log\AF001-2020-29-09-08-49-31.out

或其他形式。当我在PowerShell ISE终端中运行它时,它可以正常运行且没有错误。但是,当我从任务计划程序或命令窗口中运行它时,使EMCopy命令打开的窗口与循环中所有项目的数量一样多。在运行下一个命令之前,它不会等待上一个命令完成运行。

我该怎么做才能防止这种情况发生?我尝试了建议here,但导致的结果是该命令在所有窗口中均失败,因为它无法识别参数。

0 个答案:

没有答案