getod4b at sites.ps1
DESCRIPTION
This script obtains Office 365 one drive mysites based on a domain suffix of the users. This is helpful in isolating or obtaining a list of mysites tied to series of domain suffixes, or in this case a country that owns a set of domains.TRANSCRIPT
![Page 1: GetOD4B at Sites.ps1](https://reader035.vdocument.in/reader035/viewer/2022080304/5695d3ba1a28ab9b029ef80b/html5/thumbnails/1.jpg)
# URL for your organization's SPO admin service$AdminURI = "https://tenant-admin.sharepoint.com"
# Check and delete old file \ Where should we save the list of MySites?
$strFileName = "C:\Scripts\OD4B\Output\ListOfATMysites.txt"
If (Test-Path $strFileName){
write-host "File exists. Deleting existing output file."Remove-Item $strFileName
}
$LogFile = 'C:\Scripts\OD4B\Output\ListOfATMysites.txt'
# Begin the process
$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")$loadInfo3 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")
# Use a stored credential in credential manager
$ErrorActionPreference = "silentlycontinue"$sig = @"[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]public struct NativeCredential{ public UInt32 Flags; public CRED_TYPE Type; public IntPtr TargetName; public IntPtr Comment; public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten; public UInt32 CredentialBlobSize; public IntPtr CredentialBlob; public UInt32 Persist; public UInt32 AttributeCount; public IntPtr Attributes; public IntPtr TargetAlias; public IntPtr UserName; internal static NativeCredential GetNativeCredential(Credential cred) { NativeCredential ncred = new NativeCredential(); ncred.AttributeCount = 0; ncred.Attributes = IntPtr.Zero; ncred.Comment = IntPtr.Zero; ncred.TargetAlias = IntPtr.Zero; ncred.Type = CRED_TYPE.GENERIC; ncred.Persist = (UInt32)1; ncred.CredentialBlobSize = (UInt32)cred.CredentialBlobSize; ncred.TargetName = Marshal.StringToCoTaskMemUni(cred.TargetName); ncred.CredentialBlob = Marshal.StringToCoTaskMemUni(cred.CredentialBlob); ncred.UserName = Marshal.StringToCoTaskMemUni(System.Environment.UserName);
![Page 2: GetOD4B at Sites.ps1](https://reader035.vdocument.in/reader035/viewer/2022080304/5695d3ba1a28ab9b029ef80b/html5/thumbnails/2.jpg)
return ncred; }}[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]public struct Credential{ public UInt32 Flags; public CRED_TYPE Type; public string TargetName; public string Comment; public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten; public UInt32 CredentialBlobSize; public string CredentialBlob; public UInt32 Persist; public UInt32 AttributeCount; public IntPtr Attributes; public string TargetAlias; public string UserName;}public enum CRED_TYPE : uint { GENERIC = 1, DOMAIN_PASSWORD = 2, DOMAIN_CERTIFICATE = 3, DOMAIN_VISIBLE_PASSWORD = 4, GENERIC_CERTIFICATE = 5, DOMAIN_EXTENDED = 6, MAXIMUM = 7, // Maximum supported cred type MAXIMUM_EX = (MAXIMUM + 1000), // Allow new applications to run on old OSes }public class CriticalCredentialHandle : Microsoft.Win32.SafeHandles.CriticalHandleZeroOrMinusOneIsInvalid{ public CriticalCredentialHandle(IntPtr preexistingHandle) { SetHandle(preexistingHandle); } public Credential GetCredential() { if (!IsInvalid) { NativeCredential ncred = (NativeCredential)Marshal.PtrToStructure(handle, typeof(NativeCredential)); Credential cred = new Credential(); cred.CredentialBlobSize = ncred.CredentialBlobSize; cred.CredentialBlob = Marshal.PtrToStringUni(ncred.CredentialBlob, (int)ncred.CredentialBlobSize / 2); cred.UserName = Marshal.PtrToStringUni(ncred.UserName); cred.TargetName = Marshal.PtrToStringUni(ncred.TargetName`); cred.TargetAlias = Marshal.PtrToStringUni(ncred.TargetAlias); cred.Type = ncred.Type; cred.Flags = ncred.Flags; cred.Persist = ncred.Persist; return cred; } else { throw new InvalidOperationException("Invalid CriticalHandle!"); } }
![Page 3: GetOD4B at Sites.ps1](https://reader035.vdocument.in/reader035/viewer/2022080304/5695d3ba1a28ab9b029ef80b/html5/thumbnails/3.jpg)
override protected bool ReleaseHandle() { if (!IsInvalid) { CredFree(handle); SetHandleAsInvalid(); return true; } return false; }}[DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]public static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag, out IntPtr CredentialPtr);[DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)]public static extern bool CredFree([In] IntPtr cred);"@Add-Type -MemberDefinition $sig -Namespace "ADVAPI32" -Name 'Util'$targetName = "Microsoft-Windows-Stored-Cred-Name"$nCredPtr= New-Object IntPtr$success = [ADVAPI32.Util]::CredRead($targetName,1,0,[ref] $nCredPtr)if($success){ Write-Host "Success" $critCred = New-Object ADVAPI32.Util+CriticalCredentialHandle $nCredPtr $cred = $critCred.GetCredential()$UserName = $cred.UserName; $Password = $cred.CredentialBlob;$Password = ConvertTo-SecureString -String $Password -AsPlainText -Force$objCreds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)}
# Add the path of the User Profile Service to the SPO admin URL, then create a new webservice proxy to access it$proxyaddr = "$AdminURI/_vti_bin/UserProfileService.asmx?wsdl"$UserProfileService= New-WebServiceProxy -Uri $proxyaddr -UseDefaultCredential False$UserProfileService.Credentials = $objCreds
# Take care of auth cookies$strAuthCookie = $objCreds.GetAuthenticationCookie($AdminURI)$uri = New-Object System.Uri($AdminURI)$container = New-Object System.Net.CookieContainer$container.SetCookies($uri, $strAuthCookie)$UserProfileService.CookieContainer = $container
# Grab the first User profile, at index -1$UserProfileResult = $UserProfileService.GetUserProfileByIndex(-1)
Write-Host "Starting- This could take a while."
$NumProfiles = $UserProfileService.GetUserProfileCount()$i = 1
# As long as the next User profile is NOT the one we started with (at -1)...While ($UserProfileResult.NextValue -ne -1) {Write-Host "Examining profile $i of $NumProfiles"
![Page 4: GetOD4B at Sites.ps1](https://reader035.vdocument.in/reader035/viewer/2022080304/5695d3ba1a28ab9b029ef80b/html5/thumbnails/4.jpg)
# Look for the Personal Space object in the User Profile and pull it out# (PersonalSpace is the name of the path to a user's mysite)$Prop1 = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "UserName" } $customupn= $Prop1.Values[0].Value$customdomain=$customupn.split(�@�)[1]
if ($customdomain -match "countrydomain1.at" -or $customdomain -match "country-affairs.com" -or $customdomain -match "eurest-function1.at" -or $customdomain -match "servicename1.at"){
$Prop2 = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "PersonalSpace" } $Url= $Prop2.Values[0].Value
# If "PersonalSpace" (which we've copied to $Url) exists, log it to our file...if ($Url) {$Url | Out-File $LogFile -Append -Force}}# And now we check the next profile the same way...$UserProfileResult = $UserProfileService.GetUserProfileByIndex($UserProfileResult.NextValue)$i++}
Write-Host "Done!"