用Windows PowerShell 控制管理 Microsoft Office 365

如果想要通过PowerShell控制管理Office365,首先要安装Microsoft Online Services Sign-In Assistant 7.0,链接如下

之后,安装Microsoft Online Services Module for Windows PowerShell,链接如下

如果是管理Office365对应的AD环境,用以下code进行连接:

function connectServer($adminName, $password){
    write-host "connecting to outlook.office365.com..."
    $O365Cred = New-Object System.Management.Automation.PSCredential($adminName,$password);
    $O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell -Credential$O365Cred -Authentication Basic -AllowRedirection
    Import-PSSession $O365Session
    Connect-MsolService -Credential $O365Cred
}

对应的管理AD的users和groups的cmdlet的例子如下:

New-MsolUser -UserPrincipalName $principalName -DisplayName $displayName -PasswordNeverExpires $true -Password $userPassword

New-MsolGroup -DisplayName $displayName -Description $description

Add-MsolGroupMember -groupObjectid $parentGroup.ObjectId -GroupMemberType "Group" -GroupMemberObjectId $childGroup.ObjectId

addChildGroup -parent $i -childOfTree $child -groupPrefix $groupPrefix -groupList $groupList

Add-MsolGroupMember -groupObjectid $group.ObjectId -GroupMemberType "User" -GroupMemberObjectId $user.ObjectId

Remove-MsolUser -objectid $ObjectId -force

Remove-MsolGroup -objectid $ObjectId -force

如果是管理Office365上面Exchanger,用以下code进行连接:

function connectServer1($adminName, $password){
    write-host "connecting to ps.outlook.com..."
    Import-Module MSOnline
    $sessionOption = New-PSSessionOption -SkipRevocationCheck
    $O365Cred = New-Object System.Management.Automation.PSCredential($adminName,$password);
    $O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection 
    Import-PSSession $O365Session -AllowClobber 
    Connect-MsolService -Credential $O365Cred
}

对应的管理Exchanger的users和groups的cmdlet的例子如下:

New-MsolUser -UserPrincipalName $principalName -DisplayName $displayName -PasswordNeverExpires $true -Password $userPassword -LicenseAssignment $licenses -UsageLocation "US" -forcechangepassword $false

New-DistributionGroup -Name $name -DisplayName $displayName

New-DistributionGroup -Name $name -DisplayName $displayName -Type security

Add-DistributionGroupMember -groupObjectid $parentGroup.ObjectId -GroupMemberType "Group" -GroupMemberObjectId $childGroup.ObjectId

addDistributionChildGroup -parent $i -childOfTree $child -groupPrefix $groupPrefix -groupList $groupList

Add-DistributionGroupMember -Identity $group.Name -Member $user.Name

Add-DistributionGroupMember -Identity $group2.Name -Member $group.Name

Remove-DistributionGroup -Identity $Name -confirm:$false

Get-DistributionGroupMember -Identity $group.name

参考资料:

http://blog.crayon.no/blogs/janegil/archive/2011/05/10/administering_2D00_microsoft_2D00_office_2D00_365_2D00_using_2D00_windows_2D00_powershell.aspx

原文地址:https://www.cnblogs.com/randyyang/p/4264224.html