【问题标题】:Set-Msoluser : Access Denied. You do not have permissions to call this cmdletSet-Msoluser:拒绝访问。您无权调用此 cmdlet
【发布时间】:2020-10-07 21:36:32
【问题描述】:

更新

我已成功连接到 Office 365 管理服务和 Exchange Online Services。我已经测试了 Get-MsolUser 等 cmdlet,它们工作正常。但是,当我尝试运行命令 Set-MsolUser 来更改标题时,我收到了 Access Denied 错误,如下所示。这很奇怪,因为我可以手动进入 Exchange 并更改我想要的任何属性,但它不会让我运行这个命令?有什么办法吗?

更新 Office 365 用户属性的脚本

## Variables
$systemmessagecolor = "cyan"
$processmessagecolor = "green"
$savedcreds=$false                      ## false = manually enter creds, True = from file
$credpath = "c:\downloads\tenant.xml"   ## local file with credentials if required

## If you have running scripts that don't have a certificate, run this command once to disable that level of security
## set-executionpolicy -executionpolicy bypass -scope currentuser -force

Clear-Host

write-host -foregroundcolor $systemmessagecolor "Script started`n"

#install-module msonline
Import-Module -Name "C:\Temp\MsOnline" -Verbose
write-host -foregroundcolor green "MSOnline module loaded"

## Get tenant login credentials
$cred = Get-Credential


## Connect to Office 365 admin service
connect-msolservice -credential $cred
write-host -foregroundcolor $systemmessagecolor "Now connected to Office 365 Admin service"

## Start Exchange Online session
$EXOSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell -Credential $cred -Authentication Basic -AllowRedirection
import-PSSession $EXOSession -AllowClobber
write-host -foregroundcolor $processmessagecolor "Now connected to Exchange Online services`n"
write-host -foregroundcolor $systemmessagecolor "Script Completed`n"


# Load data from file.csv
$EXUsers = Import-csv file_path.csv


# Count variable for number of users update
$count = 0

# Go through each row that has user data in the CSV we just imported 
ForEach($User in $EXUsers)
{
    # Ppopulate hash table for Get-Msoluser splatting:
    $GetParams =
    @{
        UserPrincipalName     = $User.userPrincipalName
    }

    # Initialize hash table for Set-Msoluser splatting:
    $SetParams =
    @{
        UserPrincipalName     = $User.userPrincipalName
        Title                 = $User.title
    }

    # Get user and update.
    if ( Get-Msoluser @GetParams)
    {
         # Set User attributes
         Set-MsolUser @SetParams

         # Print that the user was updated 
         Write-Host -ForegroundColor Yellow "$User - User attributes have been updated." 

         # Update Count
         $count += 1    
     }
}

# Print the number of updated users
Write-Host $count "Users have been updated" -ForegroundColor Green

错误信息:

Set-Msoluser : Access Denied. You do not have permissions to call this cmdlet.
At line:1 char:59
+ ... ncipalName "name@company.com" | Set-Msoluser -Title "Test Title"
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Set-MsolUser], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.AccessDeniedException,Microsoft.Online.Administration.Automation.SetUser

【问题讨论】:

  • 您的SetParams 包含$description 作为键而不是description
  • @AdminOfThings 那是我的错 LOL:stackoverflow.com/a/62434489/4749264 感谢您的指出。我在另一个答案中更正了它。
  • @AdminOfThings 和 Steven 我确实注意到并修复了它。我的问题仍然适用:)
  • @AdminOfThings 请查看更新后的代码。

标签: powershell scripting active-directory exchange-server


【解决方案1】:

已解决

通过在Exchange 命令行管理程序

中运行脚本解决了访问被拒绝问题

还进行了这些更改以使脚本正常工作:

  1. principleUserName -> 身份
  2. 获取 MsolUser -> 获取邮箱
  3. 设置-MsolUser -> 设置-用户
## Variables
$systemmessagecolor = "cyan"
$processmessagecolor = "green"

# Load data from file.csv
$EXUsers = Import-csv file_path.csv

# Count variable for number of users update
$count = 0

# Go through each row that has user data in the CSV we just imported 
ForEach($User in $EXUsers)
{
    # Ppopulate hash table for Get-Msoluser splatting:
    $GetParams =
    @{
        Identity = $User.Identity
    }

    # Initialize hash table for Set-Msoluser splatting:
    $SetParams =
    @{
        Identity = $User.Identity
        Title    = $User.Title
    }

    # Get user and update.
    if ( Get-Mailbox @GetParams)
    {
         # Set User attributes
         Set-User @SetParams

         # Print that the user was updated 
         Write-Host -ForegroundColor Yellow "$User - User attributes have been updated." 

         # Update Count
         $count += 1    
     }
}

# Print the number of updated users
Write-Host $count "Users have been updated" -ForegroundColor Green

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 2019-05-09
    • 1970-01-01
    • 2023-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多