~cloudbaseit/charms/win2012r2/drupal-iis/trunk

« back to all changes in this revision

Viewing changes to lib/Modules/Networking/Networking.psm1

  • Committer: Ionut Balutoiu
  • Date: 2016-12-23 12:55:39 UTC
  • Revision ID: ibalutoiu@cloudbasesolutions.com-20161223125539-j795kbynab3uflha
Added charm code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2016 Cloudbase Solutions Srl
 
2
#
 
3
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
4
#    not use this file except in compliance with the License. You may obtain
 
5
#    a copy of the License at
 
6
#
 
7
#         http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
#    Unless required by applicable law or agreed to in writing, software
 
10
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
#    License for the specific language governing permissions and limitations
 
13
#    under the License.
 
14
 
 
15
function Invoke-DHCPRenew {
 
16
    [CmdletBinding()]
 
17
    Param(
 
18
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
 
19
        [Microsoft.Management.Infrastructure.CimInstance]$NetAdapter
 
20
    )
 
21
    PROCESS {
 
22
        if($NetAdapter.CreationClassName -ne "MSFT_NetAdapter"){
 
23
            Throw ("Invalid object class: {0}" -f $NetAdapter.CreationClassName)
 
24
        }
 
25
        $ifIndex = $NetAdapter.ifIndex
 
26
 
 
27
        $interface = Get-CimInstance -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.InterfaceIndex -eq $ifIndex}
 
28
        if($interface.IPEnabled -eq $false) {
 
29
            Throw "IP subsystem not enabled on this interface"
 
30
        } 
 
31
        if ($interface.DHCPEnabled -eq $false) {
 
32
            Throw "Interface not configured for DHCP"
 
33
        }
 
34
        $code = Invoke-CimMethod -CimInstance $interface -MethodName "RenewDHCPLease"
 
35
        return $code.ReturnValue
 
36
    }
 
37
}
 
38
 
 
39
function Invoke-DHCPRelease {
 
40
    [CmdletBinding()]
 
41
    Param(
 
42
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
 
43
        [Microsoft.Management.Infrastructure.CimInstance]$NetAdapter
 
44
    )
 
45
    PROCESS {
 
46
        if($NetAdapter.CreationClassName -ne "MSFT_NetAdapter"){
 
47
            Throw ("Invalid object class: {0}" -f $NetAdapter.CreationClassName)
 
48
        }
 
49
        $ifIndex = $NetAdapter.ifIndex
 
50
 
 
51
        $interface = Get-CimInstance -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.InterfaceIndex -eq $ifIndex}
 
52
        if($interface.IPEnabled -eq $false) {
 
53
            return 0
 
54
        } 
 
55
        if ($interface.DHCPEnabled -eq $false) {
 
56
            Throw "Interface not configured for DHCP"
 
57
        }
 
58
        $code = Invoke-CimMethod -CimInstance $interface -MethodName "ReleaseDHCPLease"
 
59
        return $code.ReturnValue
 
60
    }
 
61
}
 
62
 
 
63
Export-ModuleMember -Function * -Alias *
 
 
b'\\ No newline at end of file'