PowerOpCon.psm1

function Get-OpConMasterSchedule {
    <#
    .SYNOPSIS
        This script Retrieves the schedules from SMA OpCon.
 
    .NOTES
        Name: Get-OpConMasterSchedule
        Author: Bruce Stump
 
    .DESCRIPTION
        Retrieve schedules from SMA OpCon (An automation tool).
        Uses PowerShell 6.0 and above.
 
        -OpConServerUrl: Url to server, Mandatory.
        -OpConServerPort: Server port, defaults to 443.
        -OpConEndPoint: Rest API Endpoint for query, defaults to
                            'api/masterSchedules'.
        -Token: Token for authentication, Mandatory.
 
    .EXAMPLE
        Get-OpConMasterSchedule -OpConServerUrl $OpConServerUrl -Token $Token
 
    .EXAMPLE
        Get-OpConMasterSchedule -OpConServerUrl $OpConServerUrl -OpConServerPort 443
        -OpConEnPoint 'api/masterSchedules' -Token $Token
    #>


    [CmdletBinding()]

    param(
        [Parameter(Mandatory=$true)]
        [string]$OpconServerUrl,
        [string]$OpConServerPort = '443',
        [string]$OpConEndpoint = 'api/masterSchedules',
        [Parameter(Mandatory=$true)]
        [string]$Token
    )

    begin {
        $baseUrl = $OpconServerUrl + ':' + $OpconServerPort + '/' + $OpconEndpoint
    }

    process {
        $headers = @{
        Authorization = "Token $token"
        Accept = "application/json"
    }

        Invoke-RestMethod -Uri "$baseUrl" -Method Get -Headers $headers -SkipCertificateCheck
    }

    end {}
}
function Get-OpConMasterJob {
    <#
    .SYNOPSIS
        This script Retrieves the jobs from SMA OpCon.
 
    .NOTES
        Name: Get-OpConMasterJob
        Author: Bruce Stump
 
    .DESCRIPTION
        Retrieve jobs from SMA OpCon (An automation tool).
        Uses PowerShell 6.0 and above.
 
        -OpConServerUrl: Url to server, Mandatory.
        -OpConServerPort: Server port, defaults to 443.
        -OpConEndPoint: Rest API Endpoint for query, defaults to
                            'api/masterJobs'.
        -Token: Token for authentication, Mandatory.
 
    .EXAMPLE
        Get-OpConMasterSchedule -OpConServerUrl $OpConServerUrl -Token $Token
 
    .EXAMPLE
        Get-OpConMasterJob -OpConServerUrl $OpConServerUrl -OpConServerPort 443
        -OpConEnPoint 'api/masterJobs' -Token $Token
    #>


    [CmdletBinding()]

    param(
        [Parameter(Mandatory=$true)]
        [string]$OpconServerUrl,
        [string]$OpConServerPort = '443',
        [string]$OpConEndpoint = 'api/masterJobs',
        [Parameter(Mandatory=$true)]
        [string]$Token
    )

    begin {
        $baseUrl = $OpconServerUrl + ':' + $OpconServerPort + '/' + $OpconEndpoint
    }

    process {
        $headers = @{
        Authorization = "Token $token"
        Accept = "application/json"
    }

        Invoke-RestMethod -Uri "$baseUrl" -Method Get -Headers $headers -SkipCertificateCheck
    }

    end {}
}
function Get-OpConGlobalProperty {
    <#
    .SYNOPSIS
        This script Retrieves the global properties from SMA OpCon.
 
    .NOTES
        Name: Get-OpConGlobalProperty
        Author: Bruce Stump
 
    .DESCRIPTION
        Retrieve global properties from SMA OpCon (An automation tool).
        Uses PowerShell 6.0 and above.
 
        -OpConServerUrl: Url to server, Mandatory.
        -OpConServerPort: Server port, defaults to 443.
        -OpConEndPoint: Rest API Endpoint for query, defaults to
                            'api/globalproperties'.
        -Token: Token for authentication, Mandatory.
 
    .EXAMPLE
        Get-OpConMasterSchedule -OpConServerUrl $OpConServerUrl -Token $Token
 
    .EXAMPLE
        Get-OpConGlobalProperty -OpConServerUrl $OpConServerUrl -OpConServerPort 443
        -OpConEnPoint 'api/globalproperties' -Token $Token
    #>


    [CmdletBinding()]

    param(
        [Parameter(Mandatory=$true)]
        [string]$OpconServerUrl,
        [string]$OpConServerPort = '443',
        [string]$OpConEndpoint = 'api/globalproperties',
        [Parameter(Mandatory=$true)]
        [string]$Token
    )

    begin {
        $baseUrl = $OpconServerUrl + ':' + $OpconServerPort + '/' + $OpconEndpoint
    }

    process {
        $headers = @{
        Authorization = "Token $token"
        Accept = "application/json"
    }

        Invoke-RestMethod -Uri "$baseUrl" -Method Get -Headers $headers -SkipCertificateCheck
    }

    end {}
}
function Get-OpConScript {
    <#
    .SYNOPSIS
        This script Retrieves scripts information from SMA OpCon.
 
    .NOTES
        Name: Get-OpConScript
        Author: Bruce Stump
 
    .DESCRIPTION
        Retrieve scripts from SMA OpCon (An automation tool). There are options to only
        query used or non used scripts. This will help in cleaning up any unused scripts
        in OpCon.
 
        Uses PowerShell 6.0 and above.
 
        -OpConServerUrl: Url to server, Mandatory.
        -OpConServerPort: Server port, defaults to 443.
        -Token: Token for authentication, Mandatory.
        -ScriptType: Query all, used, or not used scripts.
 
    .EXAMPLE
        Get-OpConScript -OpConServerUrl $OpConServerUrl -Token $Token -ScriptType NotUsed
 
    .EXAMPLE
        Get-OpConScript -OpConServerUrl $OpConServerUrl -OpConServerPort 443
        -Token $Token -ScriptType Used
    #>


    [CmdletBinding()]

    param(
        [Parameter(Mandatory=$true)]
        [string]$OpconServerUrl,
        [string]$OpConServerPort = '443',
        [Parameter(Mandatory=$true)]
        [string]$Token,
        [Parameter(Mandatory=$true)]
        [ValidateSet("All","Used","NotUsed")]
        [string]$ScriptType
    )

    begin {
        $scriptBaseUrl = $OpconServerUrl + ':' + $OpconServerPort + '/api/Scripts'

        $headers = @{
            Authorization = "Token $token"
            Accept = "application/json"
        }

        $scriptID = @()
    }

    process {
        # Get all scripts.
        $opConScripts = Invoke-RestMethod -Uri "$scriptBaseUrl" -Method Get -Headers $headers -SkipCertificateCheck

        # Get IDs of all scripts.
        $opConScripts.foreach({$scriptID += $_.id})

        # Get expanded details of scripts based off of script IDs.
        $scriptObject = $scriptID.foreach({
            $scriptEndPoint = $scriptBaseUrl + '/' + $_

            Invoke-RestMethod -Uri "$scriptEndPoint" -Method Get -Headers $headers -SkipCertificateCheck
        })

        # Output details of scripts based on Script Type.
        switch ($ScriptType) {
            "All"       { $scriptObject }
            "Used"      { $scriptObject.Where({$null -ne $_.crossReferences.crossReferenceType}) }
            "NotUsed"   { $scriptObject.Where({$null -eq $_.crossReferences.crossReferenceType}) }
            default     { Write-Output "Unknown type"  }
        }
    }

    end {}
}
function Get-OpConUser {
    <#
    .SYNOPSIS
        This script Retrieves users from SMA OpCon.
 
    .NOTES
        Name: Get-OpConUser
        Author: Bruce Stump
 
    .DESCRIPTION
        Retrieve users from SMA OpCon (An automation tool).
        Uses PowerShell 6.0 and above.
 
        -OpConServerUrl: Url to server, Mandatory.
        -OpConServerPort: Server port, defaults to 443.
        -OpConEndPoint: Rest API Endpoint for query, defaults to
                            'api/Users'.
        -Token: Token for authentication, Mandatory.
 
    .EXAMPLE
        Get-OpConMasterSchedule -OpConServerUrl $OpConServerUrl -Token $Token
 
    .EXAMPLE
        Get-OpConUsers -OpConServerUrl $OpConServerUrl -OpConServerPort 443
        -OpConEnPoint 'api/Users' -Token $Token
    #>


    [CmdletBinding()]

    param(
        [Parameter(Mandatory=$true)]
        [string]$OpconServerUrl,
        [string]$OpConServerPort = '443',
        [string]$OpConEndpoint = 'api/Users',
        [Parameter(Mandatory=$true)]
        [string]$Token
    )

    begin {
        $baseUrl = $OpconServerUrl + ':' + $OpconServerPort + '/' + $OpconEndpoint
    }

    process {
        $headers = @{
        Authorization = "Token $token"
        Accept = "application/json"
    }

        Invoke-RestMethod -Uri "$baseUrl" -Method Get -Headers $headers -SkipCertificateCheck
    }

    end {}
}

function New-OpConGlobalProperty {
    <#
    .SYNOPSIS
        This script creates new global properties.
 
    .NOTES
        Name: New-OpConGlobalProperty
        Author: Bruce Stump
 
    .DESCRIPTION
        Create a new global property in SMA OpCon.
        Uses PowerShell 6.0 and above.
 
        -OpConServerUrl: Url to server, Mandatory.
        -OpConServerPort: Server port, defaults to 443.
        -OpConEndPoint: Rest API Endpoint for query, defaults to
                            'api/globalproperties'.
        -Token: Token for authentication, Mandatory.
 
    .EXAMPLE
        New-OpConGlobalProperty -OpConServerUrl $OpConServerUrl -Token $Token
 
    .EXAMPLE
        New-OpConGlobalProperty -OpConServerUrl $OpConServerUrl -OpConServerPort 443
        -OpConEnPoint 'api/globalproperties' -Token $Token
    #>


    [CmdletBinding()]

    param (
        [Parameter(Mandatory=$true)]
        [string]$OpconServerUrl,
        [string]$OpConServerPort = '443',
        [string]$OpConEndpoint = 'api/globalproperties',
        [Parameter(Mandatory=$true)]
        [string]$Token,
        [Parameter(Mandatory=$true)]
        [string]$PropertyName,
        [Parameter(Mandatory=$true)]
        [string]$PropertyVal,
        [Parameter(Mandatory=$true)]
        [string]$PropertyDesc
    )

    begin {
        $baseUrl = $OpconServerUrl + ':' + $OpconServerPort + '/' + $OpconEndpoint
    }

    process {
        $headers = @{
            Authorization = "Token $Token"
            'Content-Type' = "application/json-patch+json"
        }

        $propertyBody = @{
            name = $PropertyName
            value = $PropertyVal
            documentation = $PropertyDesc
        } | ConvertTo-Json

        $globalPropertyParams = @{
            Uri = $baseUrl
            Method = "Post"
            Headers = $headers
            Body = $propertyBody
        }

        Invoke-RestMethod @globalPropertyParams -SkipCertificateCheck
    }

    end {}
}