Apr 14 2021 08:08 AM
Hello I am trying to use get-aduser and then extract the userprincipalname for use in a second command in a PS script. However it is not working as expected. Can anyone help as to why this is not working? Here is what I have.
$user = read-host 'Enter Username'
$UPN = get-aduser -identity $user |select-object UserPrincipalName
get-aduser $user -properties * |select PasswordLastSet
get-msoluser -userprincipalname $UPN |select LastPasswordChangeTimestamp
Im trying to compare password changes from on prem and Azure in one script. Any help is appreciated.
Thanks!
Apr 14 2021 03:15 PM
@charlie4872
The command $UPN = get-aduser -identity $user | select-object UserPrincipalName does not return a string, it returns an object with the propery UserPrincipalName. You need to adjust the second call as follows: get-msoluser -userprincipalname $UPN.UserPrincipalName | select LastPasswordChangeTimestamp
Alternatively you can also fetch UserPrincipalName as string: $UPN = get-aduser -identity $user | select-object -expand UserPrincipalName
Apr 15 2021 05:13 AM
Apr 15 2021 06:18 AM
Apr 15 2021 07:27 AM
Apr 16 2021 02:37 AM
Can you output the content of the $UPN variable, maybe like this:
$user = read-host 'Enter Username'
$UPN = get-aduser -identity $user | select-object -expand UserPrincipalName
get-aduser $user -properties * |select PasswordLastSet
Write-Host "[$UPN]"
get-msoluser -userprincipalname $UPN |select LastPasswordChangeTimestamp
Apr 16 2021 05:26 AM
Apr 17 2021 05:40 AM