Powershell 按字符串内容查找一些文件并上载到FTP
需要批处理或Powershell脚本通过其包含某些字符串的内容xls文件查找某些文件,然后上载到ftp服务器Powershell 按字符串内容查找一些文件并上载到FTP,powershell,batch-file,Powershell,Batch File,需要批处理或Powershell脚本通过其包含某些字符串的内容xls文件查找某些文件,然后上载到ftp服务器 ########################################################### $Path = "f:/temp" $Text = "123456" $PathArray = @() $Results = "F:/PROFIT/INSTALL/backup/search/test.txt" # This code snippet gets al
###########################################################
$Path = "f:/temp"
$Text = "123456"
$PathArray = @()
$Results = "F:/PROFIT/INSTALL/backup/search/test.txt"
# This code snippet gets all the files in $Path that end in ".txt".
Get-ChildItem $Path -Filter "*.txt" -Recurse|
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
$PathArray | % {$_} | Out-File $Results
}
}
Write-Host "Contents of ArrayPath:"
$PathArray | ForEach-Object {$_}
#we specify the directory where all files that we want to upload
$Dir="F:/PROFIT/INSTALL/backup/search"
#ftp server
$ftp = "ftp://127.0.0.1"
$user = "ftp"
$pass = "ftp"
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
#list every sql server trace file
foreach($item in (dir $Dir "*.*")){
"Uploading $item..."
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
}
尝试了此操作,但每次都有问题:
正在上载test.txt。。。上传文件2
аргументами: Невозможно разрешить уда ленное имя:
“127.0.0.1test.txt”F:\PROFIT\INSTALL\backup\search\search.ps1:36
знак:5
+$webclient.UploadFile$uri,$item.FullName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:NotSpecified::[],MethodInvocationException
+FullyQualifiedErrorId:WebException
您在连接服务器和文件名时出错。试着这样做:
#variables
$PathToSearch = "c:\temp"
$SearchText = "hello"
$Results = "c:\temp\test.txt"
$ftp = "ftp://127.0.0.1/"
$user = "ftp"
$pass = "ftp"
#get liste file to found with string
$PathArray=Get-ChildItem $PathToSearch -Filter "*.txt" -Recurse -file | Select-String -Pattern $SearchText | get-item | select Name, FullName
Write-Host "Files founded :"
$PathArray.FullName
#send to ftp server
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$PathArray | % {
$uri = New-Object System.Uri($ftp+$_.Name)
$webclient.UploadFile($uri, $_.FullName)
$_.FullName | Out-File -FilePath $Results -Append
}
$webclient.Dispose()
那么,您的本地主机上是否有运行正常的FTP服务器?你能用filezilla连接自己吗?有防火墙吗?$ftp+$item.Name可能是一个错误连接的路径问题。如果使用纯批处理脚本将uri更改为$ftp/$item.name,则可能很难正确读取和访问Excel文件。。。