AnsweredAssumed Answered

Is it possible to directly launch PowerShell scripts in ITCM?

Question asked by u123246 on Nov 20, 2014
Latest reply on Nov 20, 2014 by u123246

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

Outcomes