[玩转系统] 从 PowerShell 调用 SharePoint Online 中的 REST API 方法
作者:精品下载站 日期:2024-12-14 14:59:51 浏览:15 分类:玩电脑
从 PowerShell 调用 SharePoint Online 中的 REST API 方法
要求:从 PowerShell 调用 SharePoint Online REST API。
PowerShell 在 SharePoint Online 中调用 REST API 方法
对于那些不熟悉这个概念的人来说,REST 代表表述性状态传输 (Representational State Transfer),它是一种可以通过 Internet 等网络提供计算机系统之间的互操作性的体系结构。 REST API 是一种提供 SharePoint 数据和功能访问的 Web 服务。它可用于许多不同的目的,例如构建自定义应用程序、创建用于自动化任务的一次性脚本或与其他系统集成。您可以使用任何支持 HTTP 请求和响应的编程语言。本博文将向您展示如何从 PowerShell 调用 SharePoint Online REST API 并以 JSON 格式返回结果。
以下是使用 PowerShell 调用 SharePoint Online REST API 的方法:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Get-SPOUserProfileProperty ($SiteURL, $UserID)
{
#Setup Credentials to connect
$Cred = Get-Credential
#Connect to Site
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
$Context.ExecuteQuery()
#Frame REST API URL
$RequestUrl = "$($SiteUrl)/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='i:0%23.f|membership|$($UserID)'"
$AuthenticationCookie = $Context.Credentials.GetAuthenticationCookie($SiteUrl, $true)
$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$WebSession.Credentials = $Context.Credentials
$WebSession.Cookies.SetCookies($SiteUrl, $AuthenticationCookie)
$WebSession.Headers.Add("Accept", "application/json;odata=verbose")
#Invoke Rest Method
$Result = Invoke-RestMethod -Method Get -WebSession $WebSession -Uri $RequestURL
Return ($Result.d.userprofileproperties.results)
}
#Set Parameters
$SiteURL="https://Crescent.sharepoint.com"
$UserID="[email protected]"
#Call the function to get user profile properties
Get-SPOUserProfileProperty -SiteUrl $SiteUrl -UserID $UserID | Select Key, Value
这将检索给定用户的所有用户配置文件属性
SharePoint Online:PnP PowerShell 调用 REST API
我们可以使用 Invoke-PnPSPRestMethod 从 PnP PowerShell 调用 REST API 方法:
#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
#Connect to the Site
Connect-PnPOnline -Url $SiteURL -Interactive
#Define the Rest Method
$RestMethodURL = $SiteURL+'/_api/web/lists?$select=Title'
#Invoke Rest Call to Get All Lists
$Lists = Invoke-PnPSPRestMethod -Url $RestMethodURL
$Lists.value
请注意,REST API 方法区分大小写! ($Lists.value 和 $Lists.Value 不同。)
调用 REST API 并将数据导入 SharePoint 列表
在另一种情况下,我必须从 Crowdstrike REST API 中提取数据并将数据添加到 SharePoint Online 列表。
#Set connection parameters
$client_id = "076f1qed3b4249e9b15cc1bf3ca11ff2"
$client_secret = "S19s5P3Zq7riLw22236TH3cxXXVeNhzdQ4bnalj0x"
$OAuthTokenEndpoint = "https://api.eu-1.crowdstrike.com/oauth2/token"
$RESTEndPoint = "https://api.eu-1.crowdstrike.com/devices/entities/devices/v2?offset=0&limit=5000&sort=status.desc"
#Config Variables for List
$SiteURL = "https://Crescent.sharepoint.com/sites/Monitoring"
$ListName = "CrowdStrike Hosts Report"
$AuthBody = @{
client_id = $client_id
client_secret = $client_secret
grant_type = "client_credentials"
}
#Get the Bearer Token
Try {
$TokenRequest = Invoke-WebRequest -Method Post -Uri $OAuthTokenEndpoint -ContentType "application/x-www-form-urlencoded" -Body $AuthBody -UseBasicParsing -ErrorAction Stop
}
Catch {
Write-Host "Unable to get the access token, aborting..."; Return
}
#The Token endpoint returns a JSON containing the bearer token
$Token = ($tokenRequest.Content | ConvertFrom-Json).access_token
#Frame Authentication header
$AuthHeader = @{
'Authorization'="bearer $Token"
'accept' = "application/json"
}
#Call a REST API with the bearer token in the header
$Hosts = Invoke-WebRequest -Headers $AuthHeader -Uri $RESTEndPoint -UseBasicParsing
#Get the "Resources" from the JSON
$Resources = ($Hosts.Content | ConvertFrom-Json).Resources
#Frame Authentication header
$Header = @{
'Authorization'="bearer $Token"
'accept' = "application/json"
'Content-Type'= 'application/json'
}
$Body = @{'ids' = $Resources} | ConvertTo-Json
$RESTEndPoint = "https://api.eu-1.crowdstrike.com/devices/entities/devices/v2"
$Response = Invoke-WebRequest -Method POST -Uri $RESTEndPoint -Headers $Header -Body $Body
#Get Hosts Data
$HostsDataCollection = ($Response.Content | ConvertFrom-Json).Resources
Try {
#Connect to the Site
Connect-PnPOnline -URL $SiteURL -Interactive
#Clear all items in the list
Get-PnPList -Identity $ListName | Get-PnPListItem -PageSize 100 -ScriptBlock { Param($items) Invoke-PnPQuery } | ForEach-Object {$_.Recycle() }
#Loop through each Row in the REST Response Add Item to SharePoint List
ForEach($HostData in $HostsDataCollection)
{
#Frame the List Item to update
$ItemValue = @{}
$AllFields = $HostData.PSObject.Properties | Select -ExpandProperty Name
ForEach($Row in $HostData.PSObject.Properties)
{
#Get Source Field Value and add to Hashtable
If($Row.TypeNameOfValue -eq "System.Object[]")
{
$ItemValue.Add($Row.Name,$Row.Value -join ",")
}
Else
{
$ItemValue.Add($Row.Name,$Row.Value)
}
}
Write-host "Adding List item with values:"
#Add New List Item
Add-PnPListItem -List $ListName -Values $ItemValue | Out-Null
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[短剧] 2025年05月31日 精选+付费短剧推荐58部
[软件合集] 25年5月31日 精选软件66个
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[韩剧] 宝物岛/宝藏岛/金银岛(2025)【全16集】【朴炯植/悬疑】
[电影] 愤怒的牦牛 (2025) 国语中字 4k
[短剧合集] 2025年05月30日 精选+付费短剧推荐56部
[软件合集] 25年5月30日 精选软件26个
[软件合集] 25年5月29日 精选软件18个
[短剧合集] 2025年05月28日 精选+付费短剧推荐38部
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[美图] 2W美女个美女小姐姐,饱眼福
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag