Azure 自动化 PowerShell 从 SharePoint 读取 CSV 文件

Azure Automation PowerShell read CSV file from SharePoint

提问人:Jeroen Budding 提问时间:11/3/2023 更新时间:11/23/2023 访问量:39

问:

添加我能够使用 Azure 自动化从 SharePoint 读取文件的那一刻。 对我来说,问题是当我进行导入时,我丢失了CSV上的所有属性值。 因此,以下文件具有属性 date,time,id,name 当我查询$GetCSV我得到所有记录时,当我查询 $GetCSV.id 时,它没有显示任何数据。

#Obtain AccessToken for Microsoft Graph via the managed identity
$headers = Get-AuthToken
$graphurl = "https://graph.microsoft.com/beta"

#Sharepoint parameters
$SharePoint = "%Company%.sharepoint.com"
$SharePointSite = "%Site%"
$ItemPathFile = "file.csv"
$ItemPath = "Apps/Entra%20Automation/$ItemPathFile"

#Get the site from Graph API
$SharePointsitequery = "$graphurl/sites/$($SharePoint):/sites/$($SharePointSite)"
$SiteRequestCall = try{Invoke-RestMethod -Method GET -headers $headers -Uri $SharePointsitequery}catch{Write-output "Error SharePoint SiteId $($_.Exception.Response.StatusCode) $($_.Exception.Response.StatusDescription)"}
$SiteId = $SiteRequestCall.id.split(",")[1]

#Get the file to the documents library
$Querygetfile = "$graphurl/sites/$siteId/drive/items/root:/$($ItemPath):/content"
$GetCSV = try{Invoke-RestMethod -Method GET -Headers $headers -Uri $Querygetfile }catch{Write-output "Error SharePoint Get $($_.Exception.Response.StatusCode) $($_.Exception.Response.StatusDescription)"}
Powershell Azure 自动化 读取 .csv 调用 REST方法

评论


答:

0赞 Venkatesan 11/23/2023 #1

当我查询$GetCSV我得到所有记录时,当我查询 $GetCSV.id 时,它没有显示任何数据。

可以使用下面修改的脚本通过 Azure 自动化从 CSV 文件中获取特定记录。

在我的共享点中,我的示例 CSV 文件如下所示:

enter image description here

脚本:

$headers = Get-AuthToken
$graphurl = "https://graph.microsoft.com/beta"

#Sharepoint parameters
$SharePoint = "%Company%.sharepoint.com"
$SharePointSite = "%Site%"
$ItemPathFile = "file.csv"
$ItemPath = "Apps/Entra%20Automation/$ItemPathFile"

#Get the site from Graph API
$SharePointsitequery = "$graphurl/sites/$($SharePoint):/sites/$($SharePointSite)"
$SiteRequestCall = try{Invoke-RestMethod -Method GET -headers $headers -Uri $SharePointsitequery}catch{Write-output "Error SharePoint SiteId $($_.Exception.Response.StatusCode) $($_.Exception.Response.StatusDescription)"}
$SiteId = $SiteRequestCall.id.split(",")[1]

#Get the file to the documents library
$Querygetfile = "$graphurl/sites/$siteId/drive/items/root:/$($ItemPath):/content"
$GetCSV = try{Invoke-RestMethod -Method GET -Headers $headers -Uri $Querygetfile }catch{Write-output "Error SharePoint Get $($_.Exception.Response.StatusCode) $($_.Exception.Response.StatusDescription)"}


$CSV = $GetCSV | ConvertFrom-Csv -Delimiter ","

# Access the properties of the CSV file
$Numeric = $CSV.Numeric
$Numeric2 = $CSV.'Numeric-2'
$NumericSuffix = $CSV.'Numeric-Suffix'

$Numeric

上面的代码在我的 Azure 自动化环境中执行并读取第一列。

输出:

enter image description here