需要将简单的 Powershell 输出附加到 Google sheet
Need simple Powershell output appended to Google sheet
我很难完成我认为应该是一项简单的任务。每周,我们的团队都会向 VMware vCenter 查询三部分输出:三个不同位置的 VM 计数。这是它的样子:
Name Value
---- -----
locationA 1433
locationB 278
locationC 23
信息通过电子邮件发送给我们的团队,以及一些喜欢看数据的高层。这一切都是通过 Powershell 脚本和 Windows 任务计划程序 运行 在服务器上自动完成的,没问题。
那个数据也放在一个Googlesheet中。我们只需在新行中附加日期,然后将数据复制并粘贴到现有的三个列中。它需要 30 秒,每周一次。考虑到将其复制到 Google sheet 所花费的时间很少,这看起来很愚蠢,但我真的想使用 Google Sheets API.[=14= 自动执行最后一个过程]
我似乎在 Google 访问和编辑 Google sheet 的脚本中一直在寻找和追寻在线的徒劳无功。我已经下载并安装了 Sheets API 库、Drive API 库、Google .net 库、设置了 Google 开发者站点,以及 运行通过 Google sheets API 文档和 OAuth 身份验证。我正在使用 Visual Studio 2013,因为我认为它最适合 Powershell 并调用 .net 命令。
我几乎没有 Powershell 之外的编码经验(如果你可以称之为编码)。我什至不知道如何拉 Google sheet,更不用说对它做任何事情了。到目前为止,我尝试过的任何方法都不起作用,而且每周手动复制此信息所花的时间很少,我已经花费了比可能值得的更多时间。我觉得如果我能解决这个问题,那将为将来进一步 Google 自动化打开大门,因为我们使用 Google 域进行操作。无论如何,非常感谢您的帮助。
这是我在 Visual Studio 中的最新脚本尝试:
using System;
using Google.GData.Client;
using Google.GData.Spreadsheets;
namespace MySpreadsheetIntegration
{
class Program {
static void Main(string[] args)
{
string CLIENT_ID = "abunchofcharacters.apps.googleusercontent.com";
string CLIENT_SECRET = "secretnumber";
string REDIRECT_URI = "https://code.google.com/apis/console";
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.ClientId = CLIENT_ID;
parameters.ClientSecret = CLIENT_SECRET;
parameters.RedirectUri = REDIRECT_URI;
parameters.Scope = SCOPE;
string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
Console.WriteLine(https://code.google.com/apis/console);
Console.WriteLine("Please visit the URL above to authorize your OAuth "
+ "request token. Once that is complete, type in your access code to "
+ "continue..."));
parameters.AccessCode = Console.ReadLine();
OAuthUtil.GetAccessToken(parameters);
string accessToken = parameters.AccessToken;
Console.WriteLine("OAuth Access Token: " + accessToken);
GOAuth2RequestFactory requestFactory =
new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
service.RequestFactory = requestFactory;
var driveService = new DriveService(auth);
var file = new File();
file.Title = "VSI - VM Totals by Service TEST";
file.Description = string.Format("Created via {0} at {1}", ApplicationName, DateTime.Now.ToString());
file.MimeType = "application/vnd.google-apps.spreadsheet";
var request = driveService.Files.Insert(file);
var result = request.Fetch();
var spreadsheetLink = "https://docs.google.com/spreadsheets/d/GoogleDoc_ID";
Console.WriteLine("Created at " + spreadsheetLink);
End Class;
End Namespace;
}
}
}
对于仍在关注此问题的任何人,我找到了解决方案。我正在以完全错误的方式(或者至少是我可以理解的方式)来解决这个问题。我的问题的一个解决方案是创建一个新的 Google 脚本,该脚本每周只访问一次我的电子邮件(在我们收到报告后),并梳理除我正在寻找的数据之外的所有内容并将其发送到 Google电子表格。
这是脚本:
function SendtoSheet(){
var threads = GmailApp.search("from:THESENDER in:anywhere subject:THESUBJECTOFTHEEMAILWHICHNEVERCHANGES")[0];
var message = threads.getMessages().pop()
var bodytext = message.getBody();
counts = []
bodytext = bodytext.split('<br>');
for (var i=0 ; i < bodytext.length; i++){
line = bodytext[i].split(':');
if (line.length > 0){
if (!isNaN(line[1])){counts.push(line[1]);}}}
var now = new Date()
counts.unshift(Utilities.formatDate(now, 'EST', 'MM/dd/yyyy'))
var sheet = SpreadsheetApp.openById("GoogleDocID")
sheet = sheet.setActiveSheet(sheet.getSheetByName("Data by Week"))
sheet.appendRow(counts)
}
Counts 数组包含通过换行符和 :'s 分解来提取数字数据的魔法。完美运行。它不涉及弄清楚如何使用 Visual Studio 或 .net Google 库,或编辑 运行 PowerShell 脚本。干净又简单。
希望这对某人有所帮助。
我很难完成我认为应该是一项简单的任务。每周,我们的团队都会向 VMware vCenter 查询三部分输出:三个不同位置的 VM 计数。这是它的样子:
Name Value
---- -----
locationA 1433
locationB 278
locationC 23
信息通过电子邮件发送给我们的团队,以及一些喜欢看数据的高层。这一切都是通过 Powershell 脚本和 Windows 任务计划程序 运行 在服务器上自动完成的,没问题。
那个数据也放在一个Googlesheet中。我们只需在新行中附加日期,然后将数据复制并粘贴到现有的三个列中。它需要 30 秒,每周一次。考虑到将其复制到 Google sheet 所花费的时间很少,这看起来很愚蠢,但我真的想使用 Google Sheets API.[=14= 自动执行最后一个过程]
我似乎在 Google 访问和编辑 Google sheet 的脚本中一直在寻找和追寻在线的徒劳无功。我已经下载并安装了 Sheets API 库、Drive API 库、Google .net 库、设置了 Google 开发者站点,以及 运行通过 Google sheets API 文档和 OAuth 身份验证。我正在使用 Visual Studio 2013,因为我认为它最适合 Powershell 并调用 .net 命令。
我几乎没有 Powershell 之外的编码经验(如果你可以称之为编码)。我什至不知道如何拉 Google sheet,更不用说对它做任何事情了。到目前为止,我尝试过的任何方法都不起作用,而且每周手动复制此信息所花的时间很少,我已经花费了比可能值得的更多时间。我觉得如果我能解决这个问题,那将为将来进一步 Google 自动化打开大门,因为我们使用 Google 域进行操作。无论如何,非常感谢您的帮助。
这是我在 Visual Studio 中的最新脚本尝试:
using System;
using Google.GData.Client;
using Google.GData.Spreadsheets;
namespace MySpreadsheetIntegration
{
class Program {
static void Main(string[] args)
{
string CLIENT_ID = "abunchofcharacters.apps.googleusercontent.com";
string CLIENT_SECRET = "secretnumber";
string REDIRECT_URI = "https://code.google.com/apis/console";
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.ClientId = CLIENT_ID;
parameters.ClientSecret = CLIENT_SECRET;
parameters.RedirectUri = REDIRECT_URI;
parameters.Scope = SCOPE;
string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
Console.WriteLine(https://code.google.com/apis/console);
Console.WriteLine("Please visit the URL above to authorize your OAuth "
+ "request token. Once that is complete, type in your access code to "
+ "continue..."));
parameters.AccessCode = Console.ReadLine();
OAuthUtil.GetAccessToken(parameters);
string accessToken = parameters.AccessToken;
Console.WriteLine("OAuth Access Token: " + accessToken);
GOAuth2RequestFactory requestFactory =
new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
service.RequestFactory = requestFactory;
var driveService = new DriveService(auth);
var file = new File();
file.Title = "VSI - VM Totals by Service TEST";
file.Description = string.Format("Created via {0} at {1}", ApplicationName, DateTime.Now.ToString());
file.MimeType = "application/vnd.google-apps.spreadsheet";
var request = driveService.Files.Insert(file);
var result = request.Fetch();
var spreadsheetLink = "https://docs.google.com/spreadsheets/d/GoogleDoc_ID";
Console.WriteLine("Created at " + spreadsheetLink);
End Class;
End Namespace;
}
}
}
对于仍在关注此问题的任何人,我找到了解决方案。我正在以完全错误的方式(或者至少是我可以理解的方式)来解决这个问题。我的问题的一个解决方案是创建一个新的 Google 脚本,该脚本每周只访问一次我的电子邮件(在我们收到报告后),并梳理除我正在寻找的数据之外的所有内容并将其发送到 Google电子表格。
这是脚本:
function SendtoSheet(){
var threads = GmailApp.search("from:THESENDER in:anywhere subject:THESUBJECTOFTHEEMAILWHICHNEVERCHANGES")[0];
var message = threads.getMessages().pop()
var bodytext = message.getBody();
counts = []
bodytext = bodytext.split('<br>');
for (var i=0 ; i < bodytext.length; i++){
line = bodytext[i].split(':');
if (line.length > 0){
if (!isNaN(line[1])){counts.push(line[1]);}}}
var now = new Date()
counts.unshift(Utilities.formatDate(now, 'EST', 'MM/dd/yyyy'))
var sheet = SpreadsheetApp.openById("GoogleDocID")
sheet = sheet.setActiveSheet(sheet.getSheetByName("Data by Week"))
sheet.appendRow(counts)
}
Counts 数组包含通过换行符和 :'s 分解来提取数字数据的魔法。完美运行。它不涉及弄清楚如何使用 Visual Studio 或 .net Google 库,或编辑 运行 PowerShell 脚本。干净又简单。
希望这对某人有所帮助。