CA Client Automation

  • 1.  Is it possible to directly launch PowerShell scripts in ITCM?

    Posted Nov 20, 2014 04:45 AM

    I have tried to find documentation on that point, but couldn't find anything helpful there.

     

    All our main logic resides or is going to reside in PowerShell scripts, but to the best of my knowledge I need to call those scripts using a DSM wrapper script if it is supposed to run correctly.

    Does anyone know a better way to do that (especially considering that we need to be able to launch those scripts in 32-bit and 64-bit environments)?

     

    I'd prefer just launching said PowerShell scripts with the ability to return error codes to ITCM and use the $rf input.

     

    Here a simple script that we use to wrap PowerShell:

     

    dim SDOLog as String

    dim HSDOLog as Integer 'FileHandle fuer Protokolldaten in DSM

    dim TempScriptPath as String

    dim ScriptSourceDir as String

    dim ScriptFileName as String

    dim ScriptFilePath as String

    dim RebootFileName as String

    dim ScriptParams as String

    dim ret as Integer

    if Argc()

    print "Es muss die Protokolldatei ($rf) übergeben werden!"

    SetStatus(NC_ERROR)

    goto end

    end if

    ScriptSourceDir = GetDirectory(0)

    ScriptFileName = "WUInstallLauncher.ps1"

    RebootFileName = "WUInstallReboot.txt"

    SDOLog= Argv(1) '$rf im Skriptaufruf

    if ExistFile(EnvGetString("TEMP") + "\" + RebootFileName) then

    HSDOLog = OpenFile(SDOLog, O_APPEND, O_TEXT)

    WriteFile(HSDOLog, "Erneuter Skriptaufruf nach Neustart!")

    else

    HSDOLog = OpenFile(SDOLog, O_WRITE, O_TEXT)

    end if

    if HSDOlog < 0 then

    print "Die DSM Protokolldatei konnte nicht geoeffnet werden!"

    SetStatus(502)

    goto end

    end if

    WriteFile(HSDOLog, "Uebertrage Skript-Datei auf lokalen Pfad...")

    TempScriptPath = EnvGetString("TEMP") + "\"

    ScriptFilePath = TempScriptPath + ScriptFileName

    if not(ExistFile(ScriptSourceDir + "\" + ScriptFileName)) then

    WriteFile(HSDOLog, "FEHLER: Skript-Datei konnte in Quell-Ordner nicht gefunden werden!")

    goto enderror

    end if

    if not(CopyFile(ScriptSourceDir + "\" + ScriptFileName, ScriptFilePath, true)) then

    WriteFile(HSDOLog, "FEHLER: Konnte Skript-Dateien nicht übertragen!")

    goto enderror

    end if

    WriteFile(HSDOLog, "Rufe Powershell-Skript auf...")

    ScriptParams = "-RunsAsSDJob 1 -PathTorf """ + SDOLog + """

    CloseFile(HSDOLog)

     

    ret=Execute("Powershell.exe -ExecutionPolicy Unrestricted -command ""& '" + ScriptFilePath + "' " + ScriptParams, true, 0)

     

    HSDOLog = OpenFile(SDOLog, O_APPEND, O_TEXT)

     

    if not(DelFile(TempScriptPath + ScriptFileName, 1)) then

    WriteFile(HSDOLog, "WARNUNG: Konnte Kopie des Skriptes nicht loeschen!")

    end if

    if not(CheckRet(ret)) then

    goto enderror

    elseif ret > 0 then

    WriteFile(HSDOlog,"Fehler beim Aufruf des Powershell Skriptes!")

    SetStatus(ret)

    goto end

    end if

    goto endok

    enderror:

    WriteFile(HSDOlog,"Fehler beim Aufruf des Powershell Skriptes!")

    SetStatus(NC_ERROR)

    goto end

     

    endok:

    WriteFile(HSDOlog,"Aufruf des Powershell Skriptes erfolgreich!")

    SetStatus(NC_OK)

    goto end

     

    end:

    CloseFile(HSDOLog)

     

    function CheckRet(rc as Integer) as Boolean

    CheckRet= true

    if ret=-1 then

    WriteFile(HSDOlog,"Das System verfügt nicht über genügend Speicherplatz oder Ressourcen!")

    CheckRet= false

    elseif ret=-2 then

    WriteFile(HSDOlog,"Die angegebene Datei wurde nicht gefunden!")

    CheckRet= false

    elseif ret=-3 then

    WriteFile(HSDOlog,"Der angegebene Pfad wurde nicht gefunden!")

    CheckRet= false

    elseif ret=-4 then

    WriteFile(HSDOlog,"Die .exe-Datei ist ungültig!")

    CheckRet= false

    elseif ret=-5 then

    WriteFile(HSDOlog,"Unbekannter Fehler!")

    CheckRet= false

    endif

    end function



  • 2.  Re: Is it possible to directly launch PowerShell scripts in ITCM?

    Broadcom Employee
    Posted Nov 20, 2014 04:52 AM
      |   view attached

    You can run power shell scripts directly from the Software Delivery Item procedure. There are two configuration setting available that are relevant to this:

     

     



  • 3.  Re: Is it possible to directly launch PowerShell scripts in ITCM?

    Posted Nov 20, 2014 05:29 AM

    And what file type woul I have to select to provide a powershell script in the respective procedure?



  • 4.  Re: Is it possible to directly launch PowerShell scripts in ITCM?

    Broadcom Employee
    Posted Nov 20, 2014 06:43 AM
      |   view attached

    On the item procedure you define the ps1 file as an executable. E.G:

     

     

     

    This results in the following command being run by the Agent:

     

    “powershell.exe -File  refresh_PC.ps1”



  • 5.  Re: Is it possible to directly launch PowerShell scripts in ITCM?

    Posted Nov 20, 2014 06:56 AM

    Great! Thank you so much for your insight on that so far.

    And now the (hopefully) final question concerning that topic:

    How do I provide parameters to the Powershell script so that I can provide for instance $rf and $up to the executable.

     

    Is it the usual way? Because I had to use

    Powershell.exe -ExecutionPolicy Unrestricted -command "& 'c:\test.ps1' -Switch 1 -Path c:\asdf"

    for that to work



  • 6.  Re: Is it possible to directly launch PowerShell scripts in ITCM?

    Broadcom Employee
    Posted Nov 20, 2014 09:10 AM
      |   view attached

    This is an example:

     

     

     

     

    Results in the SD Agent running this command:

     

     

    “powershell.exe -File  refresh_PC.ps1 arg1 arg2 C:\PROGRA~2\CA\DSM\Agent\units\00000001\usd\sdjexec\52705FAE-17F5-48BC-8C87-192A0D6805FA.res --ExecutionPolicy Unrestricted”



  • 7.  Re: Is it possible to directly launch PowerShell scripts in ITCM?

    Posted Nov 20, 2014 10:51 AM

    So to wrap your answers up on using PowerShell with ITCM:

     

    1. Check configuration policy under "DSM\Software Delivery\Agent" for "File Association: Powershell..."

    2. When creating a procedure choose "Executable" and provide name of script file.

    3. Add parameters as usual.

    4. Deploy package and be glad!

     

    Thanks a bunch, Steve.

    I've looked through the documentation for Powershell related info, but just couldn't find anything pointing me to that. This will save me a lot of extra hassle in building parameter configurations.