IT Process Automation

Expand all | Collapse all

PowerShell Process with Variables

Jump to Best Answer
  • 1.  PowerShell Process with Variables

    Posted 02-23-2019 07:04 AM

    I want to create an powershell script to be executed by PAM script operator.

    I want to pass variables to script, but I don´t know how.

    Someone has examples of powershell scripts with variables?



  • 2.  Re: PowerShell Process with Variables

    Posted 03-04-2019 12:45 PM

    I prefer to use the "Run program" operator to start powershell.exe then pass variable to it :

     

     

    First one is always the path to the ps1 script, next are key-value [-variableName,variablevalue]



  • 3.  Re: PowerShell Process with Variables
    Best Answer

    Posted 03-05-2019 11:03 AM

    Greetings!

     

    Currently the best way I have found to bring data into/out of PowerShell in an operator:

     

    • Add a new "Run Script" operator to your process
    • For script extension, set to .ps1
    • For inline script, the first block needs to be your parameter block with the variable names you want to import
    • Down in the Parameters section of the operator, put in your variables in the SAME ORDER as your parameter block, surrounded by a double-single-double quote
      • Ex: "'" + Process.variable + "'"
    • Make sure that "Post output to dataset variable" checkbox is checked
      • To read your script output, under Post-execution code use the variable "Process[OpName].scriptOutput"

     

     

    Example of the Parameters section in the operator:

    Parameters

     

     

    Hopefully that helps!



  • 4.  Re: PowerShell Process with Variables

     
    Posted 03-20-2019 04:49 AM

    Consider a process having a run script operator:-

     

    1. Create variable and assign a value to it. eg. Process.serviceName, Process.computerName
    2. click on run script operator.
    3. Go to parameters, click on Add parameter button. Type in the name of variable you created.
    4. Select Script extention as .ps1
    5. In the inline script, put the PowerShell script, eg.,

    #---------------------------------------------
    $Name= $args[0]
    $CompName= $args[1]
    Get-Service $Name -ComputerName $CompName
    #---------------------------------------------

    -- You can add multiple ITPAM variables in parameters and call them in the inline script using $args[] with their correct index values.
    args[0] will carry the value of 1st parameter that is Process.serviceName to the inline script
    args[1] will carry the value of 2nd parameter that is Process.computerName to the inline script.

     

    ###################################################################

    The process looks like this:

    Create variables, here I have created two process level variables:

     

    Adding Parameters:

    Calling parameters in the inline script:



  • 5.  Re: PowerShell Process with Variables

    Posted 03-22-2019 12:10 PM

    Thanks for the info. Hadn't thought about doing it this way, but from a readability standpoint this is much cleaner.