Create file on remote server with PowerShell

tommyjensen
Giga Expert

I am trying to modify the MID Server Script file: LaunchProc.psm1 to create a file on the remote server instead of executing a command.

But nothing appears to happen. I pass the text to write in the file via a parameter $xmldoc and the destination computer in $computer. Run from a workflow and parameters set in inputs. No error messages in ecc queue.

I have verified that I can access the remote server with powershell and execute commands.

My script is as follows:

<######################

#

######################>

function launchProcess {

      param([string]$computer, [System.Management.Automation.PSCredential]$cred, [string]$xmldoc, [int]$secondsToWait = 10)

      $signature = @"

      [DllImport("advapi32.dll", SetLastError = true)]

      public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

"@;

      $LogOnUser = Add-Type -memberDefinition $signature -name "Win32LogOnUser" -namespace Win32Functions -passThru;

      [IntPtr]$userToken = [Security.Principal.WindowsIdentity]::GetCurrent().Token;

      $context = $null

      if ($cred) {

              if ($LogOnUser::LogOnUser($cred.GetNetworkCredential().UserName, $cred.GetNetworkCredential().Domain, $cred.GetNetworkCredential().Password, 9, 0, [ref]$userToken)) {

                    $Identity = new-object security.Principal.WindowsIdentity $userToken

          $context = $Identity.Impersonate();

              }

              else {

                      $err = "The impersonation of user $user failed."

                      [Console]::Error.WriteLine($err)

                      return;

              }

      }

      Write-Output $xmldoc

      $guid = [Guid]::NewGuid().ToString()

      $file = $guid+".xml"

      $path = "\\$computer\test\"+$file

      If(-Not (Test-Path $path)){

              New-Item $path

              If(Test-Path $path){

                      $xmldoc | Out-File $path

              } Else {

                      $err = "Failed to create file"

                      [Console]::Error.WriteLine($err);

                      return;

              }

      } Else {

              $err = "Failed to create a unique file name" #Should not happen.

              [Console]::Error.WriteLine($err);

              return;

      }

      if ($context) {

  $context.Undo();

  $context.Dispose();

      }

     

      return;

}

1 ACCEPTED SOLUTION

Everything in workflow, ecc queue looked good and did not indicate any errors.The script file was also transferred correctly to the midserver.




But I found out why the script file was never executed because that was the problem. I had used an oob script file as an example assuming that it would run straigth away. However the file I used had the filetype .psm1 but the powershell on the midserver refused to execute that. Once I renamed the extension to .ps1 everything worked fine.


View solution in original post

8 REPLIES 8

PeterWiles
Kilo Sage

If you run the script directly on the MID server directly, does it work?


It will help rule out a ServiceNow/MID issue.



Pete


I have verified that if I switch the script type in the activity designer from file to custum command and create a file with "dir >\\server\share\file.txt" it works. So it is definately the script there is something wrong in. I now found at least one error but that has not solved my issue. The cmdlet New-Item needs the parameter "-type file"


tommyjensen
Giga Expert

This is really weird. I have now reduced my script to bare minimum with just following command



New-Item c:\testfile.txt -type file -value "testing"



In my workflow I have defined two new custum activities, one which runs a powershell script from Mid server script files. This script only contains above line. This does NOT wotk.



and   another custum activity which instead of running a script runs a command and I specify above line too and this WORKS!



So my question is why does one work while the other do not work? In both cases I set the script/command to run locally on the MIDserver that executes it so I am not even remoting to a different server.


I reproduced this on my developer instance which run Geneva, the first attempts was run on Fuji. So either there is an error running powershell scripts or I am doing something wrong which is very possible.