Hello,
I’ve got a csv I’m reading from with the following headers:
LastName FirstName OldUserName NewUserName Department EmailAddress OldHomeDirectory NewHomeDirectory OldDN NewDN olddisplayname newdisplayname
I’m unsure how to construct the variables from that CSV. Some examples on the Internet show $($headervalue) as the way, or can I access it like $variable.headervalue?
function Set-ARSEADProperties { $debugPreference = "Continue" Write-Debug "Gathering Users" $userADList = Import-Csv \\fs10\home\kvanmeeteren\test_Migration_10_6_2015.csv | Select-Object * $psobjectCollection = @() foreach ($individualUser in $userADList) { #Gather variables $samaccountName = $individualUser.oldusername Write-Debug "Processing $samaccountName" $distinguishedName = $individualUser.olddn $firstName = $individualUser.firstname $lastName = $individualUser.lastname $newDN = $individualUser.newdn $firstNameDotLastName = $individualUser.newusername #change samaccountname, displayname, and clear profile path attribute. #finally move to new OU if ($samAccountName -ne $firstNameDotLastName) { Write-Debug "Performing migration on $samaccountName" Rename-ADObject -Identity $distinguishedName -NewName "$newDN" -ErrorAction Stop -WhatIf Get-ADUser $samAccountName | Set-ADUser -SamAccountName $firstNameDotLastName -DisplayName "$newDN" -HomeDirectory $null -PassThru -ErrorAction Stop -WhatIf | Move-ADObject -TargetPath "ou=newusers,dc=contoso,dc=com" -ErrorAction Stop -WhatIf } catch { Write-Host "Error migrating user. Press Ctrl+C to stop script or Enter to continue" -ForegroundColor Red Pause } finally { $newpath = "\\fs12\e$\home\$firstNameDotLastName" } } Write-Debug "Waiting for domain controllers to sync and verifying user migration is complete..." do { $newuserADList = Get-ADUser -SearchBase "ou=newusers,dc=contoso,dc=com" -Filter { samaccountname -like $firstNameDotLastName } -Properties Name, Surname, DistinguishedName, GivenName, SamAccountName, Description, HomeDirectory, Department | sort Surname Write-Host "Still waiting..." -ForegroundColor Cyan $samaccountstring = "$firstNameDotLastName" Start-Sleep -Seconds 5 -ErrorAction 'Ignore' } until ($newuserADList.surname -like '*') foreach ($newindividualUser in $newuserADList) { Write-Debug "Gathering post migration details" $newuserInfo = [ordered]@{ LastName = $newindividualUser.Surname FirstName = $newindividualUser.GivenName NewSamAccountName = $newindividualUser.samaccountname OldSamAccountName = $individualUser.oldusername NewDisplayName = $newindividualUser.name OldDisplayName = $individualUser.olddisplayname NewHomeDirectoryAttribute = $newindividualUser.HomeDirectory OldHomeDirectoryAttribute = $individualUser.HomeDirectory NewPhysicalHomeDrivePath = $newpath OldPhysicalHomeDrivePath = $oldpath NewDistinguishedName = $newindividualUser.DistinguishedName OldDistinguishedName = $individualUser.olddn } }#end new user foreach $psObject = New-Object -TypeName System.Management.Automation.PSObject -property $newuserInfo $PSObjectCollection += $PSObject }#end foreach Write-Debug "Creating Results Output" $psobjectCollection | ogv -Title "Migration Results" $csvMigrationStatus = New-Item -ItemType File c:\Users\kvanmeeteren\Desktop\MigrationResults_$((get-date).toString('MM-dd-yyyy HH.mm.sstt')).csv $PSObjectCollection | Export-Csv -Path $csvMigrationStatus -NoTypeInformation