Dokumentation zum Scripting in Automate:
Für folgende Automate-Scriptfunktionen wird auf dem Agent-Computer ein installiertes PowerShell 2.0 oder neuer benötigt:
PowerShell Bypass umgeht die Powershell Execution Policy unter Verwendung des Arguments "/c powershell -ExecutionPolicy ByPass".
PowerShell-Scripte und -Befehle geben nur den Standard-Outputstream zurück. Außerdem schreibt Write-Host nicht in den Standard-Outputstream. Hierzu verwenden Sie bitte Write-Output.
Praxistipps
Der beste Weg, aus einem Automate-Script heraus ein PowerShell-Script zu starten, ist der folgende:
- Starten Sie das Script mit der "Shell"-Funktion und dem Parameter
%windir%\system32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy ByPass -File "%windir%\temp\PowerShellFile.PS1"
- Wenn Sie vom PowerShell-Script einen Rückgabewert erwarten, schreiben Sie diesen in eine Datei. Diese Datei können Sie dann mit der Variable Set-Funktion auswerten. - Beenden Sie das PowerShell-Script mit Abschlussfunktionen wie den folgenden, um bei jedem denkbaren Ergebnis des PowerShell-Scripts die richtigen Informationen zu erhalten:
Function End-Script {
<#
.SYNOPSIS
A function to wrap up the end of the script.
.DESCRIPTION
Function has multiple tasks:
1) Out-files the contents of $Error.
2) Out-files $Result
3) Terminates the Script.
.PARAMETER $Result
The result string to out-file.
.EXAMPLE
PS C:\> End-Script -Result $Result
#>
param
(
[parameter(Mandatory = $true)]
[String]$Result
)
Out-File -InputObject $Error -FilePath $ErrorPath
Out-File -InputObject $Result -Filepath $ResultsPath
Write-Log ("********************************")
Write-Log ("* $ScriptName Script Ends *")
Write-Log ("********************************")
exit;
}
Function Write-Log {
<#
.SYNOPSIS
A function to write ouput messages to a logfile.
.DESCRIPTION
This function is designed to send timestamped messages to a logfile of your choosing.
Use it to replace something like write-host for a more long term log.
.PARAMETER StrMessage
The message being written to the log file.
.EXAMPLE
PS C:\> Write-Log -StrMessage 'This is the message being written out to the log.'
#>
Param
(
[Parameter(Mandatory = $True, Position = 0)]
[String]$Message
)
add-content -path $LogFilePath -value ($Date + "`t:`t" + $Message)
}