Have a Question?
Categories
Print

Set IP Fixe

Fonction de récupération de Réseau Courant

function CurrentNetwork {
    [hashtable]$return = @{}
    $adapter = Get-NetAdapter | ? {$_.Status -eq "up"} -Verbose
    $currentIP = ($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress
    $currentGateway = ($adapter | Get-NetIPConfiguration).IPv4DefaultGateway
    $currentMask = ($adapter | Get-NetIPConfiguration).IPv4Address.PrefixLength
    $currentDNS = $adapter | Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object ServerAddresses
    $currentInterface = ($adapter | Get-NetIPConfiguration).InterfaceAlias
    $currentIndex = ($adapter | Get-NetIPConfiguration).InterfaceIndex
    $currentInterfaceAlias = ($adapter | Get-NetIPConfiguration).InterfaceAlias
    $adaptateur = Get-CimInstance win32_networkadapterconfiguration | where {$_.IPAddress -ne $null} -Verbose
    
    $currentDNSadresses = ""
    foreach ($adress in $currentDNS.ServerAddresses)
    {
        $currentDNSadresses += $adress +" - "
    }
    
    $return.ip = $currentIP
    $return.Gateway = $currentGateway.NextHop
    $return.mask = $currentMask
    $return.dns = $currentDNS.ServerAddresses
    $return.name = $currentInterface
    $return.index = $currentIndex
    $return.InterfaceAlias = $currentInterfaceAlias
    return $return
}

Fonction de Nouveaux DNS

function NewDNS {
    Switch ($xml.conf.computer.pays)
    {
        "FR"
        {
	    $newDNS = "123.45.67.89", "123.123.123.123"
        }
        "CA"
        {
        $newDNS = "123.45.67.99", "123.45.45.45"
        }
        default
        {
        $newDNS = "123.45.67.77", "123.67.67.67"
        }
    }
    return $newDNS
}

Utilisation des 2 fonctions – stockage dans une variable

$currentNetwork = CurrentNetwork
$nextDNS = NewDNS

Transformation du résultat DNS en String pour injection

$nextDNSString = ""
    foreach($dns in $nextDNS)
    {
        if ($nextDNSString -eq "")
        {
            $nextDNSString = "'"+$dns.ToString()+"'"
        }
        else
        {
            $nextDNSString = $nextDNSString.ToString()+", '"+$dns.ToString()+"'"
        } 
    }

#Si la carte ethernet n'est pas en DHCP, alors elle possède déjà une IP Fixe, donc on va lui changer en fonction de nos besoins
if (!(Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName localhost).DHCPEnabled)
{
    Write-Host "IP fixe existante, changement ..."
    Remove-NetIPAddress -IPAddress $currentNetwork.ip -Confirm:$false
    New-NetIPAddress -InterfaceIndex $index -AddressFamily IPv4 -IPAddress $NewIp -PrefixLength $NewMask
    Write-Host "Nouvelle IP: $NewIp"
}
#Sinon la carte est en DHCP, alors on va la passer en IP fixe avec les informations demandées
else
{
    Write-Host "Nouvelle IP Fixe ..."
    Start-Process powershell -Verb runas -ArgumentList "New-NetIPAddress –InterfaceIndex $index –IPAddress $NewIp -DefaultGateway $NewGateway –PrefixLength $NewMask" -Verbose
    $cmd = '"Set-DnsClientServerAddress -InterfaceIndex '+ $index +' -ServerAddresses ('+$nextDNSString+')"'
    $processDNS = Start-Process powershell -Verb runas -ArgumentList ($cmd) -WindowStyle Hidden
    $processDNS.ExitCode
    Write-Host "Nouvelle IP: $NewIp"
}