MZN.Common.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#### 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
            }
        }        
    }
}