ForEachLoop 在 PowerShell 中不工作,但我有完全访问权限,这是什么问题?

ForEachLoop not working in PowerShell, but I have full access what is the issue?

enter image description here

 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$count
#$farm = Get-SPFarm
$siteList = Get-SPSite -Limit 1
$serviceContext = Get-SPServiceContext($siteList[0])
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($servicecontext)
$profiles = $profileManager.GetEnumerator()
foreach ($hi in $profiles) {
    if([string]::IsNullOrEmpty($hi["AboutMe"].Value) -eq $false){
        write-host "Acount Name:"$hi.AccountName"|AboutMe:"$hi["AboutMe"].Value
        $count++
    }
}
write-host $count

我不断收到 foreachloop 错误,以下错误。当我输入 SP-Farm 时,我恢复了所有内容,所以这不会是访问问题?

UserProfileDBCache_WCFLogging :: ProfileDBCacheServiceClient.GetUserData threw exception: Access is denied.
At line:8 char:10
+ foreach ($hi in $profiles) {
+          ~~~
    + CategoryInfo          : OperationStopped: (:) [], UserProfileApplicationNotAvailableException
    + FullyQualifiedErrorId : Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException

以下PowerShell供您参考。

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$count
$siteList = Get-SPSite -Limit 1
$serviceContext = Get-SPServiceContext($siteList[0])
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)
$profiles = $profileManager.GetEnumerator()
foreach ($profile in $profiles) {
    if([string]::IsNullOrEmpty($profile["AboutMe"].Value) -eq $false){
        write-host "Acount Name:"$profile.AccountName"|AboutMe:"$profile["AboutMe"].Value
        $count++
    }
}
write-host $count