使用 PowerShell 与 Google Sheets API 交互

问题描述 投票:0回答:1

我有一个 Azure 自动化运行手册,当我的基础设施上发生某些违规/事件时,该运行手册就会启动。我希望能够创建一个 Google 电子表格并将数据存储在电子表格中以用于其他故障排除日志。由于我使用的是 Azure 自动化,因此如果可能的话,我首选在 Powershell 中执行此操作。 Python可以用,但是不太理想。

是否有一种相当有效的方式使用 Powershell 与 Google Workspace API(在本例中为sheets api)进行交互?值得这么麻烦吗,还是应该硬着头皮使用 Python?


奖励:我很好奇,所以我在 ChatGPT 中查找了同样的问题,这就是它的结果。

# Install the Google API client library for PowerShell
Install-Package Google.Apis -ProviderName NuGet

# Import the necessary Google API libraries
Import-Module Google.Apis.Sheets.v4

# Set up the Google API credentials
$credentialsFilePath = "C:\path\to\credentials.json"
$clientSecretsFilePath = "C:\path\to\client_secret.json"
$credential = Get-GoogleOAuth2Credential -ClientSecretsFilePath $clientSecretsFilePath -CredentialsFilePath $credentialsFilePath -Scopes "https://www.googleapis.com/auth/spreadsheets"

# Set up the Google Sheets API service
$service = New-GoogleSheetsService -Credential $credential

# Define the new spreadsheet's properties
$spreadsheet = New-Object Google.Apis.Sheets.v4.Data.Spreadsheet
$spreadsheet.Properties = New-Object Google.Apis.Sheets.v4.Data.SpreadsheetProperties
$spreadsheet.Properties.Title = "New Spreadsheet"

# Create the new spreadsheet
$spreadsheet = $service.Spreadsheets.Create($spreadsheet).Execute()

# Print the URL of the new spreadsheet
Write-Host "New spreadsheet created: $($spreadsheet.SpreadsheetUrl)"

这是一个合理的方向,还是另一个人工智能失误?我正在阅读 PowerShell 中 NuGet 提供程序的文档,它似乎相当不可靠,在安装包依赖项时始终存在问题。另外,当我查看 Google.Apis.Sheets.v4 库的文档时,它似乎没有提供 Chat-GPT 所说的方法......不过可能只是我。很高兴获得第二意见?

对于任何好奇的人,我正在查看的“Google.Apis.Sheets.v4”在这里:https://googleapis.dev/dotnet/Google.Apis.Sheets.v4/latest/api/Google.Apis.Sheets .v4.SpreadsheetsResource.CreateRequest.html

google-sheets google-api google-workspace
1个回答
0
投票

我还不知道如何投票或回复另一个现有回复,但这最近对我有帮助。 我正在开发一个 PowerShell 脚本,该脚本将 AD 用户列表(按组或 OU 过滤)提取到 CSV 文件,然后将其上传到 Google 表格。 ChatGPT 也对此提供了帮助,但是 Bryan M 引用的文章绝对有帮助。

我发现了这个,它可以帮助你找到正确的方向 jamesachambers.com/modify-google-sheets-using-powershell – 布莱恩 蒙特罗萨 2023 年 4 月 4 日 11:39

© www.soinside.com 2019 - 2024. All rights reserved.