Powershell将文件从一个服务器复制到另一个服务器并使用子字符串重命名

时间:2018-04-24 19:29:06

标签: powershell sharepoint-2013

并提前谢谢!我能够成功地将文档从一个服务器(sharepoint)移动到另一个服务器(脉冲)。但是,每个文档都以HC_.jpg开头。 如何移动文档并同时重命名。目前我使用两个单独的脚本和两个单独的计划任务,这是不太理想的。

从SharePoint Server移动文档的第一个脚本:

######################## Start Variables ########################
######################## Adam's Script######################
$destination = "\\pulse-dev.domain.com\C$\ProfilePhotos\"
$webUrl = "http://mysites-dev.domain.com"
$listUrl = "http://mysites-dev.domain.com/user photos/"
##############################################################

$web = Get-SPWeb -Identity $webUrl
$list = $web.GetList($listUrl)

function ProcessFolder {
param($folderUrl)
$folder = $web.GetFolder($folderUrl)
foreach ($file in $folder.Files) {
#Ensure destination directory
$destinationfolder = $destination + "/" + $folder.Url 
if (!(Test-Path -path $destinationfolder))
{
$dest = New-Item $destinationfolder -type directory 
}
#Download file
$binary = $file.OpenBinary()
$stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}
}

#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
#foreach ($folder in $list.Folders)   {

#ProcessFolder($folder.URL)
#}

第一个脚本完成后在Pulse服务器上运行的第二个脚本

$dir= "C:\ProfilePhotos\"
CD $dir
Get-ChildItem -Recurse |
where-Object {$_.Name -match 'HC_'} |

Rename-Item -NewName {$_.Name -replace 'HC_', ''}

1 个答案:

答案 0 :(得分:2)

替换

$stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create

$stream = New-Object System.IO.FileStream($destinationfolder + "/" + ($file.Name -replace "^HC_")), Create