locked
Triggering one PS script from another with arguments and credentials RRS feed

  • Question

  • Hi,

    I have a requirement of editing content of XML files on the network share leveraging certain credentials. Have the credentials but would like to automate this. Did not want to go down the route of mapping PS drive since my script would be executing editing of XML files asynchronously. 

    So though of using a second ps1 script to perform the XML editing and execute that in the main script with required credentials.

    First, tried Invoke-Expression method but that does not allow leveraging credentials.

    Second, tried Start-Process but did not work.

    Details of start-process approach:

    Assuming 1.PS1 is my main script and 2.ps1 is other script I leverage to make the XML file content editing, the below line is the part of 1.ps1 where I try to pass the Path of 2.ps1,XML file Path, OLD string name, NEW string name and credentials under which the second script should be executed:

    Start-Process  -wait -nonewwindow powershell -Credentiantial $credential -Argumentlist {-Path [Path of 2.PS1] [XML File Path] [OLD String] [NEW String]}

    Content of 2.ps1:

    $XMLFilePath = $args[0]

    $OLDString = $args[1]

    $NEWString = $args[2]

    (Get-Content -Path $XMLFilePath).Replace($OLDString,$NEWString) | New-Content -Path $XMLFilePath

    Kindly Advice!


    • Edited by DS2885 Friday, May 10, 2019 4:57 PM
    Friday, May 10, 2019 4:51 PM

Answers

  • What does "did not work" mean?

    Do it like this:

    #contents of test.ps1
    (Get-Content $args[0] | 
        ForEach{
            $_ -replace $args[1], $args[2]
        }) |
        Out-File -Path $args[0]
    # end content
    
    
    # command
    Start-Process powershell -Credentiantial $credential -Argumentlist '-file test.ps1', '<filepath>', '<OLD String>, <NEW String>'

    "old string" cannot have any special characters or it will have to be escaped.


    \_(ツ)_/


    • Edited by jrv Friday, May 10, 2019 7:31 PM
    • Marked as answer by DS2885 Tuesday, May 14, 2019 1:09 PM
    Friday, May 10, 2019 7:30 PM

All replies

  • Mapping a drive does not make anything asynchronous.  It will give you the direct access you need.

    You cannot use "nonewwindows" with alternate credentials.  Just use Start-Process to supply credentials to a script file or a script block and allow the new window to be displayed.


    \_(ツ)_/

    Friday, May 10, 2019 5:17 PM
  • Removing nonewwindow from commandline did not work.
    Friday, May 10, 2019 6:59 PM
  • What does "did not work" mean?

    Do it like this:

    #contents of test.ps1
    (Get-Content $args[0] | 
        ForEach{
            $_ -replace $args[1], $args[2]
        }) |
        Out-File -Path $args[0]
    # end content
    
    
    # command
    Start-Process powershell -Credentiantial $credential -Argumentlist '-file test.ps1', '<filepath>', '<OLD String>, <NEW String>'

    "old string" cannot have any special characters or it will have to be escaped.


    \_(ツ)_/


    • Edited by jrv Friday, May 10, 2019 7:31 PM
    • Marked as answer by DS2885 Tuesday, May 14, 2019 1:09 PM
    Friday, May 10, 2019 7:30 PM
  • Thank You Jrv,

    The #command above worked for me with

    test.ps1:

    (Get-Content $args[0]).Replace($args[1],$args[2]) | Set-Content $args[0]


    • Edited by DS2885 Tuesday, May 14, 2019 1:10 PM
    Tuesday, May 14, 2019 1:09 PM