Hello,
I have a script I am using to identify when new users are added to a website we use. The below code pulls a list of users that are on the website. When I first started, I exported the current list of users from the website into a CVS (UserDatabase.csv). The CSV file is imported each time and turned into the ‘$UserDatabase’ variable. Any new users detected are later appended to the CSV so they are not detected again.
The issue is that, intermittently, it will notify me of a new user being added to the website, but the user in fact already exists in the reference CSV file. It should only be flagging up users which exist on the website, and not in the local CSV. We have multiple companies with their own, separate instances of the website, so the foreach loop is used to connect to each company’s instance, pull their user data, and compare it to the data in UserDatabase.csv. The reference file contains the data from every site instance. I don’t think that would cause an issue though. Compare-object should still be able to find the user in the reference CSV. I have checked previous versions of UserDatabase.csv and the users it is flagging definitely already exist in there.
## Script using OAuth2 ### #Refresh Tokens to talk to API for the site (multiple companies on the same site, each have their own refresh token) $RefreshTokens = "Example1","Example2","Example3" $UserDatabasePath = "C:\UserDatabase\UserDatabase.csv" $UserDatabase = Import-Csv -Path $UserDatabasePath | sort firstname #URL to retrieve site API Access Token $TokenRequestURL = 'https://example/token' foreach ($RefreshToken in $RefreshTokens) { $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $Params = @{"client_id"="Example"; "grant_type"="refresh_token"; "refresh_token"="$RefreshToken"; } $SiteAccessToken = Invoke-webrequest -uri $TokenRequestURL -Method POST -body $Params -ContentType "application/x-www-form-urlencoded" -headers $headers -UseBasicParsing | convertfrom-json | select -ExpandProperty access_token #Add client ID and access token to the HTTP headers $headers.Add("X-ClientId", 'Example') $headers.Add("Authorization", "Bearer $SiteAccessToken") #Retrieve list of users through API $NewUserUri = 'URL pointing to list of users' $SiteUsers = Invoke-webrequest -uri $NewUserUri -Method GET -headers $headers -UseBasicParsing | convertfrom-json | select -expandproperty data | select firstname, lastname | sort firstname #Compare local and remote list of users. Flag any users which exist on the website, but not locally. $NewUsers = Compare-object -ReferenceObject $UserDatabase -DifferenceObject $SiteUsers -Property firstname, lastname | where-object SideIndicator -EQ "=>" ### Some code to send an email notification with details of any new users #### #Append any new users to the database $NewUsers | export-csv -append $UserDatabasePath }
Any help would be greatly appreciated. I’ve probably made an oversight in the compare-object bit, not sure what it is though as I’m still a semi-beginner.