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 ]