The CreatorCon Call for Content is officially open! Get started here.

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

Has the file been successfully copied to the MID Server? What's the response back on the ECC queue?


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.


Hi Tommy,



Can you share the procedure and script to create a file in any mid server location/ any other server location.



Thanks in advance.



Regards,


Mihir


The script I ended up with is very simple and basic. Could use some error checking etc but now it works.



Write-Output $xmldoc
$guid = [Guid]::NewGuid().ToString()
$file = $guid+".xml"
$path = "\\$computer\SHARE\"+$file
$xmldoc | Out-File $path



Then in workflow editor create a new custom powershell activity that calls this script and passes the computer to send the file to, you could change the script to recieve the full path and then pass the contents of the file.