Script running for longer than 24 hours

Hi,

Please can anyone throw light on why the script below keeps running longer than 24 hours?

$AzureUsers = Get-AzureADUser -All $true -Filter “usertype eq ‘guest’” | Where-Object -Property Userstate -EQ -Value ‘PendingAcceptance’
$Report = $AzureUsers | ForEach-Object {
$ADUserExtension = (Get-AzureADUserextension -ObjectId $.ObjectId).(“createdDateTime”)
$Ashutosh = (Get-AzureADUserextension -ObjectId $
.ObjectId).(“extension_3949a35e7317477db4e45dd98d717482_msDS_PhoneticCompanyName”)
[PSCustomObject]@{
UserPrincipalName = $.UserPrincipalName
Objectid = $
.Objectid
UserState = $.UserState
UserStateChangedOn = $
.UserStateChangedOn
UserType = $.UserType
Mail = $
.Mail
CreatedDateTime = $ADUserExtension
TenantId = $Ashutosh
}
}

$Report | Export-Csv -Path C:\Temp\DeletedGUsers28.csv -NoTypeInformation

Thank you.
Victor.

Please format your code as code!

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

What have you tried to debug your code? I’d start with running the code for only 1 user. If that works as expected I’d extend the list of users.

I don’t think your code is functional as its copied/pasted above. for example when piping you are using $. (missing the underscore). Same thing with a lot of the parts of the custom object.

I’m not familiar with ‘extension_3949a35e7317477db4e45dd98d717482_msDS_PhoneticCompanyName’ but It looks like you are running Get-AzureADUserExtension twice, once to get the ‘createdDateTime’ property and once to get the value of that extension. Why not run it once and just access the properties (reference them in the hashtable you are returning)?

For example:

TenantId = $Ashutosh

would become

TenantId = $ADUserExtension.'extension_3949a35e7317477db4e45dd98d717482_msDS_PhoneticCompanyName'

You should only need to run it once $ADUserExtension = Get-AzureADUserextension -ObjectId $_.ObjectId and then that variable should have it for use in your hashtable, you just need to access the properties like you are already doing That would eliminate a call for every single guest user you are looping through, in theory nearly doubling the speed of that section.

Lastly, How many guest users are you looping through in this state? Have you determined how many and how long it takes to loop through each user? Sometimes I’ll add some things to keep track of loops or i’ll turn a subset of 10 users or even 50 users to get a feel for how long it’s taking. You can do that by wrapping a command in Measure-Command

That’s because the code is not formatted as code. This way the forum software messes with some special characters. :man_shrugging:t3:

Thank you very much donVo!
I have been away.

Kind Regards,
Victor

Thank you Olaf. To be honest, I thought I did use site code formatting. I’ll pay more attention moving forward.

Once again, thank you for your support.

Kind Regards,
Victor.

You used the “Quote” formatting.

What’s with your issue? Is it solved?

There were 14,600 guests to loop through. I have got the results I needed.

Thank you Olaf. You have been very supportive. I am still struggling with PS though.

Kind Regards,
Victor.