SharePoint 2010: Sync name change from AD to SharePoint. Set-SPUser : Cannot get the full name or e-mail address of user - sharepoint

Typical use case is when someone gets married and their username is changed in active directory (AD). Each site collection has a list of users which are updated by an AD/SharePoint sync task, but it doesn't work well when a user's name changes. These command list all the problem site collections:
Get-SPWebApplication -Identity $Url | Get-SPSite -Limit All | Select-Object -ExpandProperty RootWeb | Get-SPUser -Limit All | Where {$_.LoginName -like $OldLoginName} | select Name, LoginName, ParentWeb
These command usually fixes the problem:
Get-SPWebApplication -Identity $Url | Get-SPSite -Limit All | Select-Object -ExpandProperty RootWeb | Get-SPUser -Limit All | Where {$_.DisplayName -eq $OldDisplayName -or $_.LoginName -eq $OldLoginName} | Set-SPUser -SyncFromAD
I'm getting following error.
Set-SPUser : Cannot get the full name or e-mail address of user
Any ideas ?

running this script fixed the problem
stsadm -o migrateuser -oldlogin $oldLogin -newlogin $newlogin -ignoresidhistory

Related

Combining & matching output from Get-AzureADUser, Get-AzureADSubscribedSku , Get-AzureADUserManager

Problem & what i have now
The script
comments are in norwegian btw, if they look strange lol
Connect-AzureAD
#variabel
$Users = Get-AzureADUser -All:$true | where-object { $null -ne $_.AssignedLicenses.SkuId } | Sort-Object CompanyName, UserPrincipalName| Select-Object -Property CompanyName, DisplayName, UserPrincipalName, Department, Mobile, TelephoneNumber
#formatting
$userlistTable = $Users | Format-Table
$userlistHTML = $Users | ConvertTo-Html
#outputs
$userlistHTML > out.html # ut som HTML
$userlistTable > out.txt # ut som Tabell i .txt
$userlistTable # ut som Tabell i terminal
My output as it stands right now:
CompanyName DisplayName UserPrincipalName Department Mobile TelephoneNumber
----------- ----------- ----------------- ---------- ------ ---------------
Company inc Usser Name username#website.com Callsenter 12345678 87654321
What i would like. is a table that has all the info in the output of $Users to inclide the users "SkuPartNumber".
The field u get by running the command Get-AzureADSubscribedSku | Select -Property SkuPartNumber
I would also like to get the users "manager", that u get by running Get-AzureADUserManager.
that last command uses the users Object ID to find their manager.
And to be honest, im very lost on how to combine these commands into one table.
its not the end of the world as it is right now. i could of just have multiple tables but having to manually cross reference these takes some time.
Im really not sure why these things are split into different commands to be honest. i get that a license is via 365 and not azure. but it seems a little backwards that i cant see the licenses from the command showing me all the user information. when a user class in powershell DOES infact show the sku ID. its burried within AssignedLicenses from running the command:
Get-AzureADUser | where-object -property UserPrincipalName -eq "emailhere#domain.com" | FL
This will give you among other things, this info:
AssignedLicenses : {class AssignedLicense {
DisabledPlans: System.Collections.Generic.List`1[System.String]
SkuId: 3b555118-da6a-4418-894f-7df1e2096870
}
conclusion
I know this was a long read. and if u made it this far im sorry.
any help with this would be amazing. This might be super easy to do, but im very far from a powershell wiz. thanks again for reading, and any help.
You can add additional properties to selected objects with calculated properties like Select #{label='name';expression={foo}}
$Users = Get-AzureADUser -All:$true
$Users | Where-Object { $_.AssignedLicenses.SkuId } |
Select-Object -Property UserPrincipalName, ## other properties here...
#{l='ManagerUPN';e={($_ | Get-AzureADUserManager).UserPrincipalName}},
#{l='AssignedSKUs';e={$_.AssignedLicenses.SkuId -join ';'}}
UserPrincipalName ManagerUPN AssignedSKUs
----------------- ---------- ------------
user#domain.com manager#domain.com 00000000-0000-0000-0000-000000000000;11111111-1111-1111-1111-111111111111
It can be slow to run Get-AzureADUserManager for every user, but that's how azure stores the relationships.
When you have a lot of users, it can be slightly faster to get the manager users first, then use Get-AzureADUserDirectReport -all $true to expand all the directreports in one call. The Microsoft.Graph.Users module is also a bit more lightweight

How can I split AD information in Powershell into a excel document?

I am a Powershell starter. I have been trying to create a script, that makes an Excel file with some AD information including the DistinguishedName. My script looks like this:
$dn = Get-ADUser -Filter * -SearchBase "OU=Users,OU=Ch01,OU=EU,DC=corp,DC=ads" | select DistinguishedName,SamAccountName,name |export-csv C:\temp\test1.csv -Delimiter ";"
An example of what I get (Note: | means new cell in Excel):
CN=Testuser\, Verfluecht,OU=Users,OU=Ch01,OU=EU,DC=corp,DC=ads | vtestuser | Testuser, Verfluecht
But in order to group the paths in excel, I need it without the CN (CN=Testuser\, Verfluecht,)
So that it would look like this:
OU=Users,OU=Ch01,OU=EU,DC=corp,DC=ads | vtestuser | Testuser, Verfluecht
How can I do this?
I tried many things such as .substring and replace, but I could not get it done.
Using this link and a calculated property, it should just drop the first part of the distinguishedname and be left with the parts you need.
Get-ADUser -Filter * -SearchBase "OU=Users,OU=Ch01,OU=EU,DC=corp,DC=ads" |
Select-Object #{Name="DistinguishedName";Expression={$_.distinguishedname | ForEach-Object {$_ -replace '^.+?(?<!\\),',''}}},samaccountname,name |
Export-Csv C:\temp\test1.csv -Delimiter ";"
On my test environment, I get the output below (without piping it to Export-Csv).
Get-ADUser -Filter * | Select-Object #{Name="DistinguishedName";Expression={$_.distinguishedname | ForEach-Object {$_ -replace '^.+?(?<!\\),',''}}},samaccountname,name
DistinguishedName samaccountname name
----------------- -------------- ----
CN=Users,DC=timhaintz,DC=com Administrator Administrator
CN=Users,DC=timhaintz,DC=com Guest Guest
CN=Users,DC=timhaintz,DC=com DefaultAccount DefaultAccount
CN=Users,DC=timhaintz,DC=com krbtgt krbtgt
Thanks, Tim.

Exchange Management Shell strip output

Exchange 2010, I am using the following in the exchange management shell
$mailidentities = Get-Mailbox | Get-MailboxPermission | where {$_.identity.tostring() -like "* STAFF/*" -and $_.identity.tostring() -NotLike "*Ex_Staff*" -and $_.User.tostring() -like "*SELF*" -and $_.IsInherited -eq $false} | Select-object Identity
foreach ($mailidentity in $mailidentities)
{
Write-Host "$mailidentity"
}
The results are returned #{Identity=domain/Group/UserName}
What is the correct syntax to get back only domain/Group/UserName ?
The final outcome is to assign fullaccess permission to a supervisory mailbox to each user.
Add the -ExpandProperty option to the Select cmdlet. Select-obejct -ExpandProperty Identity . The shortcut -exp can be used also.
For syntax of Select or Select-Object cmdlet see http://www.colorconsole.de/PS_Windows/en/Select-Object.htm or https://ss64.com/ps/select-object.html

How do I add another column to a System.Object using a list in Powershell?

I am making a script to query active directory via powershell and pull all computers that contain a username in the description field, then filter that list with only computers last logged in the past 14 days.
This is what I have so far:
$queryAD = Get-ADComputer -SearchBase 'OU=West Division,DC=cable,DC=comcast,DC=com' -Properties Name, Description -Filter {(Name -like "WA*") -and (Description -like $wildCard)} | Select-Object Name, Description
$lastLogon = $queryAD | Select-Object -ExpandProperty Description | %{$_.replace(("$NTname" + ";"),"").split(";")[0]} | %{get-date $_ -format d}
I'd like to add the list generated from $lastLogon to $queryAD, right now $queryAD is returning two columns with headers Name and Description. I need a third header added called Last Logon Date and contain the list in $lastLogon. Please advise.
You could assign the values to an array of objects to make your output cleaner (if this method is providing you the data you want) like so:
$queryAD = Get-ADComputer -SearchBase 'OU=West Division,DC=cable,DC=comcast,DC=com' -Properties Name, Description -Filter {(Name -like "WA*") -and (Description -like $wildCard)} | Select-Object Name, Description
$computer_list = #()
foreach($computer in $queryAD) {
$computer_info = New-Object PSObject -Property #{
Name = $computer.Name
Description = $computer.Description
LastLogonDate = $computer | Select-Object -ExpandProperty Description | %{$_.replace(("$NTname" + ";"),"").split(";")[0]} | %{get-date $_ -format d}
}
$computer_list += $computer_info
}
in which case $computer_list will contain all of the info you're gathering in tidy objects.
...but this method seems overcomplicated. Look into this blog entry by Matt Vogt for a better way to query for old machines in AD.

How to get a list of SiteCollections with the same name?

I have the following powershell, I am getting all sites ordering by name, but I would like to know if there is anyway to know which sites have the same name>?
$sites = Get-SPSite -Limit All
$sites | where { $web.WebTemplate -eq "Client"}
select Url,$_.Name.ToLower(), $_.RootWeb.Created, $_.RootWeb.AllProperties["ClientCode"], $_.RootWeb.AllProperties["ClientName"],$_.RootWeb.AllProperties["ClientSiteCode"] | format-table -auto | Sort-Object $_Name -descending
You can group the sites by their name and filter the count is greater then 1:
Get-SPSite -Limit All |
Group-Object Name |
Where-Object {$_.Count -gt 1}

Resources