You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ReportService2010创建父目录:PowerShell脚本多级目录创建失败

问题根源

我一眼就发现问题所在了——你没法直接用CreateFolder("foo/bar", "/", $null)创建嵌套文件夹,因为SSRS的CreateFolderAPI不支持一次性创建多级路径。这个方法的第一个参数是单个文件夹的名称,而不是完整路径,当你传入foo/bar时,SSRS会把它当作一个包含/的非法文件夹名称,自然就抛出"The name of the item 'foo/bar' is not..."这类错误了。

修复方案

我们可以写个小辅助函数,把目标路径拆成一层一层的文件夹,逐个创建父文件夹后再创建子文件夹。下面是完整的修改后的脚本:

首先添加处理嵌套文件夹的辅助函数:

function New-SsrsNestedFolder {
    param(
        [Parameter(Mandatory=$true)]
        [Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1_ReportService2010_asmx_wsdl.ReportingService2010]$SsrsProxy,
        [Parameter(Mandatory=$true)]
        [string]$FullFolderPath
    )

    $currentParentPath = "/"
    # 把路径拆成单个文件夹名,过滤掉空字符串(比如开头的/或者连续的//)
    $folderLayers = $FullFolderPath -split "/" | Where-Object { $_ -ne "" }

    foreach ($folderName in $folderLayers) {
        $targetPath = "$currentParentPath$folderName"
        $existingType = $SsrsProxy.GetItemType($targetPath)
        
        if ($existingType -eq "Unknown") {
            # 在当前父路径下创建这个层级的文件夹
            $SsrsProxy.CreateFolder($folderName, $currentParentPath, $null)
            Write-Host "Successfully created folder: $targetPath"
        } else {
            Write-Host "Folder already exists, skipping: $targetPath"
        }
        # 更新父路径为当前文件夹的路径,确保下一层在正确的位置创建
        $currentParentPath = "$targetPath/"
    }
}

然后修改你的主脚本,调用这个函数来处理嵌套路径:

# 保持你原来的代理初始化代码
$ssrsProxy = New-WebServiceProxy -Uri $uri -Credential $cred

# 现在不管是单级还是多级路径都能正常处理了
$reportFolder = "foo/bar"

# 调用辅助函数创建嵌套文件夹
New-SsrsNestedFolder -SsrsProxy $ssrsProxy -FullFolderPath $reportFolder
方案原理

这个函数会把foo/bar拆成foo和bar两个层级:

  1. 先检查根目录下的/foo是否存在,不存在就创建它
  2. 然后把父路径切换到/foo/,再检查/foo/bar是否存在,不存在就创建它

完全符合SSRS API的要求,不会再出现非法文件夹名称的错误。

内容的提问来源于stack exchange,提问作者StackOverthrow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:05:47