PowerShell 脚本不创建 WinSCP 会话的日志文件
PowerShell script not creating log file of WinSCP session
我编写了一个 PowerShell 脚本,试图将文件从我们的共享之一通过 SFTP 传输到供应商 SFTP 服务器。脚本完成且没有错误,但我的文件从未到达 SFTP 服务器。我想记录会话以找出问题所在,但我也看不到我的共享上的日志文件。
任何 help/direction 为什么我的日志文件没有被创建将不胜感激。谢谢
这是我的 PS 代码:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Declare variables
$date = Get-Date
$dateStr = $date.ToString("yyyyMMdd")
$filePath = "\share\apps\CoverageVerifier\cvgver." + $dateStr + ".0101"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::WebDAV
HostName = "secureftpa.verisk.com"
PortNumber = 443
UserName = "account"
Password = "password"
WebdavSecure = $True
TlsHostCertificateFingerprint = "00:00:00:00:00:00:00:00:00:..."
}
$session.SessionLogPath = "\share\apps\CoverageVerifier\UploadLog.log"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles($filePath, "/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
finally
{
$session.Dispose()
}
这是我在几个客户的生产中使用的工作代码:
$session 变量写入日志文件。
$csvsavepath = "C:\SCRIPTS$teamname$scriptname\"
$sessionlog = 'sessionlog.txt'
# Setup the connection Options #
$sessionOptions = Set-SFTPSessionOption -HostName ftp.public.com -UserName 'username' -Password "$Password" -SshHostKey 'ssh-rsa 2048 [key here]'
# Create and open session to remote host #
$Session = Open-SFTPSession -sessionOptions $sessionOptions -SessionLogPath "$csvsavepath$sessionlog"
# Upload files to host #
Write-ToSFTPServer -Session $Session -SourcePath "$csvsavepath$csv" -RemotePath "$csv" -TransferMode binary -DeleteSource
# clean up connection object #
Close-SFTPSession -Session $session
您在创建 $session
对象之前分配 SessionLogPath
属性。
这是正确的代码:
$session = New-Object WinSCP.Session
$session.SessionLogPath = "\share\apps\CoverageVerifier\UploadLog.log"
我编写了一个 PowerShell 脚本,试图将文件从我们的共享之一通过 SFTP 传输到供应商 SFTP 服务器。脚本完成且没有错误,但我的文件从未到达 SFTP 服务器。我想记录会话以找出问题所在,但我也看不到我的共享上的日志文件。
任何 help/direction 为什么我的日志文件没有被创建将不胜感激。谢谢
这是我的 PS 代码:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Declare variables
$date = Get-Date
$dateStr = $date.ToString("yyyyMMdd")
$filePath = "\share\apps\CoverageVerifier\cvgver." + $dateStr + ".0101"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::WebDAV
HostName = "secureftpa.verisk.com"
PortNumber = 443
UserName = "account"
Password = "password"
WebdavSecure = $True
TlsHostCertificateFingerprint = "00:00:00:00:00:00:00:00:00:..."
}
$session.SessionLogPath = "\share\apps\CoverageVerifier\UploadLog.log"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles($filePath, "/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
finally
{
$session.Dispose()
}
这是我在几个客户的生产中使用的工作代码:
$session 变量写入日志文件。
$csvsavepath = "C:\SCRIPTS$teamname$scriptname\"
$sessionlog = 'sessionlog.txt'
# Setup the connection Options #
$sessionOptions = Set-SFTPSessionOption -HostName ftp.public.com -UserName 'username' -Password "$Password" -SshHostKey 'ssh-rsa 2048 [key here]'
# Create and open session to remote host #
$Session = Open-SFTPSession -sessionOptions $sessionOptions -SessionLogPath "$csvsavepath$sessionlog"
# Upload files to host #
Write-ToSFTPServer -Session $Session -SourcePath "$csvsavepath$csv" -RemotePath "$csv" -TransferMode binary -DeleteSource
# clean up connection object #
Close-SFTPSession -Session $session
您在创建 $session
对象之前分配 SessionLogPath
属性。
这是正确的代码:
$session = New-Object WinSCP.Session
$session.SessionLogPath = "\share\apps\CoverageVerifier\UploadLog.log"