Scripts/New-BPAWmiCondition.ps1

function New-BPAWmiCondition {    
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA WMI condition.
 
        .DESCRIPTION
            New-BPAWmiCondition creates a new WMI condition.
 
        .PARAMETER Name
            The name of the new object.
 
        .PARAMETER Computer
            The computer name, IP address or host name of the remote machine. Do not specify this parameter to use the local computer.
 
        .PARAMETER Namespace
            Specifies the WMI namespace to execute the query under (i.e. root\CIMV2).
 
        .PARAMETER WQLQuery
            Specifies the WMI query that should be executed.
 
        .PARAMETER UserName
            The user to connect to the computer.
 
        .PARAMETER Password
            The password for the specified user.
 
        .PARAMETER PollIntervalSeconds
            Determines how often this condition monitors for the resource value.
 
        .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 BPAServer
            The server to create the object on.
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 02/08/2018
            Date Modified : 02/12/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [string]$Computer = "",
        [string]$Namespace = "root\CIMV2",
        [string]$WQLQuery,
        [string]$UserName,
        #[string]$Password, API BUG: does not support setting password via REST call
        [int]$PollIntervalSeconds = 1,        

        [switch]$Wait = $true,
        [int]$Timeout = 0,
        [BPATimeMeasure]$TimeoutUnit = [BPATimeMeasure]::Seconds,
        [int]$TriggerAfter = 1,

        [string]$Notes = "",

        [ValidateScript({$_.TypeName -eq "Folder"})]
        $Folder,

        [string]$BPAServer
    )

    $guid = "{$((New-Guid).Guid)}"

    if (-not $BPAServer -and $global:BPAConnectionInfo.Count -gt 1) {
        throw "Multiple BPA Servers are connected, please specify which server to create a new condition on!"
    } elseif (-not $BPAServer) {
        $BPAServer = $BPAConnectionInfo.Server
    }

    $user = Get-BPAUser -BPAServer $BPAServer | Where-Object {$_.Name -ieq ($BPAConnectionInfo | Where-Object {$_.Server -eq $BPAServer}).Credential.UserName}
    if (-not $Folder) {
        # Place the task in the users condition folder
        $Folder = $user | Get-BPAFolder -Type CONDITIONS
    }

    # Get the template object from the PoshBPA\ObjectTemplates folder, and configure the object
    $newObject = Get-BPAObjectTemplate -ConditionType ([BPATriggerType]::WMI) -BPAServer $BPAServer
    $newObject.ID             = $guid
    $newObject.Name           = $Name
    $newObject.ParentID       = $Folder.ID
    $newObject.Path           = Join-Path -Path $Folder.Path -ChildPath $Folder.Name
    $newObject.CreatedBy      = $user.ID
    $newObject.Notes          = $Notes
    $newObject.Wait           = $Wait.ToBool()
    if ($newObject.Wait) {
        $newObject.Timeout      = $Timeout
        $newObject.TimeoutUnit  = $TimeoutUnit.value__
        $newObject.TriggerAfter = $TriggerAfter
    }
    $newObject.MachineName       = $Computer
    $newObject.Namespace         = $Namespace
    $newObject.WQLQuery          = $WQLQuery
    $newObject.Username          = $UserName
    #$newObject.Password = $Password
    $newObject.IntervalInSeconds = $PollIntervalSeconds

    $newObject | New-BPAObject -BPAServer $BPAServer
    return (Get-BPACondition -ID $guid -BPAServer $BPAServer)
}