【发布时间】:2021-05-24 03:20:36
【问题描述】:
我正在为我的 SharePoint Online 实例编写管理脚本,并试图弄清楚如何防止与 SPO 服务的不必要连接。
例如,我有 5 个职能,每个执行一个行政职能。显然,每一个都要求我已经成功连接到 SPO 服务,然后才能运行其余的。
如果我打开脚本的目的是运行多个函数,我不想连接多次。
有没有办法让我在再次连接之前检查连接是否已经建立?
示例代码:
function Admin-Function_A{
Write-Host "Connecting to SharePoint Online" -ForegroundColor White
try {
Connect-Function
Write-Host "Successfully connected to SharePoint Online." -ForegroundColor Green
} catch {
Write-Host "Connection to SharePoint Online failed." -ForegroundColor Red
}
}
function Connect-Function{
# Import the module
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking
# Load credential
$credential = Get-Credential -Message "Enter admin account password" -UserName $adminUsername
Connect-SPOService -Url $adminUrl -Credential $credential
}
【问题讨论】:
标签: powershell sharepoint sharepoint-online