无法转换' System.Object []'到#System; System.String'参数' Path'需要。指定的错误

时间:2018-05-16 17:12:13

标签: powershell

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

  

设置位置:无法转换' System.Object []'到#System; System.String'参数' Path'需要。规定   错误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:"**

2 个答案:

答案 0 :(得分:3)

$tests = Get-ChildItem -Path "$fullName" -Recurse -Include *.dll

cd $tests.Directory

cdSet-Location的别名,$tests包含多个dll文件,因此$tests.directory是一个包含多个文件的数组,显示为{{1} 1}}。你无法同时改变所有这些。

我不清楚你要改变哪一个,因为你有System.Object[],所以可能有许多不同的目录。也许你需要-recurse或者你需要一个循环来处理它们。

答案 1 :(得分:1)

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

替换它:

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:"**
}