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

Powershell to Copy files from one server to another and rename using substring

提前致谢!!!我能够成功地将文档从一台服务器 (sharepoint) 移动到另一台 (pulse)。但是,每个文档都以 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_', ''}

替换

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

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