[英]PowerShell script not creating log file of WinSCP session
I wrote a PowerShell script where I'm trying to SFTP a file from one of our shares to a vendor SFTP server. 我编写了一个PowerShell脚本,试图在其中将文件从我们的一个共享SFTP传输到供应商SFTP服务器。 The script completes without error but my file never arrives on the SFTP server.
该脚本已正确完成,但我的文件从未到达SFTP服务器。 I would like to log the session to find out where the problem is but I cannot see the log file out on my share either.
我想登录该会话以找出问题所在,但也看不到共享上的日志文件。
Any help/direction on why my log file is not getting created would be appreciated. 为什么不创建我的日志文件的任何帮助/说明将不胜感激。 Thanks.
谢谢。
Here is my PS code: 这是我的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()
}
You are assigning the SessionLogPath
property before creating the $session
object. 在创建
$session
对象之前,您正在分配SessionLogPath
属性。
This is the correct code: 这是正确的代码:
$session = New-Object WinSCP.Session
$session.SessionLogPath = "\\share\apps\CoverageVerifier\UploadLog.log"
This is the working code I use in production across a few customers: 这是我在一些客户的生产中使用的工作代码:
The $session variable writes the log file. $ 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.