当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 十个最有用的 Office 365 PowerShell 命令

作者:精品下载站 日期:2024-12-14 06:03:21 浏览:15 分类:玩电脑

十个最有用的 Office 365 PowerShell 命令


使用 Windows PowerShell 管理 Office 365 乍一看可能看起来很奇怪。毕竟,云解决方案承诺简单性和易用性——这些形容词很少与 Windows PowerShell 一起使用。但请耐心听我说。在本文中,我将向您展示对系统管理员来说十个最有用的 Office 365 PowerShell cmdlet。也许在阅读这些说明后,您会同意 PowerShell 可以成为一个有价值的工具,即使对于基于云的系统也是如此。

1. 使用PowerShell连接Office 365实例

首先,我们需要安装 Windows PowerShell 的 Office 365 模块并连接到 Office 365 实例。采取以下步骤:

1. 下载并安装适用于 IT 专业人员 RTW 的 Microsoft Online Services 登录助手。

2. 导入 Microsoft Azure Active Directory 和 Office 365 的在线服务 PowerShell 模块:

Install-Module -Name AzureAD
Install-Module -Name MSOnline

3. 输入您的 Office 365 管理员凭据:

$Cred = Get-Credential

4. 创建远程 PowerShell 会话:

$O365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection

5. 将会话命令导入本地 Windows PowerShell 会话:

Import-PSSession $O365

6. 连接到所有 Office 365 服务:

Connect-MsolService –Credential $O365

导入 Windows PowerShell 模块后,我们就可以管理 Office 365 实例了。

2. 使用 PowerShell 连接到 Exchange Online 和 SharePoint Online

我们还可以分别连接到 Microsoft Exchange Online 和 Microsoft SharePoint Online。使用 PowerShell 连接到 Exchange Online 与连接到 Office 365 基本相同:

$Cred = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic –AllowRedirection

连接到 SharePoint Online 略有不同。为了管理您的 SharePoint Online 租户,您首先需要下载并安装 SharePoint Online Management Shell 功能。然后运行以下 PowerShell 脚本:

$admin="[email protected]"
$orgname="enterprise"
$userCred = Get-Credential -UserName $admin -Message "Type the password."
Connect-SPOService -Url https://$orgname-admin.sharepoint.com -Credential $userCred

3. 获取可用 Office 365 PowerShell cmdlet 的列表

要获取所有可用的 Office 365 PowerShell 命令的列表,我们需要运行 Get-Command cmdlet:

Get-Command -module MSOnline

[玩转系统] 十个最有用的 Office 365 PowerShell 命令

我们还可以获得 Azure Active Directory 的 cmdlet 列表:

Get-Command -module AzureAD

4. 使用 PowerShell 获取所有 Office 365 用户的列表

如果您需要提供 Office 365 用户和许可证的列表,请使用 Get-MsolUser cmdlet。它将检索 Office 365 租户中拥有有效许可证的所有用户,以及 DisplayName、City、Department 和 ObjectID 参数。

Get-MsolUser | Select DisplayName, City, Department, ObjectID

要查看帐户许可证的数量,您需要运行以下 cmdlet:

Get-MsolAccountSku

要列出可用的服务,请运行以下脚本:

Get-MsolAccountSku | select -ExpandProperty ServiceStatus

[玩转系统] 十个最有用的 Office 365 PowerShell 命令

5. 使用 PowerShell 在 Office 365 中创建新用户

要创建新用户,我们使用 New-MsolUser 命令:

New-MsolUser -UserPrincipalName [email protected] -DisplayName "John Smith" -FirstName “John” -LastName “Smith”

系统将输出用户的密码和许可证状态数据。

6. 使用 PowerShell 从所有站点删除用户

要立即从所有站点中删除用户,我们使用以下命令:

Get-SPOSite | ForEach {Remove-SPOUser -Site $_.Url -LoginName " [email protected]"}

7. 使用 PowerShell 更改 Office 365 中的密码

如果您需要更改帐户的密码,请使用 Set-MsolUserPassword cmdlet。您可以指定一个新密码,如下例所示,也可以省略 -NewPassword 参数,让系统自动生成随机密码。

Set-MsolUserPassword -UserPrincipalName [email protected] -NewPassword P@SSw0rd!

8. 使用 PowerShell 管理 Office 365 中的组成员身份

我们还可以使用 PowerShell cmdlet 管理 Office 365 组。要检索 Office 365 中所有组的列表,只需使用命令 Get-MsolGroup。要将用户添加到组,请使用 Add-MsolGroupMember 命令:

Add-MsolGroupMember -GroupObjectId 5b61d9e1-a13f-4a2d-b5ba-773cebc08eec -GroupMemberObjectId a56cae92-a8b9-4fd0-acfc-6773a5c1c767 -GroupMembertype user

GroupObjectId 是组的十六进制 ID,可以通过 Get-MsolGroup 命令获取。 GroupMemberObejctId 是用户对象 ID,您可以通过运行以下命令找到它:

Get-MsolUser | Select ObjectID.

要从组中删除用户,请使用 Remove-MsoGroupMember cmdlet。

9. 使用 PowerShell 创建 SharePoint 网站集

我们还可以使用 PowerShell 创建 SharePoint 网站集:

New-SPOSite -Url "https://enterprise.sharepoint.com/sites/NewSite" -Owner "[email protected]" -StorageQuota "100" -Title "New Site"

10. 使用 PowerShell 在 Office 365 中创建报告

PowerShell 是制作不同报告的绝佳工具。以下是通过 PowerShell 完成的一些有用的 Office 365 报告:

  • 所有邮箱的详细信息:
Get-mailbox | get-MailboxStatistics
  • 过去 30 天内未登录的所有邮箱的列表:
Get-Mailbox –RecipientType 'UserMailbox' | Get-MailboxStatistics | Sort-Object LastLogonTime | Where {$_.LastLogonTime –lt ([DateTime]::Now).AddDays(-30) } | Format-Table DisplayName, LastLogonTime
  • 有关最高数量发件人和收件人的报告:
Get-MailTrafficTopReport
  • 关于所有团体及其成员的报告:
function Get-AllO365Members
{
 Try
 {
 $O365Groups=Get-UnifiedGroup
 foreach ($O365Group in $O365Groups)
 {
 Write-Host "Group Membership: " $O365Group.DisplayName -ForegroundColor Green
 Get-UnifiedGroupLinks –Identity $O365Group.Identity –LinkType Members
 Write-Host
 }
 }
 catch [System.Exception]
 {
 Write-Host -ForegroundColor Red $_.Exception.ToString()
 }
}
Get-AllO365Members

请注意,大多数报告 cmdlet 已于 2018 年 1 月弃用,并由新的 MS Graph 报告 API 取代。因此,某些报告现在仅在 Office 365 安全与合规中心中可用。

如您所见,使用 PowerShell 进行 Office 365 管理既快速又简单,就像在 Microsoft Windows Server 中一样。不要忘记审核您对 Office 365 环境所做的所有更改;它将帮助您更快地排除故障并从问题中恢复。要了解如何在 Office 365 环境中配置本机审核,请查看我们的 Exchange Online 审核快速参考指南和 Exchange Online 邮箱审核快速参考指南。如果您想要有关 Office 365 环境中发生的情况的可操作情报,以及灵活的报告、主动提醒 Google 式搜索等功能,请申请免费试用 Netwrix Auditor for Office 365。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯