none
powershell script to set ip address for Local Area Connection

    Question

  • Trying to write a script that will ask for some basic IP info, and then assign it to the Local Area Connection interface.. I am using what is below and it's not really working.

     

    I am running it on a server with an existing static IP address. No matter what I enter the script will only change the gateway, and it won't add the secondary DNS server. (I don't have one entered at all on this server)

     

    I'd like the script to ask for IP, mask, gateway, and prompt for DNS1 and DNS2.

     

    It would be a huge bonus if I could alter the DNS prompt a little.

    Something like "Enter DNS1, or press enter to accept the default 10.210.1.100"

    Can anyone help me out with this? I'm not partial to the code block below, if we need to trash it, lets trash it. 

     

     

    function Set-IPAddress {
    		param(	[string]$networkinterface = "Local Area Connection",
    			[string]$ip = $(read-host "Enter an IP Address (ie 10.215.1.10)"),
    			[string]$mask = $(read-host "Enter the subnet mask (ie 255.255.0.0)"),
    			[string]$gateway = $(read-host "Enter the default gateway (ie 10.215.1.250"),
    			[string]$dns1 = "10.210.1.101",
    			[string]$dns2 = "10.210.1.130",
    			[string]$registerDns = "TRUE"
         )
         
             
         #Start writing code here
         $dns = $dns1
         if($dns2){$dns ="$dns1,$dns2"}
         $index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $networkinterface}).InterfaceIndex
         $NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}
         $NetInterface.EnableStatic($ip, $mask)
         $NetInterface.SetGateways($gateway)
         $NetInterface.SetDNSServerSearchOrder($dns)
         $NetInterface.SetDynamicDNSRegistration($registerDns)
        
     }
     
     Set-IPAddress
    

    Thursday, March 17, 2011 6:14 PM

Answers

All replies

  • OK in order to change the prompt you can use
    read-host -prompt "Enter DNS1, or press enter to accept the default 10.210.1.100"

    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com
    Thursday, March 17, 2011 8:21 PM
  • $dns ="$dns1,$dns2" replace to $dns =$dns1,$dns2

    Thursday, March 17, 2011 8:27 PM
  • OK in order to change the prompt you can use
    read-host -prompt "Enter DNS1, or press enter to accept the default 10.210.1.100"

    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com
    That doesn't make much sense. 
    Thursday, March 17, 2011 8:35 PM
  • $dns ="$dns1,$dns2" replace to $dns =$dns1,$dns2

    That worked. Thank you.
    Thursday, March 17, 2011 8:39 PM
  • OK in order to change the prompt you can use
    read-host -prompt "Enter DNS1, or press enter to accept the default 10.210.1.100"

    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com
    That doesn't make much sense. 
    $dns1 = $(if ($a=read-host "Enter DNS1, or press enter to accept the default 10.210.1.100") {$a} else {"10.210.1.100"})
    Thursday, March 17, 2011 8:54 PM
  • OK in order to change the prompt you can use
    read-host -prompt "Enter DNS1, or press enter to accept the default 10.210.1.100"

    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com
    That doesn't make much sense. 
    $dns1 = $(if ($a=read-host "Enter DNS1, or press enter to accept the default 10.210.1.100") {$a} else {"10.210.1.100"})
    Perfect.
    Thursday, March 17, 2011 9:05 PM
  • Dear

    I have executed this script in Powershell

    function Set-IPAddress {
      param ([string] $networkInterface = "Local Area Connection",
       [String] $ip = $(read - 10.215.1.10 "Entrez une adresse IP (c'est à dire 10.215.1.10)"),
       [String] $masque = $(read - 255.255.255.0 "Entrez le masque de sous-réseau (c'est à dire 255.255.0.0)"),
       [String] $passerelle = $(read - 10.215.1.250 "Entrez la passerelle par défaut (c'est à dire 10.215.1.250"),
       [String] $DNS1 = "10.210.1.101",
       [String] $DNS2 = "10.210.1.130",
       [String] $registerDns = "TRUE"
         )

    i have saved this script and i have executed this in a dos screen

    c:\>powershell c:\Ip.ps1

    I don't have any error but my ip is always the self>????

     

     

     


    Thursday, June 16, 2011 3:10 PM
  • Could this script be modified to pull the info from an Excel spreadsheet saved on a network share?
    Monday, September 26, 2011 3:26 PM