Author Posts

October 5, 2015 at 8:56 am

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

October 5, 2015 at 9:03 am

One particular issue is getting the results after setting them.

DEBUG: Waiting for domain controllers to sync and verifying user migration is complete...
Get-ADUser : The search filter cannot be recognized
At \\fs10\home\kvanmeeteren\Powershell\InProgress\Set-ARSEADProperties.ps1:143 char:21
+             $newuserADList = Get-ADUser -SearchBase "ou=newusers..
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : The search filter cannot be recognized,Microsoft.ActiveDirectory.Management.Commands.Get
   ADUser

October 5, 2015 at 9:23 am

Update this line
$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

to

$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

Or you might need to make the sam account name in the excel spread sheet in " "

I just tired this

$sam = "Jeffery.Test"

PS H:\> Get-ADUser -Filter {Samaccountname -eq "$sam"}

PS H:\> Get-ADUser -Filter {Samaccountname -eq $sam}

DistinguishedName : CN=Jeffery\, Test,OU=TEST,OU=CTM,OU=CSD,OU=COACD,DC=coacd,DC=org
Enabled : True
GivenName : Jeffery
Name : Jeffery, Test
ObjectClass : user
ObjectGUID : 8d3a5c82-69f3-4be2-b7e6-d86df7c96326
SamAccountName : Jeffery.Test
SID : S-1-5-21-682003330-789336058-2146660071-97636
Surname : Test
UserPrincipalName : Jeffery.Test@coacd.org

PS H:\> $list = Get-ADUser -Filter {Samaccountname -eq $sam}

PS H:\> $list

DistinguishedName : CN=Jeffery\, Test,OU=TEST,OU=CTM,OU=CSD,OU=COACD,DC=coacd,DC=org
Enabled : True
GivenName : Jeffery
Name : Jeffery, Test
ObjectClass : user
ObjectGUID : 8d3a5c82-69f3-4be2-b7e6-d86df7c96326
SamAccountName : Jeffery.Test
SID : S-1-5-21-682003330-789336058-2146660071-97636
Surname : Test
UserPrincipalName : Jeffery.Test@coacd.org