delanguageRegister | Login
fingerprint

Login

call

Contact

help

Help

PowerShell Template

Optimize your addy workflows with this easily customizable template.

Use your existing PowerShell script and customize it to be used in admins buddy.

IT workflow automation

admins buddy offers a powerful automation solution that allows you to automate time-consuming workflows and thus increase your efficiency. To help you get started, we've created a PowerShell script template that you can easily customize to suit your needs and workflows.

The template includes a header and a footer designed specifically for admins buddy to make sure your script works smoothly. The placeholders in the header and footer are designed to take the parameters from admins buddy and format the output correctly.

To use the template, all you need to do is replace your existing code in the body of the script with your own code and replace the parameter placeholders in the header with the appropriate parameters. The template is flexible enough to cover a wide range of automation scenarios.

Use this template to streamline your IT workflows and save time and effort. We hope it will help you to successfully implement your automation projects.

Template

    
    [CmdletBinding()]
Param(
    [Parameter(Mandatory=$false)]
    [string]$installPath="not-set",
    [Parameter(Mandatory=$false)]
    [string]$jobId="not-set",
    [Parameter(Mandatory=$false)]
    [string]$action="not-set"
)

$debugScript = 0;
if ($debugScript -eq 1) {
    write-host "debugging mode on" -f yellow
    $installPath = "C:\addy\" #Debugging
    $jobId = "jobXXXXXXX" #only for debugging
}
$ErrorActionPreference = 'Continue'

# Only use TLSv1.2 and TLSv1.3
$AllProtocols = [System.Net.SecurityProtocolType]'Tls12,Tls13'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols

$errorCount = 0 # counting errors. If this variable is greater than 0, the script should not run
# loading functions
write-host "$(get-date -f  "dd.MM.yyyy HH:mm:ss") include functions"
if (test-path "$($installPath)scripts\functions.global.ps1") {
    write-host "Functions-File exists"
    import-module "$($installPath)scripts\functions.global.ps1" -force
} else {
    write-host "Functions file do not exist. Increasing ErrorCounter." 
    $errorCount++
}

set-location $installPath # setting the location
$machineId = Get-MachineId # generate a unique machineID
write-addylog "All parameter initialized" # write log

# Handling with parameters
if ($debugScript -eq 0) {
    # Load parameter information about this job
    $jsonBody = @{ localCurrentTime = $(get-date -f  "dd.MM.yyyy HH:mm:ss")}
    $body = (ConvertTo-Json -Depth 4 $jsonBody) 
    $resultInitializeInvoke = Invoke-RestMethod -Uri "$addyhostaddress/api/v1/heartbeat-consumer?action=checkforjobs&jobId=$jobId" -Method POST -Body $body -ContentType 'application/json; charset=UTF-8' -Headers @{"Publickey"="$publickey";"Privatekey"="$privatekey";"Machineid" = "$machineId"} # -Headers @{'Authorization'='Basic YWRtaW46YWRtaW4'}
    # take a look in the answer from the addy with $resultInitializeInvoke
    # Example Jobname: $resultInitializeInvoke.jobDataArray.businessAutomationJobsPendingJobname 
    
    # Example to get input parameter
    # $addyPayloadResourceInput = $resultInitializeInvoke.jobDataArray.businessAutomationJobsPendingPayload.payload

    # Example to get a specific input parameter
    # [string]$aStringParameter = $addyPayloadResourceInput.aStringParameter # "aStringParameter" is definied in the Input Parameter. This are now a parameter for this script 
    # $aStringParameter = $aStringParameter.trim()
    
    # Example to get resource parameter
    # [string]$ParamAnyString = $resultInitializeInvoke.scriptParameter.anyString #"anyString" is definied in the Resource Parameter. This are now a parameter for this script
    # $ParamAnyString = $ParamAnyString.trim()

    # cast scriptParameter int
    # [int]$ParamaNumber = "$($resultInitializeInvoke.scriptParameter.aNumber)" #"aNumber" is definied in the Resource Parameter. This are now a parameter for this script
}
###################### End header - Start main ############################

# Start: Introducing commands
write-addylog "Start the Script." #-Level "INFO","ERROR", "WARN"
$startOfScript = get-date

write-addylog "Initializing variables"
$errorCount = 0
$error.clear()
# End: Introducing commands


# Start: set a response
$jsonMessageBody = @{  
    message = "working"
    timestamp=$(get-date -f  "dd.MM.yyyy HH:mm:ss")
}
write-addylog "Set a job response. Message to `"working`""
set-JobResponse -jobId $jobId -jsonMessageBody $jsonMessageBody -publickey $publickey -privatekey $privatekey -machineId $machineId
# End: set a response


# Start: load resource parameter
write-addylog "Initializing resource parameter"
$addyPayloadResourceParameter = $resultInitializeInvoke.scriptParameter

[string]$temp0resourceparameter = $addyPayloadResourceParameter.temp0resourceparameter #"temp0resourceparameter" is definied in the Resource Parameter. This are now a parameter for this script
$temp0resourceparameter = $temp0resourceparameter.trim()
write-addylog "[resource parameter] `"temp0resourceparameter`" is set to: $temp0resourceparameter"
# End: load resource parameter


# Start: load input parameter
write-addylog "Initializing input parameter"
$addyPayloadResourceInput = $resultInitializeInvoke.jobDataArray.businessAutomationJobsPendingPayload.payload

[string]$LocationName = $addyPayloadResourceInput.temp1parameter # "temp1parameter" is definied in the Resource input Parameter. This are now a parameter for this script 
$temp1parameter = $temp1parameter.trim()
write-addylog "[input parameter] `"temp1parameter`" is set to: $temp1parameter"
# End: load input parameter


#
#
#
# Main part
#
#
#


# Start: cleanup commands
$EndOfScript = get-date
write-addylog "Start of script: $startOfScript"
write-addylog "End of script: $EndOfScript"
$errorCount = 0
$error.clear()
$addyPayloadResourceInput = ""; $addyPayloadResourceParameter = "" # Reset important variables
# End: cleanup commands


###################### End main - Start footer ############################
# setting the state
if ($debugScript -eq 0) {
    write-addylog "update state of this job to done"
    update-modifiedState -jobId $jobId -modifiedState "done" -publickey $publickey -privatekey $privatekey -machineId $machineId
    Start-Sleep 2 
    write-addylog "End of script reached"
    Start-Sleep 2
    exit
}