0

I have now tried about a hundred ways of automating the execution of powershell scripts from C# and Dynamics NAV 2013.

Running the script works 100% from either Powershell ISE or executing it with the following command in commandline:

powershell.exe -version 3.0 -command ". 'C:\temp\script.ps1'"

So the last option I tried now was to put this all in a .bat file and execute the .bat file using System.Diagnostics.Process (also, executing the .bat file manually works 100%)

Setup.GET;
ProcessInfo := ProcessInfo.ProcessStartInfo(FileName); //The .bat File
ProcessInfo.UseShellExecute := FALSE;
ProcessInfo.RedirectStandardError := TRUE;

//<< Credentials
ProcessInfo.Domain := Setup.Domain;
ProcessInfo.UserName := Setup.Username;

SecurePwd := SecurePwd.SecureString();

FOR i := 1 TO STRLEN(Setup.Password) DO
  SecurePwd.AppendChar(Setup.Password[i]);

ProcessInfo.Password := SecurePwd;
//>> Credentials

Process := Process.Start(ProcessInfo);
StreamReader := Process.StandardError();

MESSAGE(StreamReader.ReadToEnd());

Without setting the credentials I get as output from the message:

The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.

And it just about complains about something on every line in the script.

Import-Module : The specified module 'D:\psmodules\Multitenancy\NAVMultitenancySamples.psm1' was not loaded because no valid module file was found in any module directory.

etc. etc.

With the credentials filled in, message is blank but it didn't do anything that it was supposed to.

Now, from the message I can see that it is trying to access the registry entries for Powershell version 1 instead of 3... could that be part of the problem? Version 3 is installed:

PS C:\> $Host.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1      
event.er
  • 65
  • 1
  • 8

1 Answers1

0

Have you checked the UAC side of things? It may require UAC privileges, as opposed to just Admin credentials;

This other StackOverflow question details how to elevate a System.Diagnostics.Process command;

startInfo.Verb = "runas";
Community
  • 1
  • 1
Jake Edwards
  • 1,190
  • 11
  • 24
  • UAC is turned off. Added as suggested: `ProcessInfo.Verb := 'runas';` Switched `ProcessInfo.UseShellExecute := TRUE;` (removed the credentials) returns: `A call to System.Diagnostics.Process.Start failed with this message: Access is denied` Put credentials back and switch UseShellExecute off, returned nothing from the StandardError, only an ExitCode = -1073741502 – event.er Aug 27 '14 at 07:27
  • Exit Codes apparently don't work very well from Powershell, only verifying by checking the output manually -- does StandardOutput have any information? – Jake Edwards Aug 27 '14 at 07:35
  • StandardOutput is also blank. – event.er Aug 27 '14 at 08:08
  • By adding my own exit codes, I narrowed it down that powershell "hangs" on an Import-Module command. As mentioned previously running it manually works. So I thought it might have to do with the folder permissions where the module its trying to import resides, granted full access to the executing user - still nothing. – event.er Aug 27 '14 at 15:31
  • I assume you're running with the credentials the same as your current user? The user has permissions to those Import-Module paths? Any extra info with the -Verbose command ? – Jake Edwards Aug 28 '14 at 05:19
  • That was indeed part of the issue. Somehow, because my NAV service was running on NETWORK SERVICE, even though I was passing admin credentials to Process.Start it tried to authenticate accessing/importing the scripts with NETWORK SERVICE. Changed my service to run with network admin credentials and it was working :) Thanks for the assistance!! – event.er Aug 28 '14 at 10:42