title: Triggering a VBScript from a C# dotnet core API
body: <p>I have an ASP.NET Core API running in IIS on an app server, which needs to be able to trigger a series of VBScripts which open, populate and print a number of documents. The Application Pool being used by the API has its identity set to a domain user with the permissions required to successfully run the scripts, <code>Fooser</code>. If I log on as that user and run the script <code>Bar.vbs</code> it executes successfully.</p>
  
  <p>When I try to run <code>Bar.vbs</code> from the API using a <code>Process</code>, though, it fails on trying to open the template document. I added some logging in <code>Bar.vbs</code> of the <code>WScript.Network</code> <code>Username</code> property and, sure enough, it is <code>Fooser</code> (or whatever user I set it to be).</p>
  
  <p><code>C:\Company\CompanyDocuments\Scripts\Bar.vbs</code> looks like this:</p>
  
  <pre><code>' -- Script Start
  
  Set logShell = CreateObject("WScript.Shell")
  Set objWord = OpenWord("C:\Company\CompanyDocuments\test.doc")
  Set EVENT_INFORMATION = 4
  
  ' -- Script End
  
  Function OpenWord(wrdDoc2Open)
  Dim Word
  Const wdDialogFilePrintSetup = 97
  Set Word = CreateObject("Word.Application")
  logShell.LogEvent EVENT_INFORMATION, "Test 1: opened word"
  Word.Visible= True
  ' Set Options in Word to try to speed up document processing.
  With Word.Options
          .CheckSpellingAsYouType = False
          .CheckGrammarAsYouType = False
          .SuggestSpellingCorrections = False
          .SuggestFromMainDictionaryOnly = False
          .CheckGrammarWithSpelling = False
          .ShowReadabilityStatistics = False
  End With
  logShell.LogEvent EVENT_INFORMATION, "Test 2: opening document " &amp; wrdDoc2Open
  logShell.LogEvent EVENT_INFORMATION, "Test 2.1: User" &amp; CreateObject("WScript.Network").UserName
  
  Set g_objDoc = Word.documents.Open(wrdDoc2Open,,True)
  
  If Err Then 
      logShell.LogEvent EVENT_INFORMATION, "Test Error."
  End If
  
  logShell.LogEvent EVENT_INFORMATION, "Test 3: opened document"
  Set OpenWord = Word
  End Function
  </code></pre>
  
  <p>And the process in being run by the API is:</p>
  
  <pre><code>var scriptProcess = new Process
  {
      StartInfo =
      {
          FileName = @"cscript",
          WorkingDirectory = scriptsPath.ToString(),
          WindowStyle = ProcessWindowStyle.Normal,
          Arguments = $"//B //Nologo {fileName} {(arguments.Count != 0 ? string.Join(' ', arguments) : string.Empty)}"
      }
  };
  scriptProcess.Start();
  scriptProcess.WaitForExit();
  scriptProcess.Close();
  </code></pre>
  
  <p>The process is successfully starting the script, but the script is failing without reaching either the <code>"Test Error."</code> or the <code>"Test 3: opened document"</code> statements.</p>
  
  <p>This is integrating with a legacy system so it's simply too much work to replace the vbs.</p>