仅在必要时连接 SPOService
Connect-SPOService Only When Necessary
我正在为我的 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
}
根据我对 SPOService 连接的了解,一旦您打开它,它就会保持连接状态,直到您使用 Disconnect-SPOService 关闭它或关闭会话。
您可以将所有函数添加到同一个脚本并在执行您的工作之前调用 Connect-Function。
我的理解正确吗?
有什么事告诉我
这都是理论上的,因为我无法访问 SPO
function Test-SPOConnected {
[CmdletBinding()]
param()
Write-Verbose 'Testing Connection to SharePoint Online'
try {
# Choose a command that you know should return something if you are connected,
# preferably only a small amount of objects, or error otherwise
# Based off documentation I've choosen Get-SPOSite
# however there could be a better option
$results = Get-SPOSite -ErrorAction Stop
If ($results) {
Write-Verbose 'Successfully connected to SharePoint Online.'
$true
}
else {
Write-Warning 'Nothing returned. Not connected to SharePoint Online.'
$false
}
}
catch {
Write-Warning 'Connection to SharePoint Online failed.'
$false
}
}
然后在其他 code/functions 中,您可以使用 if 语句检查连接
if (!Test-SPOConnected) {
Connect-Function
}
# do SPO stuff
我正在为我的 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
}
根据我对 SPOService 连接的了解,一旦您打开它,它就会保持连接状态,直到您使用 Disconnect-SPOService 关闭它或关闭会话。 您可以将所有函数添加到同一个脚本并在执行您的工作之前调用 Connect-Function。
我的理解正确吗? 有什么事告诉我
这都是理论上的,因为我无法访问 SPO
function Test-SPOConnected {
[CmdletBinding()]
param()
Write-Verbose 'Testing Connection to SharePoint Online'
try {
# Choose a command that you know should return something if you are connected,
# preferably only a small amount of objects, or error otherwise
# Based off documentation I've choosen Get-SPOSite
# however there could be a better option
$results = Get-SPOSite -ErrorAction Stop
If ($results) {
Write-Verbose 'Successfully connected to SharePoint Online.'
$true
}
else {
Write-Warning 'Nothing returned. Not connected to SharePoint Online.'
$false
}
}
catch {
Write-Warning 'Connection to SharePoint Online failed.'
$false
}
}
然后在其他 code/functions 中,您可以使用 if 语句检查连接
if (!Test-SPOConnected) {
Connect-Function
}
# do SPO stuff