Powershell 如何将请求文件上载到FTP服务器并等待从FTP服务器取回文件
我想连接DTCC FTP服务器以获取一些凭据文件 程序只是创建请求文件,然后检索生成的数据。只需要一个脚本来完成这两件事Powershell 如何将请求文件上载到FTP服务器并等待从FTP服务器取回文件,powershell,ftp,sftp,ftp-client,Powershell,Ftp,Sftp,Ftp Client,我想连接DTCC FTP服务器以获取一些凭据文件 程序只是创建请求文件,然后检索生成的数据。只需要一个脚本来完成这两件事 $server="ftp 123.123.123.123" $user="123456" $passwd="123456" $data_Password = "1234" $RequestCode = "ABCD" $locationCode= "ABCD" $lastUsedSeqNum = "0560" # Create file name for RequestF
$server="ftp 123.123.123.123"
$user="123456"
$passwd="123456"
$data_Password = "1234"
$RequestCode = "ABCD"
$locationCode= "ABCD"
$lastUsedSeqNum = "0560"
# Create file name for RequestFile
$scriptName = "FTX"+$user.Substring(0,4)+".P" + $user.Substring(5) + ".PB101" + $requestCode + "." + $locationCode + $lastUsedSeqNum
$scriptPath = (Resolve-Path .\).Path
Write-Host "Current Path is: "$scriptPath
$ScriptName = $MyInvocation.MyCommand.Nameame
Write-Host "Current running script name is: "$scriptName
$ftpFile=$scriptPath + "\$scriptName.I"
# Create RequestFile
$line1 = " "+"PPASSWD0102"+ " " +$user.Substring(0,4) +"-"+$user.Substring(5)+$data_Password + " " +$RequestCode.Substring(0,4) +$((Get-Date).ToString('HHMM'))+$lastUsedSeqNum
$line2 = "TD" +$RequestCode +"01HDR"
# Create RequestFile
Add-content $ftpFile $line1
Add-content $ftpFile $line2
我想使用powershell来实现这一点。以上是我目前正在编写的代码。这是为了创建一个请求文件
有一件事是scriptname不起作用
我想把这个上传到服务器上。然后等待15分钟,从FTP服务器下载文件。我真的对powershell感到困惑
永远感谢你的帮助!谢谢 我被一件事弄糊涂了。您花费所有精力构建文件名:
$scriptName = "FTX"+$user.Substring(0,4)+".P" + $user.Substring(5) + ".PB101" + $requestCode + "." + $locationCode + $lastUsedSeqNum
然后立即尝试覆盖它
$ScriptName = $MyInvocation.MyCommand.Nameame
我不知道你想在这里做什么
至于你的实际问题,我会改编剧本 下面的脚本要求您将FTP服务器重新格式化为URI格式。它会将位于
$ftpFile
的文件上载到位于$ftp
的位置,等待15分钟,然后从ftp服务器下载相同的文件,并将其保存到$ftpFile
,覆盖它
# Get a FileInfo object for $ftpFile
$ftpFileInfo = Get-Item -Path $ftpFile
# Be sure to include the complete folder path on the server if there is one and
# also be sure to include the trailing /
$ftp = "ftp://ftp.example.com/dir/"
$user = "user"
$pass = "Pass"
# Build a URI to the FTP destination location
$ftpUri = New-Object -TypeName System.Uri -ArgumentList $ftp
$ftpFileUri = New-Object -TypeName System.Uri -ArgumentList $ftpuri, $ftpFileInfo.Name
# Create the web client
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object -TypeName System.Net.NetworkCredential -ArgumentList $user, $pass
Write-Host "Uploading from $($ftpFileInfo.FullName) to $($ftpFileUri.AbsoluteUri)..."
$webclient.UploadFile($ftpFileUri, $ftpFileInfo.FullName)
# Wait 15 minutes
Write-Host "Waiting until $((Get-Date).AddMinutes(15).ToString('HH:mm:ss'))..."
Start-Sleep -Seconds 900
# Download the file at $ftpFileUri and save it to the path for $ftpFileInfo.FullName
Write-Host "Downloading from $($ftpFileUri.AbsoluteUri) to $($ftpFileInfo.FullName)..."
$webclient.DownloadFile($ftpFileUri, $ftpFileInfo.FullName)
这取决于FTP服务器的要求,可能有效,也可能无效。您可能需要使用与上述方法类似的方法
此外,请记住此方法仅适用于普通FTP。如果您使用的是FTP,或者您称之为FTP,但实际上是指SFTP,那么您需要研究其他内容。有一个更强大的.Net库,您可以使用PowerShell编写脚本,以便与FTP、FTPS或SFTP一起使用。有一件事让我感到困惑。您花费所有精力构建文件名:
$scriptName = "FTX"+$user.Substring(0,4)+".P" + $user.Substring(5) + ".PB101" + $requestCode + "." + $locationCode + $lastUsedSeqNum
然后立即尝试覆盖它
$ScriptName = $MyInvocation.MyCommand.Nameame
我不知道你想在这里做什么
至于你的实际问题,我会改编剧本 下面的脚本要求您将FTP服务器重新格式化为URI格式。它会将位于
$ftpFile
的文件上载到位于$ftp
的位置,等待15分钟,然后从ftp服务器下载相同的文件,并将其保存到$ftpFile
,覆盖它
# Get a FileInfo object for $ftpFile
$ftpFileInfo = Get-Item -Path $ftpFile
# Be sure to include the complete folder path on the server if there is one and
# also be sure to include the trailing /
$ftp = "ftp://ftp.example.com/dir/"
$user = "user"
$pass = "Pass"
# Build a URI to the FTP destination location
$ftpUri = New-Object -TypeName System.Uri -ArgumentList $ftp
$ftpFileUri = New-Object -TypeName System.Uri -ArgumentList $ftpuri, $ftpFileInfo.Name
# Create the web client
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object -TypeName System.Net.NetworkCredential -ArgumentList $user, $pass
Write-Host "Uploading from $($ftpFileInfo.FullName) to $($ftpFileUri.AbsoluteUri)..."
$webclient.UploadFile($ftpFileUri, $ftpFileInfo.FullName)
# Wait 15 minutes
Write-Host "Waiting until $((Get-Date).AddMinutes(15).ToString('HH:mm:ss'))..."
Start-Sleep -Seconds 900
# Download the file at $ftpFileUri and save it to the path for $ftpFileInfo.FullName
Write-Host "Downloading from $($ftpFileUri.AbsoluteUri) to $($ftpFileInfo.FullName)..."
$webclient.DownloadFile($ftpFileUri, $ftpFileInfo.FullName)
这取决于FTP服务器的要求,可能有效,也可能无效。您可能需要使用与上述方法类似的方法
此外,请记住此方法仅适用于普通FTP。如果您使用的是FTP,或者您称之为FTP,但实际上是指SFTP,那么您需要研究其他内容。有一个更强大的.Net库,您可以使用PowerShell编写脚本,用于FTP、FTPS或SFTP。第二次使用ScriptName时出错。你写了:$MyInvocation.MyCommand.namame。。。如果您对ScriptName有意见,可能就是这样。噢,谢谢。这太愚蠢了…这解决了你的问题吗?这解决了一件事..但我仍然对使用FTP和powershell上载文件感到困惑。在你的第二个ScriptName使用中出现错误。你写了:$MyInvocation.MyCommand.namame。。。如果您对ScriptName有意见,可能就是这样。噢,谢谢。这太愚蠢了…这解决了你的问题吗?这解决了一件事…但我仍然对使用FTP和powershell上传文件感到困惑。