I have an existing csv file that have multiple columns, I'm trying to use the "Owner" column value and check if that user exists in AzureAD or not.
If a user exists then Ignore and if it's not then export it to the new CSV file called $OrphanOneDrive.
I already have this script so far but some reason it's not working yet so I would be really appreciated if I can get any help or suggestion.
I check it using status message because this is how it look if a user in not existed.
$CSVImport = Import-CSV $LitHoldUserSites
ForEach ($CSVLine in $CSVImport) {
$CSVOwner = $CSVLine.Owner
try{
$CheckinAzureAD = Get-AzureADUser -ObjectId $CSVOwner
}catch{
$StatusMessage = $_.Exception.Message
if($Null -eq $StatusMessage ){
#Ignore because User Exists
}else{
#Export to new csv for every owner that got an error
$CheckinAzureAD | Export-Csv $OrphanOneDrive -notypeinformation -force
}
}

$CheckinAzureADbecomes null, because of thisExport-Csvis never called. Maybe you want to export the CSV line were the user couldnt be found, in which ase it would be$CSVLine | Export-Csv...Export-CSVhave an-AppendparameterExport-Csvis called but only it'sbeginandendblocks are called, however the logic of this cmdlet runs in theprocessblock.