FTP文件到远程Windows服务器的Powershell脚本

时间:2017-06-01 21:03:41

标签: windows powershell

我一直在努力让脚本工作,可以将文件FTP到远程Windows服务器,并希望看看我是否能得到一些帮助。我在StackOverflow和Google上搜索多个页面并且到目前为止都没有成功。下面是我的代码

逻辑如下:

  1. 使用文件夹中的模式选取最旧的文件
  2. 连接到FTP服务器
  3. 将文件复制到远程服务器
  4. 将文件从in文件夹移动到存档文件夹
  5. 代码似乎失败了,我尝试将文件FTP到服务器 - $ webclient.UploadFile($ uri,$ latestfile)

    获得此例外:

      

    使用“2”参数调用“UploadFile”的异常:“WebClient请求期间发生异常。”   在C:\ Downloads \ powershell \ testmove3.ps1:22 char: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"
    

2 个答案:

答案 0 :(得分:0)

好的,一夜之间就开始报道我得到了一个解决方案 - yeeehaw !!

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

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

trap [Exception] { 
    $recipient = "recipient@yahoo.com"
        $sender = "sender@yahoo.com"
        $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

答案 1 :(得分:0)

如果要在任何实时支持环境中使用此功能,我建议使用提示输入密码的提示:

$ftppass = Read-Host "Enter password" -AsSecureString