MZN.Common.psm1

#### FUNCTIONS ####
<#
.Synopsis
   Returns Installed and Issued license of the RD Farm
.DESCRIPTION
   Uses WMI to querie the license servers and fetches the installed license and issued license
.EXAMPLE
   Get-RDLicenseReport -computername LIC001
#>

Function Get-RDLicenseReport
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [alias('LicenseServer') ]
        $ComputerName
    )

    Process
    {
        $fileName = (Invoke-WmiMethod Win32_TSLicenseReport -Name GenerateReportEx -ComputerName $ComputerName).FileName
        $summaryEntries = (Get-WmiObject Win32_TSLicenseReport -ComputerName $ComputerName|Where-Object FileName -eq $fileName).FetchReportSummaryEntries(0,0).ReportSummaryEntries
        return $summaryEntries
    }
    
}

<#
.Synopsis
   Sends RD License Report in a mail
.DESCRIPTION
   Sends RD License Report in a mail
.EXAMPLE
   Send-RDLicenseReport -SMTPServer contoso-com.mail.protection.outlook.com -to test@contoso.com -computername LIC001 -Organisation "Contoso"
#>

Function Send-RDLicenseReport
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # The SMTP server used to send the mai
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $SMTPServer,

        # Mail address to send the mail to
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string[]]
        $To,

        # License server ComputerName
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [Alias('LicenseServer')]
        [string]
        $Computername,

        # The organisation where the report is run
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string]
        $Organisation
    )

    
    Process
    {
        $report = Get-RDLicenseReport -ComputerName $Computername

        Send-MailMessage -SmtpServer $SMTPServer -from RDLicenseReport@contoso.com -to $To -Subject "$Organisation RDLicense Summary Report" -body "Dear Reader, `n `nYou can find the current $Organisation license consumption below: `n `nProduct Version: $($report.ProductVersion) `nInstalled Licenses: $($report.InstalledLicenses) `nIssued Licenses: $($report.IssuedLicenses)"
    }
   
}

<#
.Synopsis
   Gets all the A records of the given DNS zone
.DESCRIPTION
   This function gets all the A Records of a DNS zone and returns them in a variable. To run this function the dnsserver powershell module must be installed.
.EXAMPLE
   Get-AllDNSRecords -dnsserver dc01.contoso.com -dnszone contoso.com
#>

Function Get-AllDNSRecords
{
    [CmdletBinding(SupportsShouldProcess=$true, 
                  ConfirmImpact='Low')]
    [Alias()]
    [OutputType([string])]
    Param
    (
        # Provide a DNS server hosting the DNS zone
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [ValidateNotNullOrEmpty()]
        [alias('DNSServer')]
        [string]
        $ComputerName,

        # Provide a DNS server hosting the DNS zone
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [alias('DNSZone')]
        [ValidateNotNullOrEmpty()]
        [string]
        $Zone
    )

    Process
    {
        if ($pscmdlet.ShouldProcess("$ComputerName", "Getting all DNS records from zone $Zone"))
        {
            try{
                $records = Get-DnsServerResourceRecord -ZoneName $Zone -ComputerName $ComputerName -EA stop
                Write-Verbose "Retrieved all DNS records from zone $zone"
                return ,$records
            }
            catch{
                Write-Error "Unable to get DNS records from zone $zone on server $ComputerName"
                return
            }
        }        
    }
}