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

[玩转系统] 10 个最常用的 Office 365 PowerShell 命令

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

10 个最常用的 Office 365 PowerShell 命令


[玩转系统] 10 个最常用的 Office 365 PowerShell 命令

对于 Office 365 管理员来说,PowerShell 仍然是一个有价值的工具。尽管云解决方案意味着简单性和易用性(这不是通常描述 Windows PowerShell 的方式),但仍有一些对系统管理员有用的 Office 365 PowerShell cmdlet。此外,管理员有时会发现,键入几行代码即可更轻松地获得所需内容,而不是在 Office 365 门户中搜索众多可用选项。查看这些命令后,您会发现 PowerShell 即使使用起来并不简单,也可以是一个有价值的工具,即使对于基于云的系统也是如此。

使用 PowerShell 进行 Office 365 报告的另一种更直接的解决方案是使用适用于 Office 365 的 Lepide Auditor,我们将在本文末尾介绍它。

10 个最常用的 Office 365 PowerShell 命令

以下是 Office 365 中十个最常用的 PowerShell cmdlet 的列表。

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=获取凭证
  4. 连接到所有 Office 365 服务:
    Connect-MsolService -凭证 $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
Connect-ExchangeOnline -Credential $Credential -ShowBanner:$False

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

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

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

运行 Get-Command cmdlet 以获取所有可用 Office 365 PowerShell 命令的列表:
Get-Command -模块 MSOnline

[玩转系统] 10 个最常用的 Office 365 PowerShell 命令

我们还可以获得 Azure Active Directory 的 cmdlet 列表:
获取命令-模块 AzureAD

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

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

Get-MsolUser -maxresults 100 | Select DisplayName, Department, ObjectID
Get-MsolUser -EnabledFilter EnabledOnly -MaxResults 100 

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

要列出可用的服务,请运行以下脚本:
获取 MsolAccountSku |选择-ExpandProperty ServiceStatus

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

要创建新用户,我们使用 New-MsolUser 命令:
New-MsolUser -UserPrincipalName [email protected] -DisplayName“John Smith”-FirstName“John”-LastName“Smith”

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

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

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

$AdminSiteURL="https://lepidesoftware-admin.sharepoint.com"
$UserAccount = "[email protected]"
Connect-SPOService -URL $AdminSiteURL -Credential (Get-Credential)
$SitesCollections = Get-SPOSite -Limit ALL
ForEach($Site in $SitesCollections)
{
    Write-host -f Yellow "Checking Site Collection:"$Site.URL
    $User = Get-SPOUser -Limit All -Site $Site.URL | Where {$_.LoginName -eq $UserAccount}
    If($User)
    {
        Remove-SPOUser -Site $Site.URL -LoginName $UserAccount
        Write-host -f Green "`tUser $($UserAccount) has been removed from Site collection!"
    }
}

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

如果需要更改帐户的密码,请使用 Set-MsolUserPassword cmdlet。

您可以指定一个新密码,如下例所示,也可以省略 -NewPassword 参数,让系统自动生成随机密码。

Set-MsolUserPassword -UserPrincipalName [email protected] -NewPassword 2wsx@WSX@12

8. 使用 PowerShell 管理 Office 365 中的组成员资格

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

此命令给出租户中的所有用户组:
Connect-MsolService
获取 MsolGroup

要将成员添加到 Office 365 组:

$Credential = Get-Credential
Connect-ExchangeOnline -Credential $Credential -ShowBanner:$False
Add-UnifiedGroupLinks -Identity "[email protected]" -LinkType "Members" -Links "[email protected]"

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

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

9. 使用 PowerShell 创建 SharePoint 网站集

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

Connect-SPOService  -url https://lepidesoftware-admin.sharepoint.com
New-SPOSite -Url "https://lepidesoftware.sharepoint.com/sites/PowershellSite1" -Owner "[email protected]" -StorageQuota "100" -Title "Powershell Site1"

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

PowerShell 可用于创建不同的报告。以下是通过 PowerShell 完成的一些有用的 Office 365 报告:

所有邮箱的详细信息:
获取邮箱|获取邮箱统计

过去 30 天内未登录的所有邮箱的列表:

get-mailbox -resultsize unlimited |Get-MailboxStatistics  | where {$_.LastLogonTime -lt (get-date).AddDays(-7)} | ft displayName,lastlogontime,lastloggedonuseraccount,servername

有关最高数量发件人和收件人的报告:
获取 MailTrafficSummaryReport -类别 TopSpamRecipient

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

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

取消回复欢迎 发表评论:

关灯