Forum Discussion
Unable to catch the error
- cimperceeMar 09, 2023Copper ContributorI did try it, to get some idea how you would code it by combining it with my own code. I didn't manage to get it work. error is still not handled properly.
$users = import-csv ".\OU.csv" -Delimiter ";"
foreach ($user in $users) {
$O365users = Get-ADUser -SearchBase $user.ou -Properties * -Filter * | ? {$_.admindescription -eq $null -and $_.emailaddress -ne $null} | select EmailAddress,SamaccountName
}
foreach ($o365user in $o365users){
Try{
$user = get-mailbox $o365user.emailaddress | Select AccountDisabled,RecipientTypeDetails,primarysmtpaddress,userprincipalname,displayname,alias -errorAction Stop
if ($user.accountdisabled -eq $true -and $user.recipientTypedetails -eq "usermailbox"){
$staleusers += [PSCustomObject]@{
"Emailaddress" = $user.primarysmtpAddress
"Name" = $user.displayname
"UPN" = $user.userprincipalname
"Status" = $user.accountdisabled
"RecipientType" = $user.recipienttype}
}}
Catch{
"User $o365user.emailaddress does not exist"}
}
$StaleUsers | Export-Csv .\staleusers1.csv -notypeinformation
#$nonexistentusers |where {$_.| export-csv .\nonexistentusers.csv
foreach ($users in Staleusers){
get-aduser -properties * -filter * | ? {$_.emailaddress -eq $staleusers.emailaddress}
}- Varun_GhildiyalMar 09, 2023Iron Contributor
It looks like there are a couple of issues with the code you provided.
Firstly, there's a typo in the variable name in the foreach loop that tries to access the $o365users variable. The variable is named $O365users (capital "O"), so you need to use that instead.
Secondly, the foreach loop that tries to get the corresponding AD user for each stale Office 365 user is not assigning the result to a variable, so it's not doing anything with the output. You should assign the result to a variable, like $aduser, and then do something with that variable
Here's the modified code that should work:
$users = import-csv ".\OU.csv" -Delimiter ";" $staleusers = @() foreach ($user in $users) { $O365users = Get-ADUser -SearchBase $user.ou -Properties * -Filter * | ? {$_.admindescription -eq $null -and $_.emailaddress -ne $null} | select EmailAddress,SamaccountName } foreach ($o365user in $O365users){ Try{ $user = get-mailbox $o365user.emailaddress | Select AccountDisabled,RecipientTypeDetails,primarysmtpaddress,userprincipalname,displayname,alias -errorAction Stop if ($user.accountdisabled -eq $true -and $user.recipientTypedetails -eq "usermailbox"){ $staleusers += [PSCustomObject]@{ "Emailaddress" = $user.primarysmtpAddress "Name" = $user.displayname "UPN" = $user.userprincipalname "Status" = $user.accountdisabled "RecipientType" = $user.recipienttype } } } Catch{ "User $o365user.emailaddress does not exist" } } $StaleUsers | Export-Csv .\staleusers1.csv -notypeinformation foreach ($staleuser in $staleusers){ $aduser = get-aduser -properties * -filter * | ? {$_.emailaddress -eq $staleuser.emailaddress} # do something with $aduser, e.g. output its properties $aduser }Note that I added a $staleusers variable to collect the stale Office 365 users, which is then exported to a CSV file. I also added a foreach loop to get the corresponding AD user for each stale user, which assigns the result to the $aduser variable and outputs its properties. You'll need to replace # do something with $aduser with whatever code you want to execute for each AD user.
Edit# I didnt Corrected the typo in the modified script,