-1

I'm programatically launching a Google Cloud Compute Instance running Windows Server 2016 with a start up script.

The executable in the start up script requires to be launched as a specific user, so I'm trying to launch it with psexec to simulate said user:

C:/psexec.exe \\\\WIN-SERVER-2016 -u WIN-SERVER-2016\\customuser -p custompassword -accepteula -w "c:/app" cmd /c node index.js

c:/app/index.js contains a simple hello world, which should write to a file.

If I log in as any user and launch this exact command from cmd, the file is written. Launching from the startup script (supplied as windows-startup-script-cmd in the Google Cloud Compute Engine Instance) results in no file written.

What could be the solution? Is there a more efficient way to execute a start-up script as a specific user?

Dxx
  • 934
  • 2
  • 11
  • 41

1 Answers1

1

Looking at the concern , I would not recommend you to use PSEXEC .

NOrmally, we use PSExec in order to invoke a GUI in the remote system which PS doesn't support by native.

In your case, I would suggest you to run using the Invoke-Command

Something like this:

$username = 'WIN-SERVER-2016\customuser'
$password = "custompassword"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr

$Script_block = {cmd /c  node index.js}

Invoke-Command -ComputerName  WIN-SERVER-2016 -Credential $cred -ScriptBlock $Script_block 

This should also take it from the Metadata key if you are using windows-startup-script-cmd

Note: I have not considered the accepteula -w "c:/app" part. Please incorporate the placeholders accordingly.

Hope it helps...!!!

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45