0
votes

I'm having an issue piping a string element from an object into Get-ChildItem. My code is below:

$RegEx = 'Auto Apply Quota Path:\s+(.*)[\s\S]*?' +
         'Source Template:\s+(.*)\s+' +
         'Limit:\s+(.*)'
[int]$i = 0
[array]$objArr = @()

(dirquota au l | Out-String) -split '\r\n\r\n' | Where-Object {
    $_ -match $RegEx
} | ForEach-Object {
    $objArr += New-Object -Type PSObject -Property ([ordered]@{
        QuotaPath  = $matches[1]
        Template   = $matches[2]
        QuotaLimit = $matches[3]
    })
}

$objArr | % {
    gci $objArr[$i].QuotaPath
    $i++
} | Export-Csv -Path 'E:\outfile.csv'

Now I'm pretty sure the problem is because dot-referencing the string element in the object returns the property of the string, which is Length, and I've verified this with the outfile.csv. I need the string itself and am not having any luck finding out how to get that.

Below is the error message, which oddly contains the string I need, which is the UNC path, and it DOES exist:

gci : Illegal characters in path.
At line:20 char:5
+     gci $objArr[$i].QuotaPath
+     ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (E:\Dir\Subdir:String) [Get-ChildItem], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand

gci : Cannot find path 'E:\Dir\Subdir' because it does not exist.
At line:20 char:5
+     gci $objArr[$i].QuotaPath
+     ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (E:\Dir\Subdir:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

The solutions I've seen online say to put the string into a custom object, but they're already in an object, so that doesn't help. I'm sure I'm missing something obvious.

1
Can you post a short example that contains only the minimum amount of code needed to reproduce the problem? - Bill_Stewart
"Now I'm pretty sure the problem is because dot-referencing the string element in the object returns the property of the string, which is length" - what leads you to think that's the problem? - TessellatingHeckler
@Bill_Stewart Not sure what you mean, if you run the code I posted on a file server with auto quotas defined, you should be able to reproduce the issue. If I alter the bottom portion like so: $objArr | % { $objArr[$i].QuotaPath $i++ } | Export-Csv -Path 'E:\outfile.csv' I get a CSV file with this: #TYPE System.String "Length" "21" "21" "21" "21" "17" - Ash Housewares
@TessellatingHeckler When I export to CSV, I get output like this: #TYPE System.String "Length" "21" "21" "21" "21" "17" - I'm just wondering if the same values are being passed to gci rather than the path string - Ash Housewares

1 Answers

0
votes

The error messages complain about illegal characters and non-existing paths. Since your input string has linebreaks encoded as CR-LF and the . metacharacter in the regular expression matches all characters except newline (LF) the most likely reason for your issue is that your QuotaPath values have a trailing CR, which isn't a valid character in a path.

You can avoid the issue by removing CR from the string:

(dirquota au l | Out-String) -replace '\r\n', "`n" -split '\n\n'

Also, you may want to avoid appending to an array in a loop, because it tends to perform poorly. Just collect the loop output in a variable:

$objArr = (dirquota au l | Out-String) -replace '\r\n', "`n" -split '\n\n' |
          Where-Object { $_ -match $RegEx } |
          ForEach-Object { New-Object -Type PSObject -Property ... }

then process the objects like this:

$objArr | ForEach-Object {
    Get-ChildItem $_.QuotaPath
} | Export-Csv 'E:\outfile.csv'