Blog de Florent Appointaire

Blog sur les technologies Microsoft (Windows Server, System Center, Azure, Windows Azure Pack/Azure Stack, etc;)

Powershell

En référence à mon premier article (Programme Batch) j'ai créé le même script comme promis. Cette version prend en charge Powershell de A à Z version 2.

J'ai insérer Powershell V3 pour activer ou non le Firewall, je ferai ceci pour tous les modules courant 2013. Il y a encore pas mal de boulot comme faire toutes les vérifications, gérer les erreurs, etc.

Voici l'interface d'utilisation :

 Capture6

Je me forme actuellement sur Powershell avec interface graphique. Je pense que je convertirai ce script en interface graphique plus tard.

Ce script fonctionne sur Windows 7, Windows 8, Windows Server 2008 R2, Windows Server 2012.

En attendant je vous souhaite une bonne utilisation et si vous avez des remarques/suggestions, n'hésitez pas à me le laisser en commentaire ou à m'envoyer un mail :)

Pour le téléchargement du script, c'est ici. Renommez le fichier texte en ps1. N'oubliez pas de lancer le script en Administrateur.

Le code source :

#// ===========================================================================================
#//  Created 	       : 26/12/2012      
#//  Last modification : 26/12/2012                 
#//  For    	       : http://florent-appointaire.fr    
#// ===========================================================================================
#//  Object : IP Configuration with Powershell
#// ===========================================================================================
#//  
#//                             | Author: Florent APPOINTAIRE | 
#//      
#// ===========================================================================================

$powershellVersion = $Host.Version
$powershellVersion = $powershellVersion.Major
$date = Get-Date -Format dd-MM-yyyy
$hour = Get-Date -Format t
$userName = [Environment]::UserName
$location = $MyInvocation.MyCommand.Path

Function ipconfig/all { 
    ipconfig /all 
}

Function DHCP {
    Clear-Host
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||                     (1) = DHCP                      ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    $selectCard = Get-WmiObject Win32_NetworkAdapterConfiguration | Format-Table  Description,Index,DHCPEnabled -AutoSize
    $selectCard
    Write-Host "`t" -NoNewLine
    $select = Read-Host 'Select a DeviceID for the DHCP'
    $selectCard = Get-WmiObject Win32_NetworkAdapterConfiguration | Where {$_.Index -eq $select} 
    $selectCard.EnableDHCP()
    $selectCard.SetDNSServerSearchOrder()
    Write-Host "`t" -NoNewLine
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    powershell $location
} 

Function Config {
    Clear-Host
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||                (2) = Switch Configuration           ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    $selectCard = Get-WmiObject Win32_NetworkAdapterConfiguration | Format-Table  Description,Index,DHCPEnabled -AutoSize
    $selectCard
    Write-Host "`t" -NoNewLine
    $select = Read-Host 'Select a DeviceID for the automatic configuration'
    $selectCard = Get-WmiObject Win32_NetworkAdapterConfiguration | Where {$_.Index -eq $select} 
    $selectCard.EnableStatic("192.168.1.2", "255.255.255.0")
    $selectCard.SetGateways("192.168.1.1", 1)
    Write-Host "`t" -NoNewLine
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    powershell $location
} 

Function NetworkCard {
    Clear-Host
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||       (3) = Activate/Desactivate Network Card       ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    $selectCard = Get-WmiObject win32_NetworkAdapter | Format-Table  AdapterType,DeviceID,Name -AutoSize
    $selectCard
    Write-Host "`t" -NoNewLine
    $select = Read-Host 'Select a DeviceID'
    $selectCard = Get-WmiObject win32_NetworkAdapter | Where {$_.DeviceID -eq $select} 
    Write-Host "`t" -NoNewLine
    $response = Read-Host 'You want to activate or desactivate the network card with the id' $select '? (A/D)'
    if ($response -eq 'A'){
            $selectCard.Enable()
            $myDir
    } elseif ($response -eq 'D'){
            $selectCard.Disable()
            $myDir
    } else {
        Write-Host 'Please write (A)ctivate or (D)esactivate'
        NetworkCard
    }
    Write-Host "`t" -NoNewLine
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    powershell $location
}

Function Ip {
    Clear-Host
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||                   (4) = Change IP                   ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    $selectCard = Get-WmiObject Win32_NetworkAdapterConfiguration | Format-Table  Description,Index,DHCPEnabled -AutoSize
    $selectCard
    Write-Host "`t" -NoNewLine
    $select = Read-Host 'Select a DeviceID to change the IP address of a network card'
    $selectCard = Get-WmiObject Win32_NetworkAdapterConfiguration | Where {$_.Index -eq $select}
    $IPAddress = Read-Host 'IP Address'
    $netMask = Read-Host 'Mask'
    $gateway = Read-Host 'Gateway'
    $DNS = Read-Host 'DNS'
    $selectCard.EnableStatic($IPAddress, $netMask)
    $selectCard.SetGateways($gateway, 1)
    $selectCard.SetDNSServerSearchOrder($DNS)
    Write-Host "`t" -NoNewLine
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    powershell $location
} 

Function Ipc {
    Clear-Host
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||                   (5) = IPCONFIG                    ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    ipconfig/all
    Write-Host "`t" -NoNewLine
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    powershell $location
} 

Function Firewall {
    Clear-Host
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||                (6) = Firewall On/Off                ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    $response = Read-Host 'You want to activate or desactivate your firewall (A/D) ?'
    if ($response -eq 'A'){
        if ($powershellVersion -eq 2){
            netsh advfirewall set allprofiles state on
        } elseif ($powershellVersion -eq 3){
            Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True 
        }
    } elseif ($response -eq 'D'){
        if ($powershellVersion -eq 2){
            netsh advfirewall set allprofiles state off
        } elseif ($powershellVersion -eq 3){
            Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False 
        }
    } else {
        Write-Host 'Please write (A)ctivate or (D)esactivate'
        Firewall
    }
    Write-Host "`t" -NoNewLine
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    powershell $location
} 

Function Quit {
    Clear-Host
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||                      (7) Quit                       ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    $response = Read-Host 'Are you sur you want to quit (Y/N) ? '
    if ($response -eq 'Y'){
        $process =  Get-Process -ProcessName powershell
        Stop-Process $process.Id 
    } elseif ($response -eq 'N'){
        powershell $location    
    } else {
        Write-Host 'Please, write (Y)es or (N)o'
        Quit
    }   
}

Function Help {
    Clear-Host
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||                      (H) Help                       ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host " "
    Write-Host "`t" -NoNewLine
    Write-Host "This script support the OS Windows 7, Windows 8, Windows"
    Write-Host "`t" -NoNewLine
    Write-Host "Server 2008 R2, Windows Server 2012."
    Write-Host " "
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||                Improvements / Bugs                  ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host " "
    Write-Host "`t" -NoNewLine
    Write-Host "If you have any suggestions or ideas for improvements, or"
    Write-Host "`t" -NoNewLine
    Write-Host "if you encounter bugs, don't hesitate to contact me at "
    Write-Host "`t" -NoNewLine
    Write-Host "the address below."
    Write-Host " "
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host "`t" -NoNewLine
    Write-Host '||                       About                         ||'
    Write-Host "`t" -NoNewLine
    Write-Host '========================================================='
    Write-Host " "
    Write-Host "`t" -NoNewLine
    Write-Host "Author  : Florent APPOINTAIRE"
    Write-Host "`t" -NoNewLine
    Write-Host "Contact : florent.appointaire@gmail.com"
    Write-Host "`t" -NoNewLine
    Write-Host "Website : http://www.florent-appointaire.fr"
    Write-Host "`t" -NoNewLine
    Write-Host "Version : 1.0"
    Write-Host " "
    Write-Host "`t" -NoNewLine
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    powershell $location
}

Clear-Host
Write-Host "`t" -NoNewLine
Write-Host '========================================================='
Write-Host "`t" -NoNewLine
Write-Host '|| CONFIGURATION IP AUTOMATIQUE [Windows 7/ Windows 8] ||'
Write-Host "`t" -NoNewLine
Write-Host '========================================================='

Write-Host ''
Write-Host "`t" -NoNewLine
Write-Host 'Hello' $userName
Write-Host ''
Write-Host "`t" -NoNewLine
Write-Host 'Nous sommes le' $date 'et il est' $hour

Write-Host ''
Write-Host "`t" -NoNewLine
Write-Host '========================================================='
Write-Host "`t" -NoNewLine
Write-Host '||       (1) = DHCP                                    ||'
Write-Host "`t" -NoNewLine
Write-Host '||       (2) = Switch Configuration                    ||'
Write-Host "`t" -NoNewLine
Write-Host '||       (3) = Activate/Desactivate Network Card       ||'
Write-Host "`t" -NoNewLine
Write-Host '||       (4) = Change IP                               ||'
Write-Host "`t" -NoNewLine
Write-Host '||       (5) = IPCONFIG                                ||'
Write-Host "`t" -NoNewLine
Write-Host '||       (6) = Firewall On/Off                         ||'
Write-Host "`t" -NoNewLine
Write-Host '||       (7) = Quit                                    ||'
Write-Host "`t" -NoNewLine
Write-Host '||       (H) = Help                                    ||'
Write-Host "`t" -NoNewLine
Write-Host '========================================================='

Write-Host ''
Write-Host "`t" -NoNewLine
$choice = Read-Host 'What is your choice ? '

switch ($choice) 
    {
        1 { DHCP }
        2 { Config }
        3 { NetworkCard }
        4 { Ip }
        5 { Ipc }
        6 { Firewall }
        7 { Quit }
        H { Help } 
        default { C:\Users\Administrator\Desktop\Test2.ps1 }
    }
Facebook Like
Anonymous