Mar 06 2023 10:42 AM
I am trying to query a user and was hoping to build a csv file for those that I can and cannot get. I'm not really sure why.
users = import-csv ".\OU.csv" -Delimiter ";"
foreach ($user in $users) {
$O365user = Get-ADUser -SearchBase $user.ou -Properties * -Filter * | ? {$_.admindescription -eq $null -and $_.emailaddress -ne $null} | select EmailAddress,SamaccountName
}
foreach ($User in $O365user){
try {
$dUser = Get-mailbox $user.emailaddress -ErrorAction Stop
}
catch {
if ($_ -like "*The operation couldn't be performed because object*") {
"User '$u' does not exist."
}
else {
"An error occurred: $_"
}
continue
}
"User '$($user.displayname)' exists."
# Do stuff with $ADUser
}
Mar 07 2023 02:29 AM
Mar 07 2023 02:31 AM
@cimpercee I think this should work:
users = import-csv ".\OU.csv" -Delimiter ";"
foreach ($user in $users) {
$O365user = Get-ADUser -SearchBase $user.ou -Properties * -Filter * | ? {$_.admindescription -eq $null -and $_.emailaddress -ne $null} | select EmailAddress,SamaccountName
}
foreach ($User in $O365user){
try {
$dUser = Get-mailbox $user.emailaddress -ErrorAction Stop
$dUser | export-csv -path c:\data\users.csv -NoTypeInformation -Delimiter ';' -Encoding UTF8 -Append
}
catch {
"User '$u' does not exist."
}
If the user can be found, it exports the data to a CSV file and appends it for every user. It the user can't be found, it will just write that it can't be found and continue processing
Mar 07 2023 08:38 AM
Mar 08 2023 10:33 PM
Mar 09 2023 01:58 AM - edited Mar 09 2023 02:00 AM
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,