1.0.7/DavinciPS.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
Function Get-SubnetAddresses {
    Param ([IPAddress]$IP,[ValidateRange(0, 32)][int]$maskbits)

    # Convert the mask to type [IPAddress]
    #IP address is built backwards
    $mask = ([Math]::Pow(2, $MaskBits) - 1) * [Math]::Pow(2, (32 - $MaskBits))
    $maskbytes = [BitConverter]::GetBytes([UInt32] $mask)
    $DottedMask = [IPAddress]((3..0 | ForEach-Object { [String] $maskbytes[$_] }) -join '.')

    # bitwise AND them together to get the subnet ID
    $lower = [IPAddress] ( $ip.Address -band $DottedMask.Address )

    # Similar operation for the broadcast address
    # subnet mask bytes need to be inverted and reversed before adding
    $LowerBytes = [BitConverter]::GetBytes([UInt32] $lower.Address)
    [IPAddress]$upper = (0..3 | %{$LowerBytes[$_] + ($maskbytes[(3-$_)] -bxor 255)}) -join '.'

    # Make an object for use elsewhere
    Return [pscustomobject][ordered]@{
    Lower=$lower
    Upper=$upper
    }
}
Function Get-IPRange {
    param (
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName)][IPAddress]$lower,
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName)][IPAddress]$upper
    )

    $IPList = [Collections.ArrayList]::new()
    $null = $IPList.Add($lower)
    $i = $lower

    # increment ip until reaching $upper in range
    while ( $i -ne $upper ) { 
     # IP octet values are built back-to-front, so reverse the octet order
        $iBytes = [BitConverter]::GetBytes([UInt32] $i.Address)
        [Array]::Reverse($iBytes)

        #+1 the int value and reverse again
        $nextBytes = [BitConverter]::GetBytes([UInt32]([bitconverter]::ToUInt32($iBytes,0) +1))
        [Array]::Reverse($nextBytes)

        # Convert to IP and add to list
        $i = [IPAddress]$nextBytes
        $null = $IPList.Add($i)
    }

    return $IPList
    }

function Get-AvailableIPs {
    param (
        $networkAddress,
        $CIDR,
        $DNSZone,
        $serverName
    )

#Get a list of IP addresses
$referenceList = Get-SubnetAddresses $networkAddress $CIDR | Get-IPRange | Select-Object -ExpandProperty IPAddressToString

#Query DNS to get a list of entries
$result = Get-DnsServerResourceRecord -ZoneName $DNSZone -ComputerName $serverName | 
Where-Object {$_.RecordData.ipv4address -like "1*"} |
Select-Object -Property @{Name = "IP Address"; Expression={$_.RecordData.IPv4Address}} 

#Compare the two lists
$compare = Compare-Object -ReferenceObject $referenceList -DifferenceObject $result."IP Address" -IncludeEqual

#Format the list to show a friendly label
$compare | ForEach-Object{
    if($_.sideIndicator -eq '<='){
        $_.sideIndicator = " Avalible"
    }
    if($_.sideIndicator -eq '=>'){
        $_.sideIndicator = " In use "
    }
    if($_.sideIndicator -eq '=='){
        $_.sideIndicator = " In use "
    }
}

#Display the results of the comparison to the user
$compare | Select-Object @{Name="IP Address";Expression={$_.InputObject}}, @{Name="Status";Expression={$_.sideIndicator}}

}

function Add-MailUser{
    param(
        [string]$FilePath
    )

Import-Csv c:\temp\ImportMailUser.csv | ForEach-Object {
    $Fname = $($_.FirstName)
    $Lname = $($_.LastName)
    $Name = $Fname + " " + $Lname
    $Email = $($_.ExternalEmail)
    $Password = $($_.Password)
    $SEmail = $Fname+$Lname+"_ext@epicroofing.ca"
    $groupName = $($_.GroupEmail)
    New-MailUser -Name $Name -ExternalEmailAddress $Email -FirstName $Fname -LastName $Lname -MicrosoftOnlineServicesID $SEmail -Password (ConvertTo-SecureString -String $password -AsPlainText -Force)

    Add-DistributionGroupMember -Identity $groupName -Member $Email
    }
}


Export-ModuleMember -Function Get-AvailableIPs
Export-ModuleMember -Function Add-MailUser