使用PowerShell獲取 O365 Onedrive for business列表

    年也過完了,今天也該開工了,再來分享一個關於O365的腳本,之前分享過一個可以批量開啓用戶Onedrive for business的腳本,對於做一些遷移的工作來說會比較有幫助,今天再來分享一個如何獲取用戶開啓Onedrive for business情況的腳本,在O365 admin portal推出報表的功能以前,這個腳本用來查看用戶開啓onedrive for business的情況是非常有幫助的,現在來說我們也可以通過這個腳本來查看實時的狀態信息


    大概的使用方法很簡單,這個腳本是微軟官方提供的,會調用一些SharePoint的API,本身邏輯不會很複雜


    腳本的修改基本都是在腳本內部先修改完成的,所以運行的時候基本只要直接執行就可以了

圖像 002.png


    之後可以看到運行結束了

圖像 003.png


    之後去指定好的位置查看結果即可,可以看到目前開啓了Onedrive for business的人是以下這些,和我目前環境中的情況是一致的

 圖像 004.png



    下邊是具體的代碼

# Specifies the URL for your organization's SPO admin service
$AdminURI = "https://your organization name-admin.sharepoint.com"

# Specifies the User account for an Office 365 global admin in your organization
$AdminAccount = "global admin account"
$AdminPass = "password for global admin account"

# Specifies the location where the list of MySites should be saved
$LogFile = 'C:\Users\youralias\Desktop\ListOfMysites.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")

# Convert the Password to a secure string, then zero out the cleartext version ;)
$sstr = ConvertTo-SecureString -string $AdminPass -AsPlainText –Force
$AdminPass = ""

# Take the AdminAccount and the AdminAccount password, and create a credential

$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminAccount, $sstr)


# 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 = $creds

# Set variables for authentication cookies
$strAuthCookie = $creds.GetAuthenticationCookie($AdminURI)
$uri = New-Object System.Uri($AdminURI)
$container = New-Object System.Net.CookieContainer
$container.SetCookies($uri, $strAuthCookie)
$UserProfileService.CookieContainer = $container

# Sets 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"

# Look for the Personal Space object in the User Profile and retrieve it
# (PersonalSpace is the name of the path to a user's OneDrive for Business site. Users who have not yet created a 
# OneDrive for Business site might not have this property set.)
$Prop = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "PersonalSpace" } 
$Url= $Prop.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!"



可以看到裏邊有一些內容是需要修改的,比如admin url,賬號密碼,保存的位置等等,可以根據自己的實際情況進行修改,因爲本身腳本需要修改的內容其實不多,所以也沒花時間把他改成參數化的形式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章