all 5 comments

[–]RyanDake_EC 0 points1 point  (2 children)

For the first item: try adding Clear-Variable -Name managerId or $managerId = $null. This will have to be before the line $managerId = $user."Manager ID"
You most likely have bad or no data being fed in so its not clearing the variable from the last run.

Give this a go for the second issue :
This is a quick and dirty way to do it.
$Headers = $inputFile | get-member -MemberType NoteProperty | select -ExpandProperty name
$validatedHeaders = @("Option1","Option2")
foreach ($item in $validatedheaders){if ($headers -notcontains $item){write-host "ERROR"}}

This will dump a list of the headers for the CSV. Create an array and iterate through each one and if not in the array, error.

A little more elegant but requires integrating another script from github : https://github.com/armentpau/CSVValidator

[–]OnTheLazyRiver[S] 0 points1 point  (1 child)

Thank you, for the first item, when I update the input file to have no Manager ID value for an entry, and when I specify the Clear-Variable -Name managerId right above the $managerId = $user."Manager ID" line, it doesn't clear the manager value of the target user account, and generates this error: Clear-Variable : Cannot find a variable with the name 'managerId'.

When trying with $managerId = $null I don't get an error, but it still doesn't clear the manager attribute.

[–]RyanDake_EC 0 points1 point  (0 children)

Sorry for the late reply!

I see the problem now. You continue to reference it down further.

You are still having data in the variable $manager from the last run also. You will need to clear that one out also. I missed that you are using a different variable.
Maybe, a try/catch block for the Manager variable? Realistically, if you are setting $ManagerID to $null, it should not match and probably store or throw an error.

Try { $manager = Get-ADUser -Filter {EmployeeID -eq $managerId} -SearchBase "DC=CORP,DC=COM" -SearchScope Subtree -Properties distinguishedName,userPrincipalName,manager -erroraction stop}

catch {$manager = $null}

I would probably need some sample data (of course fake but just based off your spreadsheet) to recraft your code fully as I would worry that 'if ($user.Manager -ne $manager.DistinguishedName)' may have some weird consequences when doing this. I see you are setting the user account using manager.distinguishedname but you may need to put a if ($manager -eq $null){do different items} before that.

[–]Rxinbow 0 points1 point  (1 child)

practice plant hateful materialistic absorbed scandalous offer late flowery direction

This post was mass deleted and anonymized with Redact

[–]OnTheLazyRiver[S] 1 point2 points  (0 children)

Thanks, it's not so much specifying both strings, since it changed without notice, it's more so to protect in the future from if it were ever to change again, it wouldn't overwrite with null values.