Get-Content:无法将参数绑定到参数“Path”,因为它为空

Get-Content:无法将参数绑定到参数“Path”,因为它为空

运行代码时出现错误。CSV 文件已在位置可用。


Cannot convert value "System.Collections.Specialized.OrderedDictionary" to type "System.Management.Automation.LanguagePrimitives+InternalPSCustomObject". Error: "Cannot process 
argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again."
At line:4 char:13
+             $Object = [PSCustomObject]@{
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastConstructorException
 
A null key is not allowed in a hash literal.
At line:6 char:17
+                 $_.Parameter    =   $_.Status
+                 ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Collecti...deredDictionary:OrderedDictionary) [], RuntimeException
    + FullyQualifiedErrorId : InvalidNullKey
 
A null key is not allowed in a hash literal.
At line:6 char:17
+                 $_.Parameter    =   $_.Status
+                 ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Collecti...deredDictionary:OrderedDictionary) [], RuntimeException
    + FullyQualifiedErrorId : InvalidNullKey
 
A null key is not allowed in a hash literal.
At line:6 char:17
+                 $_.Parameter    =   $_.Status
+                 ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Collecti...deredDictionary:OrderedDictionary) [], RuntimeException
    + FullyQualifiedErrorId : InvalidNullKey
 
A null key is not allowed in a hash literal.
At line:6 char:17
+                 $_.Parameter    =   $_.Status
+                 ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Collecti...deredDictionary:OrderedDictionary) [], RuntimeException
    + FullyQualifiedErrorId : InvalidNullKey
 

$Results = Get-ChildItem -Path "C:\Kumar\*.csv" |Select-Object -Property ComputerName, Parameter, Status | ForEach-Object {
    Get-Content $_.FullName | ConvertFrom-Csv | ForEach-Object {
        if ($null -ne $_.ComputerName) {
            $Object = [PSCustomObject]@{
                ComputerName    =   $_.ComputerName
                $_.Parameter    =   $_.Status
            }
        } else {
            $Object | Add-Member -MemberType NoteProperty -Name $_.Parameter -Value $_.Status
        }
    }
    $Object
}

$Results | Export-Csv -Path "C:\temp\Results.csv" -NoTypeInformation

答案1

您之所以会得到错误,是因为这行代码Select-Object -Property ComputerName, Parameter, Status放在您读取文件内容之前。当您将结果通过管道传输到时Get-Content,输出不是文件的路径。您应该将该行放在第一个ForEach子句内以避免任何错误。

$Results = Get-ChildItem -Path "C:\Kumar\*.csv" | ForEach-Object {
    Get-Content $_.FullName | ConvertFrom-Csv | Select-Object -Property ComputerName, Parameter, Status | ForEach-Object {
        if ($null -ne $_.ComputerName) {
            $Object = [PSCustomObject]@{
                ComputerName    =   $_.ComputerName
                $_.Parameter    =   $_.Status
            }
        } else {
            $Object | Add-Member -MemberType NoteProperty -Name $_.Parameter -Value $_.Status
        }
    }
    $Object
}

$Results | Export-Csv -Path "C:\temp\Results.csv" -NoTypeInformation

相关内容