Network.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#region Functions
#region Download-File
function Download-File()
{
    #Input Parameters
    Param(
        [string]$Source,
        [string]$destinationDirectory,
        [string]$destinationFilename,
        [Parameter(Mandatory=$false)] 
        [ValidateSet("IE11-Win8.1","IE11-Win8.1x64","Chrome35","Safari6","Firefox31")] 
        [string]$UserAgent="Chrome35"
)

    # Check if source is provided
    if( -not($Source) )
    {
        throw "You must specify a value for source."
        return
    }

    # Check if destination file name is provided
    if( -not($destinationFilename))
    {
        # Create a filename from URL
        $filename = $Source.substring($Source.lastindexof("/") + 1, $Source.length - $Source.lastindexof("/") - 1)
    }
    else
    {
        $filename = $destinationFilename
    }

    # Check if destination directory is provided
    if( -not ($destinationDirectory))
    {
        $destination = (resolve-path .).path + "\"
        $destination = (resolve-path .).path + "\" + $filename
    }
    else
    {
        if( -not(test-path $destinationdirectory))
        {
            throw "Destination Directory does not exist!"
            return
        }
        $destination = (resolve-path $destinationDirectory).path + "\" + $filename    
    }

    # Download file
    $wc = New-Object System.Net.WebClient
    $ua

    if( $UserAgent -eq "IE11-Win8.1" )
    {
        $ua = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"
    }
    if( $UserAgent -eq "IE11-Win8.1x64" )
    {
        $ua = "Mozilla/5.0 (Windows NT 6.3; Win64, x64; Trident/7.0; Touch; rv:11.0) like Gecko"
    }
    if( $UserAgent -eq "Chrome35" )
    {
        $ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36"
    }
    if( $UserAgent -eq "Safari6" )
    {
        $ua = "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"
    }
    if( $UserAgent -eq "Firefox31" )
    {
        $ua = "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0"
    } 

    $wc.Headers.Add("user-agent", $ua)
    $wc.DownloadFile($Source, $destination)
}
#endregion

#region Get-ExternalIP
function Get-ExternalIP 
{
    $wc=New-Object net.webclient
    $wc.downloadstring("http://checkip.dyndns.com") -replace "[^\d\.]"
}
#endregion

#region Get-MACAddress
function Get-MACAddress
{
    param($Target);
    $object = New-Object Object

    if( ($Target -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") -eq $true )
    {
        arp -d; # clear arp cache
        $ping = (new-object System.Net.NetworkInformation.Ping).Send($Target);
        $mac = arp -a;
        if($ping)
        {
            ($mac | ? {$_ -match $Target}) -match "([0-9A-F]{2}([:-][0-9A-F]{2}){5})" | out-null;
            Add-Member -memberType NoteProperty -name "IP Address" -value $Target -inputObject $object
            Add-Member -memberType NoteProperty -name "MAC Address" -value $matches[0] -inputObject $object
        }
    }
    else
    {
        $ip = [System.Net.Dns]::GetHostAddresses($Target) | where-object {$_.IPAddressToString -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" } | foreach {$_.IPAddressToString}

        arp -d; # clear arp cache
        $ping = (new-object System.Net.NetworkInformation.Ping).Send($ip);
        $mac = arp -a;              
        if($ping)
        {
            ($mac | ? {$_ -match $ip}) -match "([0-9A-F]{2}([:-][0-9A-F]{2}){5})" | out-null;
            Add-Member -memberType NoteProperty -name "IP Address" -value $ip -inputObject $object
            Add-Member -memberType NoteProperty -name "MAC Address" -value $matches[0] -inputObject $object
        }
    }
    $object
}
#endregion

#region Get-MACAddressWMI
function Get-MACAddressWMI
{
    Param
    (
        [string]$ComputerName = ".",
        [system.management.automation.psCredential]$Credential
    )

    if($Credential)
    {
        Get-WMIObject -Class win32_networkadapterconfiguration -ComputerName $ComputerName -Credential $Credential | Where-Object {$_.macaddress.length -gt 0} | Select-Object -Property Description, MACAddress | Format-Table
    }
    else
    {
        Get-WMIObject -Class win32_networkadapterconfiguration -ComputerName $ComputerName | Where-Object {$_.macaddress.length -gt 0} | Select-Object -Property Description, MACAddress | Format-Table
    }
}
#endregion

#region Ping-Host
function Ping-Host
{
    param([string]$HostName,[int32]$Requests = 3)
    
    for ($i = 1; $i -le $Requests; $i++) {
        $Result = Get-WmiObject -Class Win32_PingStatus -ComputerName . -Filter "Address='$HostName'"
        Start-Sleep -Seconds 1
        if ($Result.StatusCode -ne 0) {return $FALSE}
    }
    return $TRUE
}
#endregion

#region Get-NetAdapterIP
function Get-NetAdapterIP
{
    Param(
    )

    $items = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .
    $results = @()

    foreach($i in $items)
    {
        $obj = New-Object PSObject
        $adaptername = (Get-NetAdapter -InterfaceDescription $i.Description).Name
        $obj | Add-Member -MemberType NoteProperty -Name Adapter -Value $adaptername
        $obj | Add-Member -MemberType NoteProperty -Name Address -Value $i.ipaddress
        $obj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $i.macaddress
        $results += $obj
    }

    Write-Output $results
}
#endregion

#region Test-Port
Function Test-Port
{
    Param
    (
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Server')]
        [string[]]$Computername,

        [Parameter(Mandatory = $true, Position = 1)]
        [int]$Port,

        [Parameter(Mandatory = $false, Position = 2)]
        [ValidateSet("TCP","UDP")]
        $Protocol = "TCP",

        [Parameter(Mandatory = $false)]
        $Timeout = 3
    )

    Begin {}

    Process
    {
        # Create the client
        $client = New-Object System.Net.Sockets.TcpClient

        foreach($c in $Computername)
        {
            # Create the custom object to return
            $result = New-Object psobject
            $result | Add-Member NoteProperty "Destination Hostname" $c

            # Get the IPs of the hostname from DNS
            try
            {
                $IPs = [System.Net.Dns]::GetHostAddresses($c) -join ","
                $result | Add-Member NoteProperty "Resolved IP" $IPs
            }
            catch
            {
                $result | Add-Member NoteProperty "Resolved IP" "Could Not Resolve"
            }

            # Create the timespan for the responce
            $date = Get-Date
            $timeToWait = New-TimeSpan -Start $date -End $date.AddSeconds($Timeout)

            if($Protocol -eq "TCP")
            {
                try
                {
                    # Try to connect
                    $connectionResult = $client.BeginConnect($c, $Port, $null, $null)
                    $success = $connectionResult.AsyncWaitHandle.WaitOne($timeToWait.Seconds)
                    if(!$success)
                    {
                        # Timeout
                        $result | Add-Member NoteProperty "Connected IP" $client.Client.RemoteEndPoint.Address
                        $result | Add-Member NoteProperty "Connected Port" $client.Client.RemoteEndPoint.Port
                        $result | Add-Member NoteProperty "Local IP" $client.Client.LocalEndPoint.Address
                        $result | Add-Member NoteProperty "Local Port" $client.Client.LocalEndPoint.Port
                        $result | Add-Member NoteProperty "Status" "Timeout"
                    }
                    else
                    {
                        $client.EndConnect($connectionResult)

                        # Add the IP that the client connected to and the status of the port to the custom object
                        $result | Add-Member NoteProperty "Connected IP" $client.Client.RemoteEndPoint.Address
                        $result | Add-Member NoteProperty "Connected Port" $client.Client.RemoteEndPoint.Port
                        $result | Add-Member NoteProperty "Local IP" $client.Client.LocalEndPoint.Address
                        $result | Add-Member NoteProperty "Local Port" $client.Client.LocalEndPoint.Port
                        $result | Add-Member NoteProperty "Status" "Listening"
                    }
                }
                catch [System.Net.Sockets.SocketException]
                {
                    # Add the "Connected IP" property and the status of the port
                    $result | Add-Member NoteProperty "Connected IP" $client.Client.RemoteEndPoint.Address
                    $result | Add-Member NoteProperty "Connected Port" $client.Client.RemoteEndPoint.Port
                    $result | Add-Member NoteProperty "Local IP" $client.Client.LocalEndPoint.Address
                    $result | Add-Member NoteProperty "Local Port" $client.Client.LocalEndPoint.Port
                    $result | Add-Member NoteProperty "Open" "Timeout"
                }
                finally
                {
                    # Close and dispose the client
                    $client.Close()
                    $client.Dispose()
                }
            }
            else
            {
                # Create the client
                $client = New-Object System.Net.Sockets.Udpclient(0)

                # Create a random message
                $encoder = new-object system.text.asciiencoding
                $message = $encoder.GetBytes("$(Get-Date)")

                try
                {
                    # Try to connect
                    $client.Connect($c,$port)
                    $result | Add-Member NoteProperty "Connected IP" $client.Client.RemoteEndPoint.Address
                    $result | Add-Member NoteProperty "Connected Port" $client.Client.RemoteEndPoint.Port
                    $result | Add-Member NoteProperty "Local IP" $client.Client.LocalEndPoint.Address
                    $result | Add-Member NoteProperty "Local Port" $client.Client.LocalEndPoint.Port

                    # Send and receive the test message
                    [void]$client.Send($message,$message.length)
                    $asyncResult = $client.BeginReceive($null,$null)
                    $asyncResult.AsyncWaitHandle.WaitOne($timeToWait) | Out-Null

                    if ($asyncResult.IsCompleted)
                    {
                        # Received a responce
                        $result | Add-Member NoteProperty "Status" "Listening"
                    }
                    else
                    {
                        # The timeout was reached
                        $result | Add-Member NoteProperty "Status" "Timeout"
                    }
                }
                catch [System.Net.Sockets.SocketException]
                {
                    # Add the "Connected IP" property and the status of the port
                    $result | Add-Member NoteProperty "Connected IP" $client.Client.RemoteEndPoint.Address
                    $result | Add-Member NoteProperty "Connected Port" $client.Client.RemoteEndPoint.Port
                    $result | Add-Member NoteProperty "Local IP" $client.Client.LocalEndPoint.Address
                    $result | Add-Member NoteProperty "Local Port" $client.Client.LocalEndPoint.Port
                    $result | Add-Member NoteProperty "Status" "Timeout"
                }
                finally
                {
                    # Close and dispose the client
                    $client.Close()
                    $client.Dispose()
                }
            }
            # Return the result
            $result
        }
    }

    End {}
}
#endregion

#endregion

#region Aliases
New-Alias Get-IP Get-NetAdapterIP
#endregion