Solved

Powershell update value with concatenated objects

Posted on 2015-02-24
6
117 Views
Last Modified: 2015-03-04
I am trying to update the UPN for a list of users in O365.  I have connected via Powershell and am able to update individual entries however my Powershell is not strong enough to do them all in one shot.

Get-MsolUser | where-object {$_.UserPrincipalName -eq ($_.firstname) +
"." + ($_.lastname) + "@domain.com"} | Set-MsolUserPrincipalName -NewUserPrincipalName ($_.firstname.substring(0,1)) + ($
_.lastname) + "@domain.com"

you cannot call a method on a null-valued expression.
At line:1 char:1
+ Get-MsolUser | where-object {$_.UserPri ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

basically trying to set the UPN to first initial + lastname instead of the firstname.lastname.  I have fixed dirsync to create the UPN this way however need to fix all the users which are set the wrong way.  Any help with the code would be appreciated.
0
Comment
Question by:YMartin
6 Comments
 
LVL 28

Expert Comment

by:becraig
ID: 40629004
Something like this should work but not tested.

Get-MsolUser | where-object {$_.UserPrincipalName -eq ($_.firstname) | %  {
$nuname = ($_.firstname.substring(0,1)) + ($_.lastname) + "@domain.com"
Set-MsolUserPrincipalName -NewUserPrincipalName $nuname 
}

Open in new window

0
 
LVL 1

Author Comment

by:YMartin
ID: 40629060
The problem is that the set-msoluserprincipalname also requires the old userprincipalname.  Both would need to be stored in the variable and be called individually.  Tried to accomplish this but it did not work for me.  It would have piped it for my command but with your version it would need to be saved to the variable.
0
 
LVL 38

Expert Comment

by:Vasil Michev (MVP)
ID: 40629206
You already have the UserPrincipalName going through the pipeline, $_.UserPrincipalName. The brackets are messed up though.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40629581
I prefer subexpressions to concatentation so I've done that below.  The only issue with becraig's was that it needed to specify the -UserPrincipalName parameter for Set-MsolUserPrincipalName.  You could use the intermediate variable, but I've just specified the value directly.
Get-MsolUser | where-object {$_.UserPrincipalName -eq "$($_.firstname).$($_.lastname)@domain.com"} | % {
    Set-MsolUserPrincipalName -UserPrincipalName $_.UserPrincipalName -NewUserPrincipalName "$($_.firstname.substring(0,1))$($_.lastname)@domain.com"
}

Open in new window

0
 
LVL 28

Expert Comment

by:becraig
ID: 40629602
yup I did forget to add the piece footech thanks .
Get-MsolUser | where-object {$_.UserPrincipalName -eq ($_.firstname) | %  {
$nuname = ($_.firstname.substring(0,1)) + ($_.lastname) + "@domain.com"
Set-MsolUserPrincipalName  -Userprinicpalname $_.UserPrincipalName -NewUserPrincipalName  $nuname 
}
                                          

Open in new window

0
 
LVL 1

Author Closing Comment

by:YMartin
ID: 40645729
Thanks footech.  That worked.  Sorry I was not able to get the code from becraig to work but I appreciate the help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now