通过我的脚本,我尝试扫描目录以查找每天自动创建的子目录,该子目录在目录名称中包含日期。一旦它找到昨天的日期(因为我需要上传前一天),它就会查找另一个子目录,然后查找包含“JONES”的任何文件。一旦找到这些文件,它就会执行 foreach
循环,使用 winscp.com
上传它们。
我的问题是我正在尝试使用从winscp 创建的.xml 日志发送给用户以确认上传。问题是 .xml 文件仅包含最后上传的文件。
这是我的脚本:
# Set yesterday's date (since uploads will happen at 2am)
$YDate = (Get-Date).AddDays(-1).ToString('MM-dd-yyyy')
# Find Directory w/ Yesterday's Date in name
$YesterdayFolder = Get-ChildItem -Path "\\Path\to\server" | Where-Object {$_.FullName.contains($YDate)}
If ($YesterdayFolder) {
#we specify the directory where all files that we want to upload are contained
$Dir= $YesterdayFolder
#list every sql server trace file
$FilesToUpload = Get-ChildItem -Path (Join-Path $YesterdayFolder.FullName "Report") | Where-Object {$_.Name.StartsWith("JONES","CurrentCultureIgnoreCase")}
foreach($item in ($FilesToUpload))
{
$PutCommand = '& "C:\Program Files (x86)\WinSCP\winscp.com" /command "open ftp://USERNAME:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2777667474706875636741535754094f48545349464a420944484a" rel="noreferrer noopener nofollow">[email protected]</a>:21/dropoff/ -explicitssl" "put """"' + $Item.FullName + '""""" "exit"'
Invoke-Expression $PutCommand
}
} Else {
#Something Else will go here
}
我觉得我的 $PutCommand
行全部包含在 ForEach
循环中,并且每次连接/退出时它都会覆盖 xml 文件,但我还没有没有运气打破这个脚本。
最佳答案
您正在为每个文件一次又一次地运行 WinSCP。每次运行都会覆盖前一次运行的日志。
改为仅调用一次 WinSCP。它甚至更好,因为您可以避免为每个文件重新连接。
$FilesToUpload = Get-ChildItem -Path (Join-Path $YesterdayFolder.FullName "Report") |
Where-Object {$_.Name.StartsWith("JONES","CurrentCultureIgnoreCase")}
$PutCommand = '& "C:\Program Files (x86)\WinSCP\winscp.com" /command "open ftp://USERNAME:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bcbdac8c8ccd4c9dfdbfdefebe8b5f3f4e8eff5faf6feb5f8f4f6" rel="noreferrer noopener nofollow">[email protected]</a>:21/dropoff/ -explicitssl" '
foreach($item in ($FilesToUpload))
{
$PutCommand += '"put """"' + $Item.FullName + '""""" '
}
$PutCommand += '"exit"'
Invoke-Expression $PutCommand
尽管您真正需要做的就是检查 WinSCP 退出代码。如果是 0,则一切顺利。无需XML日志作为证明。
更好的是,使用 WinSCP .NET assembly from PowerShell script ,而不是从命令行驱动 WinSCP。它会为您进行所有错误检查(如果出现任何问题,您会收到异常)。并且您可以避免命令行中所有令人讨厌的东西(例如转义凭据和文件名中的特殊符号)。
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
FtpSecure = [WinSCP.FtpSecure]::Explicit
TlsHostCertificateFingerprint = "xx:xx:xx:xx:xx:xx..."
HostName = "ftps.hostname.com"
UserName = "username"
Password = "password"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files
foreach ($item in ($FilesToUpload))
{
$session.PutFiles($Item.FullName, "/dropoff/").Check()
Write-Host "Upload of $($Item.FullName) succeeded"
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
关于powershell - WinSCP XML 日志与 PowerShell 确认多个文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38440456/