0

Disclaimer: I am pretty new to PowerShell and scripting in general

I am attempting to loop through a list of folders, capture their names, split and trim the names into individual objects and write the output to two separate .CSV files. I have the basic function down to capture the information I want and export, but I want a more concise function.

Want: To capture all the information from the folder name into individual objects. If no error, capture the user information specified and output to downloads\$folderName.csv. Then if an error occurs, put the count, date, ID, and techId into another array that will output to a "downloads\$foldername_badID.csv" CSV file.

What I have so far:

$date= @()
$termId= @()
$techId=@()
$badId= @()

$termFolder= "2018_Completed"

Get-ChildItem -Path "$path\$termFolder" | Where-Object{ $_.PSIsContainer } | Select-Object Name | Format-Table -Property * -AutoSize | Out-String -Width 4096 | Out-File C:\temp\foldernames.txt

$File= Get-Content "C:\temp\foldernames.txt"

foreach( $line in $File ){
    $date+= @($line.Split(".")[0])
    $termId+= @($line.Split(".").Trim()[1])
    $techId+= @($line.Split(".").Trim()[2])

}
$termId | ForEach-Object {
    try{
        Get-ADUser $_ -Properties *
    }
    catch{
        $badId+= [pscustomobject]@{

            BadID = $_
        }
    }
} | Select-Object CN, EmployeeID, EmailAddress, SamAccountName | Export-Csv "C:\Users\$env:USERNAME\downloads\$termFolder.csv" -NoTypeInformation
$badId | Export-CSV "C:\Users\$env:USERNAME\downloads\$termFolder-invalid_ids.csv" -NoTypeInformation

What I am trying to incorporate (ref):

$date= @()
    $termId= @()
    $techId=@()
    $badId= @()

    class TermFolderClass {
        [string]$termId
        [int]$date
        [string]$techId;
    }

    $termFolder= "2018_Completed"

    Get-ChildItem -Path "$path\$termFolder" | Where-Object{ $_.PSIsContainer } | Select-Object Name | Format-Table -Property * -AutoSize | Out-String -Width 4096 | Out-File C:\temp\foldernames.txt

    $File= Get-Content "C:\temp\foldernames.txt"

    foreach( $line in $File ){
        # $date+= @($line.Split(".")[0])
        # $termId+= @($line.Split(".").Trim()[1])
        # $techId+= @($line.Split(".").Trim()[2])

        $splitFolder+= @([TermFolderClass]@{date= $line.Split(".")[0]; termId= $line.Split(".").Trim()[1]; $techId= $line.Split(".").Trim()[2]})

    }
    $termId | ForEach-Object {
        try{
            Get-ADUser $_ -Properties *
        }
        catch{
            $badId+= [pscustomobject]@{

                BadID = $_
            }
            # Pause
        }
    } | Select-Object CN, EmployeeID, EmailAddress, SamAccountName | Export-Csv "C:\Users\$env:USERNAME\downloads\$termFolder.csv" -NoTypeInformation
    $badId | Export-CSV "C:\Users\$env:USERNAME\downloads\$termFolder-invalid_ids.csv" -NoTypeInformation

Currently I am getting this error when attempting to run the incorporated version:

Cannot create object of type "TermFolderClass". The System.Object[] property was not found for the TermFolderClass object. The available property is: [termId ] , [date ] , [techId ] At line:22 char:9 + $splitFolder+= @([TermFolderClass]@{date= $line.Split(".")[0] ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : ObjectCreationError

Cannot create object of type "TermFolderClass". The System.Object[] property was not found for the TermFolderClass object. The available property is: [termId ] , [date ] , [techId ]

Justin
  • 81
  • 8
  • When you use `Get-ChildItem -Path "$path\$termFolder"` you don't define `$path` prior. Where is the value of `$path` coming from? – Thom A Mar 17 '20 at 15:00
  • I excluded the path because it is an internal server. The $path is there to show a placeholder variable for the path that would be there. – Justin Mar 17 '20 at 17:02

1 Answers1

1

1) "C:\temp\foldernames.txt" is dot separated values file (DSV). You can read it with Import-Csv command.

$items = Import-Csv -Path C:\temp\foldernames.txt -Delimiter '.' -Header Date, TermId, TechId

It will be loaded and autotrimmed as structure below:

Date TermId TechId ---- ------ ------ 2000-01-01 1234567890 1234567890 2000-01-02 1234567890 1234567890 2000-01-03 1234567890 1234567890

2) You can use ArrayList to collect invalid TermIds.

$badItems = New-Object -TypeName 'Collections.ArrayList'

foreach ($item in $items)
{
    try { Get-ADUser $item.TermId -Properties * }
    catch { $badItems.Add($item) | Out-Null }
}

3) Finally you can save wrong TermIds in similar way to:

$badItems | select TermId | Export-Csv -Path C:\BadItems.txt -NoTypeInformation

fdafadf
  • 809
  • 5
  • 13
  • 1
    You made my like a million times easier with that DSV tip! It is working almost entirely as desired now. However, the output for the $badItems is including all items. I changed the $_ to $item.TermId so the output is correct. ```try{ Get-ADUser $item.TermId -Properties * | Select-Object CN, EmployeeID, EmailAddress, SamAccountName | Export-Csv "C:\Users\$env:USERNAME\downloads\$termFolder.csv" -NoTypeInformation -Append } catch{ $badItems.Add($item) | Out-Null }``` – Justin Mar 17 '20 at 18:27