我正在使用SSRS PowerShell cmdlet将所有SSRS报告移至新服务器。现在,我正在尝试重新创建新报告服务器上已存在的文件层次结构。
为了做到这一点,我正在通过我在PC上创建的报告文件夹的本地副本进行递归。使用New-RsFolder
函数,我可以创建第一级文件夹,但是一旦到达任何类型的子目录,脚本就会失败。
Import-Module ReportingServicesTools
$ReportPath = 'C:\MyFiles\SSRS 2016 Upgrade\Reporting Server\'
$Folders = Get-ChildItem $ReportPath -Recurse -Directory
foreach($Folder in $Folders){
$SubFolderPath = ''
$arrSubFolders = $Folder.FullName.Replace($ReportPath, '').Split('\')
foreach($SubFolder in $arrSubFolders){
$SubFolderPath += '/'
try{
New-RsFolder -ReportServerUri $ReportServerURI -FolderName $SubFolder -RsFolder $SubFolderPath
Write-Host "Created folder $($SubFolderPath)/$($SubFolder)"
}
catch{
Write-Host "Folder $($SubFolderPath)/$($SubFolder) exists"
}
$SubFolderPath += $SubFolder
}
}
当我回到会计时,脚本开始捕获该文件夹已存在,如下所示:
Created folder //Accounting
Created folder //Customer Service
Created folder //Dashlets
Created folder //Data Sources
Created folder //DataSets
Created folder //Forms
Created folder //Human Resources
Created folder //Information Technology
Created folder //Integrated
Created folder //Maintenance
Created folder //Management
Created folder //Old Reports
Created folder //Operations
Created folder //Owner Operator Settlements
Created folder //Report Parts
Created folder //Safety and Recruiting
Folder //Accounting exists
Folder /Accounting//PnG Proposal and Payment exists
Folder //Accounting exists
Folder /Accounting//PTI Logistics exists
Folder //Accounting exists
Folder /Accounting//Settlements exists
Folder //Accounting exists
Folder /Accounting//XRS exists
Folder //Accounting exists
Folder /Accounting//PTI Logistics exists
Folder /Accounting/PTI Logistics//PTI Logistics - Settlements exists
Folder //Accounting exists
Folder /Accounting//PTI Logistics exists
Folder /Accounting/PTI Logistics//PTI Logistics - Settlements exists
Folder /Accounting/PTI Logistics/PTI Logistics - Settlements//Drill-Through exists
Folder //Accounting exists
Folder /Accounting//Settlements exists
Folder /Accounting/Settlements//Drill-Through exists
Folder //Accounting exists
Folder /Accounting//Settlements exists
Folder /Accounting/Settlements//TMW Templates exists
Folder //Customer Service exists
递归从来都不是我的强项,所以如果有人能对此有所了解,我会很感激。
答案 0 :(得分:2)
Get-ChildItem -Recurse
枚举目标目录树,其父路径位于其后代之前。
此外,您可以使用-Name
开关输出目录' path strings ,相对于目标目录。
这意味着您可以专注于逐个创建每个文件夹,依靠其祖先路径预先存在(在/
的情况下)或在之前的迭代中创建:
Import-Module ReportingServicesTools
$ReportPath = 'C:\MyFiles\SSRS 2016 Upgrade\Reporting Server'
Get-ChildItem $ReportPath -Recurse -Directory -Name | ForEach-Object {
# Split the relative input path into leaf (directory name)
# and parent path, and convert the parent path to the target parent path
# by prepending "/" and converting path-internal "\" instances to "/".
$SubFolderParentPath = '/' + ((Split-Path -Parent $_) -replace '\\', '/') #'
$SubFolderName = Split-Path -Leaf $_
try{
New-RsFolder -ReportServerUri $ReportServerURI -Path $SubFolderParentPath -FolderName $SubFolderName
Write-Host "Created folder ${SubFolderPath}/${SubFolder}"
}
catch {
# Report the specific error that occurred, accessible via $_
Write-Host "An error occurred for ${SubFolderPath}/${SubFolder}: $_"
}
}
这种方法不仅比你的方法更有效,而且还避免了多次创建祖先路径的尝试。
注意:
使用-Path
而不是-RsFolder
,因为作为New-RsFolder
available in the PowerShell Gallery一部分的ReportingServices
module版本仅支持-Path
来指定目标location(文件夹)。
catch
块现在报告发生的实际错误($_
),因为特定问题可能是其他而不是预先存在的文件夹。
请务必使用-Path
启动传递给/
的目标路径。