简体   繁体   English

PowerShell脚本未创建WinSCP会话的日志文件

[英]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.

相关问题 使用 WinSCP 和 PowerShell 在文件上传之间暂停 - Pause between file uploads with WinSCP and PowerShell WinSCP Session.GetFiles错误没有这样的文件或目录 - WinSCP Session.GetFiles error No such file or directory WinSCP Session.EnumerateRemoteFiles的多个文件掩码 - Multiple file mask for WinSCP Session.EnumerateRemoteFiles 使用 PowerShell ISE 或 WinSCP 将最新文件上传到 FTP 服务器 - Upload most recent file to FTP server using PowerShell ISE or WinSCP 使用 PowerShell 和 WinSCP .NET 程序集计算上传文件的数量 - Count number of uploaded file using PowerShell and WinSCP .NET assembly WinSCP Session.GetFiles是否检查文件上传是否完成? - Does WinSCP Session.GetFiles check if file is finished uploading? PowerShell 中的 WinSCP .NET 程序集 - 创建 SessionOptions - 提供的值无效,或者属性为只读 - WinSCP .NET assembly in PowerShell - Creating SessionOptions - The value supplied is not valid, or the property is read-only 在 PowerShell 中加载 WinSCP - 无法加载文件或程序集 - 此程序集由比当前加载的运行时更新的运行时构建 - Loading WinSCP in PowerShell - Could not load file or assembly - This assembly is built by a runtime newer than the currently loaded runtime 如何在 WinSCP C# 中使用 Session.GetFiles 下载单个文件 - How to download a single file using Session.GetFiles in WinSCP C# 使用 Session.PutFile function 检查文件是否在 WinSCP 中传输成功 - Check if a file transferred successfully in WinSCP, when using the Session.PutFile function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM