好的原谅我的脚本noobness,我有一个PowerShell脚本,它查找特定文件扩展名的文件夹并将它们上传到FTP站点并创建一个日志文件。
这很有效,但是在上传后需要重命名文件扩展名。 我知道我需要使用WebRequest而不是WebClient,但我正在努力解决它。我怎样才能做到这一点? 这是当前的脚本:
$LogFile='C:\test\log.txt'
$TimeStamp= get-date -Format yyyyMMdd"_"HHmmss
$ScriptrunTime= get-date
Add-Content $LogFile ' '
Add-Content $LogFile '------------------------------------------------------'
Add-Content $LogFile ' '
$msg= '----------- Script run time: ' + $TimeStamp +' -----------'
Add-Content $LogFile $msg
Add-Content $LogFile ' '
$FTP="ftp://username:password@ftp.site/folder/"
$webclient = New-Object System.Net.WebClient
$webclient.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$Source ='C:\source'
try{
$data = @(dir -file $Source\*.tmp | select fullname, name)
$msg='Source: ' + $Source
Add-Content $LogFile $msg
Add-Content $LogFile ' '
$msg='Target: ' + $FTP
Add-Content $LogFile $msg
Add-Content $LogFile ' '
$msg='There are ' + $data.Count + ' files to upload'
Add-Content $LogFile $msg
foreach($item in $data){
Add-Content $LogFile ' '
$UploadFile=$ftp+$item.Name
$UploadFileName=$item.FullName
try
{
$msg="Uploading $UploadFile"
Add-Content $LogFile $msg
$webclient.UploadFile($UploadFile, $UploadFileName)
Add-Content $LogFile ' '
Add-Content $LogFile "Upload completed"
Add-Content $LogFile ' '
Add-Content $LogFile ' '
Add-Content $LogFile "Delete file"
}
catch
{
$ErrorDesc=$_.Exception.Message
Add-Content $LogFile ' '
$msg='Error occured: '+ $ErrorDesc
Add-Content $LogFile $msg
}
Add-Content $LogFile ' '
}
}
catch
{
$ErrorDesc=$_.Exception.Message
Add-Content $LogFile ' '
$msg='Error occured: '+ $ErrorDesc
Add-Content $LogFile $msg
}
$TimeStamp= get-date -Format yyyyMMdd"_"HHmmss
$ScriptrunTime= get-date
Add-Content $LogFile ' '
$msg='----------- Script end time: ' + $TimeStamp +' -----------'
Add-Content $LogFile $msg
答案 0 :(得分:0)
The WebClient.UploadFile
method doesn't require that the address
and fileName
parameters refer to a file of the same name. So you could change the extension of the file as you upload it. Assuming the new extension is ext
:
$UploadFile = $ftp + [IO.Path]::ChangeExtension($item.Name, "ext");
$webclient.UploadFile($UploadFile, $UploadFileName);