Functions/Public/New-AMServiceCondition.ps1
function New-AMServiceCondition { <# .SYNOPSIS Creates a new Automate service condition. .DESCRIPTION New-AMServiceCondition creates a new service condition. .PARAMETER Name The name of the new object. .PARAMETER ServiceName The name of the service. .PARAMETER Action The state of the service to trigger on. .PARAMETER Wait Wait for the condition, or evaluate immediately. .PARAMETER Timeout If wait is specified, the amount of time before the condition times out. .PARAMETER TimeoutUnit The unit for Timeout (Seconds by default). .PARAMETER TriggerAfter The number of times the condition should occur before the trigger fires. .PARAMETER Notes The new notes to set on the object. .PARAMETER Folder The folder to place the object in. .PARAMETER Connection The server to create the object on. .LINK https://github.com/AutomatePS/AutomatePS/blob/master/Docs/New-AMServiceCondition.md #> [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="Low")] param ( [Parameter(Mandatory = $true, Position = 0)] [ValidateNotNullOrEmpty()] [string]$Name, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$ServiceName, [ValidateNotNullOrEmpty()] [AMServiceTriggerState]$Action = [AMServiceTriggerState]::StoppedResponding, [ValidateNotNullOrEmpty()] [switch]$Wait = $true, [int]$Timeout = 0, [AMTimeMeasure]$TimeoutUnit = [AMTimeMeasure]::Seconds, [int]$TriggerAfter = 1, [string]$Notes = "", [ValidateScript({$_.Type -eq "Folder"})] $Folder, [ValidateNotNullOrEmpty()] [ArgumentCompleter([AMConnectionCompleter])] $Connection ) if ($PSBoundParameters.ContainsKey("Connection")) { $Connection = Get-AMConnection -Connection $Connection } else { $Connection = Get-AMConnection } switch (($Connection | Measure-Object).Count) { 1 { if (-not $Folder) { $Folder = Get-AMDefaultFolder -Connection $Connection -Type CONDITIONS } switch ($Connection.Version.Major) { 10 { $newObject = [AMServiceTriggerv10]::new($Name, $Folder, $Connection.Alias) } {$_ -in 11,22,23,24} { $newObject = [AMServiceTriggerv11]::new($Name, $Folder, $Connection.Alias) } default { throw "Unsupported server major version: $_!" } } $newObject.Notes = $Notes $newObject.Wait = $Wait.ToBool() if ($newObject.Wait) { $newObject.Timeout = $Timeout $newObject.TimeoutUnit = $TimeoutUnit $newObject.TriggerAfter = $TriggerAfter } $newObject.Action = $Action $newObject.ServiceName = $ServiceName $newObject | New-AMObject -Connection $Connection } 0 { throw "No servers are currently connected!" } default { throw "Multiple Automate servers are connected, please specify which server to create the new condition on!" } } } |