2017-06-01 114 views
-1

我一直在努力获得一个脚本工作,可以FTP文件到远程Windows服务器,并希望看看我能否得到一些帮助。我通过搜索StackOverflow上和谷歌multpile网页,并已成功使far.Below是我PowerShell脚本FTP文件到远程Windows服务器

逻辑如下代码:

  1. 一个文件夹中选择了一个模式最早的文件
  2. 连接到FTP服务器
  3. 将文件复制到远程服务器
  4. 从文件夹将文件移动到存档文件夹

代码似乎是失败的,我尝试将文件FTP服务器 - $ webclient.UploadFile($ URI,$ latestfile)

获取此异常:

异常调用一个 “UploadFile” 与“ 2“参数:”在WebClient请求期间发生异常。“ 在C:\下载\的powershell \ testmove3.ps1:22字符:26 + $ webclient.UploadFile < < < <($ URI,$ latestfile) + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId :DotNetMethodException

$source = "C:\downloads\" 
$destination = "C:\dest\" 
Get-ChildItem -Path C:\downloads | Where-Object { $_.name -like "TEST.CSV-PlainText*.txt" } 

$latestfile=gci -path $source | Where-Object { $_.name -like "TEST.CSV-PlainText*.txt"} | sort FirstWriteTime | select -last 1 

"Oldest File $latestfile" 


## Get ftp object 
$ftp_client = New-Object System.Net.WebClient 
$user="someuser" 
$pass="somepass" 
$ftp_address = "ftp://ftp.testserver.com" 


## Make uploads 
$uri = New-Object System.Uri($ftp+$item.Name) 
"Item is $latestfile" 
$webclient.UploadFile($uri,$latestfile) 
"File uploaded to remote servr" 

Move-Item $latestfile.FullName $destination 
"File $latestfile moved" 
+0

想知道为什么反对票吗?在发布问题之前,我真的尝试了很多选择。我认为这个论坛是为了互相帮助 - 它确定如果你不能帮助,但不要提供一个理由downvote。这令人沮丧。 – Prashanth

+0

我没有downvote,但是这个代码需要被调试才能确定它为什么抛出这个异常。你比其他任何人都更有能力做到这一点。例如,我们不知道'$ item.Name'来自哪里,或者'$ uri'和'$ latestfile'在传递给'UploadFile'时包含理智值,或者你的诊断代码(例如''Item是$ latestfile“')正在输出。没有这种信息就很难回答这个问题。顺便说一句,在你的第二个管道中,你有'排序FirstWriteTime',这应该是'排序LastWriteTime'。 – BACON

+0

另外,用于构造'New-Object System.Uri($ ftp + $ item.Name)'参数的变量不会出现在您提供的代码中的任何其他地方。在此之前,您可以设置一个名为'$ ftp_address'的变量(名称不同)。 – BACON

回答

0

好吧,是它过夜,高兴地报告,我有一个解决方案 - yeeehaw!

我甚至添加了一个逻辑来捕获异常并发送电子邮件通知。希望这可以帮助任何人使用Powershell解决FTP问题。最后,它会向调用程序返回成功或失败代码。

#PowerShell.exe -File "C:\temp\FTP.ps1" 

trap [Exception] { 
    $recipient = "[email protected]" 
     $sender = "[email protected]" 
     $server = "test.mailserver.com" 
     $subject = "FTP Test" 
     $body = "Exception Title: " + $_.Exception.GetType().FullName + "`r`n`r`n" + "Exception Details: " + $_.Exception.Message 
     $msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body 
     $client = new-object System.Net.Mail.SmtpClient $server 
     $client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials 
     $client.Send($msg) 

     exit 1 
     } 



    $ftpuser = "testuser" 
    $ftppass = "testpass" 
    $ftpserver = "ftp://ftp.testserver.com/" 
    $file = "C:\temp\one.txt" 
    $filenewname = "one.txt" 
    $webclient = New-Object System.Net.WebClient 
    $ftp = $ftpserver+$filenewname 
    $uri = New-Object System.Uri($ftp) 

    #Error was happening because the method call was attempting to use the HttpProxy on the Server machine. 
    #If the proxy is not set to null explicitly in your code, then you will get error - "An exception occurred during a webclient request" 
    $webclient.Proxy = $NULL 

    $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpuser,$ftppass) 
    "Uploading $filenewname in $ftpserver" 
    $webclient.UploadFile($uri,$file) 
    "Uploaded $filenewname in $ftpserver" 


return 0 
相关问题