Azure Automation读取SharePoint CSV后无法获取属性值问题
已通过Azure Automation成功读取SharePoint中的CSV文件,但导入后所有属性值丢失。该CSV包含date、time、id、name字段,执行$GetCSV能看到完整记录,但执行$GetCSV.id时无数据返回。
用户当前使用的PowerShell代码:
#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)"}
原因分析
当前通过Invoke-RestMethod请求文件/content端点时,返回的是纯文本格式的CSV内容,而非PowerShell可直接访问的对象数组。因此$GetCSV存储的是整个CSV的字符串内容,无法通过.id这种属性访问方式提取字段值。
解决方案
需要将返回的纯文本CSV内容转换为PowerShell对象数组,修改获取文件内容的代码部分即可:
#Get the file to the documents library $Querygetfile = "$graphurl/sites/$siteId/drive/items/root:/$($ItemPath):/content" $GetCSV = try{ # 获取纯文本CSV内容 $csvText = Invoke-RestMethod -Method GET -Headers $headers -Uri $Querygetfile # 将文本转换为PowerShell对象数组 $csvText | ConvertFrom-Csv }catch{ Write-output "Error SharePoint Get $($_.Exception.Response.StatusCode) $($_.Exception.Response.StatusDescription)" }
验证
修改后执行$GetCSV会显示结构化的对象数组,每个对象包含date、time、id、name属性。此时执行$GetCSV.id即可正确返回所有id字段的值。
内容的提问来源于stack exchange,提问作者Jeroen Budding
相关产品推荐
相关产品推荐

