【问题标题】:Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified error无法将“System.Object[]”转换为参数“Path”所需的类型“System.String”。指定错误
【发布时间】:2018-05-16 17:12:12
【问题描述】:

运行 powershell 脚本时出现以下错误

设置位置:无法将“System.Object[]”转换为参数“Path”所需的类型“System.String”。指定的 错误 15-May-2018 08:31:42 方法不受支持。

以下是有问题的脚本,请您在这里提出问题

cd $lsolutionPath
    Get-ChildItem -Path "$lsolutionPath" -Filter "*Tests" -Recurse -Directory | where {$_.FullName -inotlike "*.sonarqube*"} | ForEach-Object {
    $fullName = $_.FullName
    $projName = $_.BaseName
    write-output $projName
    write-output $fullName

    Write-Output "Starting Build Helper unit test run:3"
    $tests = Get-ChildItem -Path "$fullName" -Recurse -Include *.dll
    Write-Output "Starting Build Helper unit test run:4"
    if($tests -eq $null) {
        Write-Error "Could not find *Tests.dll"
        return 999
        }

    cd $tests.Directory
    Write-Output $tests.Directory
    Write-Output "target args"
    $targetArgs = "\""$tests\"" -nologo -parallel none -noshadow -xml \""$xUnittestResultsPath\$projName.xml\"" -nunit \""$testResultsPath\$projName.xml\"""
    Write-Output "$target args"
     Write-Output "###### Target Args:"**

【问题讨论】:

    标签: powershell


    【解决方案1】:
    $tests = Get-ChildItem -Path "$fullName" -Recurse -Include *.dll
    
    cd $tests.Directory
    

    cdSet-Location 的别名,$tests 包含多个 dll 文件,因此$tests.directory 是多个文件的数组,即显示为System.Object[]。您不能同时更改为所有这些。

    我不清楚你想改成哪一个,因为你有-recurse,所以可能有很多不同的目录。也许你需要get-childitem ... | Select-Object -First 1 或者你需要一个循环来处理每一个。

    【讨论】:

    • 我同意 +1,目录的 gci 也在递归 - 所以递归 *tests 文件夹或 *.dll 文件似乎存在差异
    【解决方案2】:

    $tests 变量是一个文件对象数组,因此您可能只需为$tests 数组创建另一个foreach 循环。

    替换这个:

    cd $tests.Directory
    Write-Output $tests.Directory
    Write-Output "target args"
    $targetArgs = "\""$tests\"" -nologo -parallel none -noshadow -xml \""$xUnittestResultsPath\$projName.xml\"" -nunit \""$testResultsPath\$projName.xml\"""
    Write-Output "$target args"
     Write-Output "###### Target Args:"**
    

    有了这个:

    $tests | foreach {
    Set-Location $_.Directory
    Write-Output $_.Directory
    Write-Output "target args"
    $targetArgs = "\""$($_.FullName)\"" -nologo -parallel none -noshadow -xml \""$xUnittestResultsPath\$projName.xml\"" -nunit \""$testResultsPath\$projName.xml\"""
    Write-Output "$target args"
    Write-Output "###### Target Args:"**
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多