2

I'm developing a project in Google Apps Script and Powershell to assist in creating customized Powershell configuration scripts for mass-configuration of Windows machines.

When testing the code in the Apps Script web IDE, the string is split properly. However, when calling the script through Powershell's Invoke-Webrequest cmdlet, the result I'm getting from Google contains an error saying that .split is not a function.

Powershell code:

$queryString = "files=$query" # query is a string of google file IDs joined by commas
$server = "https://script.google.com/macros/s/google_script_id/exec?"
$request = $server + $queryString
try {
    $Response = Invoke-WebRequest $request -UseBasicParsing
} catch {
    Write-Host "Failed to complete the web request.  Trying again in 5 seconds..."

Google Apps Script code:

let content = "";
let filestr = e.parameters.files; //e.parameters.files is the string of file IDs joined by commas, passed by Powershell
console.log(typeof filestr); //logs type as string, see below
let files = filestr.split(",");
for (let i = 0; i < files.length; i++) {
  try {
    let script = DriveApp.getFileById(files[i])
    content += script.getAs("text/plain");
    content += "::"
  } catch (ex) {
    message = `Failed::${ex.name}:${ex.message}`;
    return message;
  }
}
message = `SUCCESS::${content}`;
return message;

Picture showing that filestr is a string with console.log(typeof filestr):
Picture showing that filestr is a string with console.log(typeof filestr)

HTML error I'm getting in my PowerShell script (filestr.split is not a function):

"<!DOCTYPE html><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style type="text/css" nonce="1fq8pQfEFhMLUJvDdsXUDw">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style></head><body style="margin:20px"><div><img alt="Google Apps Script" src="//ssl.gstatic.com/docs/script/images/logo.png"></div><div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">TypeError: filestr.split is not a function (line 117, file &quot;Code&quot;)</div></body></html>"

Like I said, the script processes normally in the Apps Script IDE, but for some reason I get an error when PowerShell calls it via a web request. Hopefully this post is pointless and it's an easy solve, but lots of the questions I've seen so far have involved people calling .split on object properties or other things that they think are strings, but actually aren't. I'm at least fairly confident that I'm calling this method on an actual string though, and the fact that it works sometimes and not others is puzzling.

After typing this up, I went to have a last look around the web, and found a Stack Overflow post that mentioned that when an object method is called on a primitive value, a temporary object is created to house the value so that the object method can be run. Afterwards the object is discarded. Is this process somehow to blame for the behavior I experienced, or is there some other reason why creating an object first worked?

SO answer that gave me the inspiration to create a wrapper object: How is a Javascript string not an object?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 2
    Thanks for sharing a solution, but I encourage you to post it in the form of an _answer_, which makes for a clearer separation between the problem and the solution, and also allows you to accept the answer (after 48 hours) to signal to future readers that an effective solution was found. – mklement0 Sep 30 '21 at 21:59
  • 2
    I think that in your script, `e.parameters.files` is an array. [Ref](https://developers.google.com/apps-script/guides/web) By this, such an error occurs. I think that this is the reason for your issue. In this case, you can also use `e.parameter.files`. Because `e.parameter.files` returns the string value. – Tanaike Oct 01 '21 at 04:21
  • Kindly move solution to the answer box below. – TheMaster Oct 01 '21 at 11:16
  • @Tanaike I think you're probably right. Thanks for that link, I hadn't seen that page yet and was working off of an understanding that I gleaned from studying my predecessor's code. However, this means I've been treating array values as strings up to this point, so it's interesting that I didn't run into problems sooner. I wonder if this is an instance where Google/JS are trying to be nice by glossing over a catch-able error for their users. – justin.k.pierson Oct 01 '21 at 14:01

1 Answers1

0

I changed the line let filestr = e.parameters.files; to let filestr = new String(e.parameters.files);, and am now able to call .split() on the string without getting an error in PowerShell.

Nimantha
  • 6,405
  • 6
  • 28
  • 69