none
Script for change IP

    Domanda

  • Hello,

    i does anybody know why is this script not working for setting dns servers? Thank you Rudolph

    		param(
            	[Parameter(Mandatory=$true)][string]$networkinterface,
    			[Parameter(Mandatory=$true)][string]$ip,
    			[Parameter(Mandatory=$true)][string]$mask,
    			[Parameter(Mandatory=$true)][string]$gateway,
    			[Parameter(Mandatory=$true)][string]$dns1,
    			[Parameter(Mandatory=$false)][string]$dns2
            )
         #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)

    giovedì 28 giugno 2012 09:45

Tutte le risposte

  • try:

    if($dns2){
    $NetInterface.SetDNSServerSearchOrder($dns1,$dns2)
    }
    else{
    $NetInterface.SetDNServerSearchOrder($dns1)
    }
    

    and see if that helps
    giovedì 28 giugno 2012 10:00
  • Code:

    		param(
            	[Parameter(Mandatory=$true)][string]$networkinterface,
    			[Parameter(Mandatory=$true)][string]$ip,
    			[Parameter(Mandatory=$true)][string]$mask,
    			[Parameter(Mandatory=$true)][string]$gateway,
    			[Parameter(Mandatory=$true)][string]$dns1,
    			[Parameter(Mandatory=$false)][string]$dns2
            )
         #Start writing code here
         $dns = $dns1
         if($dns2){
            $NetInterface.SetDNSServerSearchOrder($dns1,$dns2)
         } else{
            $NetInterface.SetDNServerSearchOrder($dns1)
         }
         $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)

    Error:

    PS C:\Users\dvorak.ruda\Desktop> .\SETIPBYMAC.ps1
    You cannot call a method on a null-valued expression.
    At C:\Users\dvorak.ruda\Desktop\SETIPBYMAC.ps1:14 char:45
    +         $NetInterface.SetDNServerSearchOrder <<<< ($dns1)
        + CategoryInfo          : InvalidOperation: (SetDNServerSearchOrder:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    
    
    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     : 
    __DYNASTY        : __PARAMETERS
    __RELPATH        : 
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         : 
    __NAMESPACE      : 
    __PATH           : 
    ReturnValue      : 0
    
    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     : 
    __DYNASTY        : __PARAMETERS
    __RELPATH        : 
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         : 
    __NAMESPACE      : 
    __PATH           : 
    ReturnValue      : 0
    
    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     : 
    __DYNASTY        : __PARAMETERS
    __RELPATH        : 
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         : 
    __NAMESPACE      : 
    __PATH           : 
    ReturnValue      : 84
    
    
    
    
    ____________________________________________________________________________________________________________________________________________________________________________________

    giovedì 28 giugno 2012 11:05
  • there is a typo in that line

    correct it:

    $NetInterface.SetDNServerSearchOrder($dns1)

    $NetInterface.SetDNSServerSearchOrder($dns1)

    giovedì 28 giugno 2012 11:08
  • whoaaa, wait a moment ! typo is one problem

    You use $NetInterface before initializing it !

    move this block of code at the end of script

    giovedì 28 giugno 2012 11:25
  • So i have now this code:

    		param(
            	[Parameter(Mandatory=$true)][string]$networkinterface,
    			[Parameter(Mandatory=$true)][string]$ip,
    			[Parameter(Mandatory=$true)][string]$mask,
    			[Parameter(Mandatory=$true)][string]$gateway,
    			[Parameter(Mandatory=$true)][string]$dns1,
    			[Parameter(Mandatory=$false)][string]$dns2
            )
         #Start writing code here
         $index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $networkinterface}).InterfaceIndex
         $NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}
      
         $dns = $dns1
         if($dns2){
            $NetInterface.SetDNSServerSearchOrder($dns1,$dns2)
         } else{
            $NetInterface.SetDNSServerSearchOrder($dns1)
         }
    
         $NetInterface.EnableStatic($ip, $mask)
         $NetInterface.SetGateways($gateway)

    And dnsservers are not set up. It's terrible.

    giovedì 28 giugno 2012 12:46
  • any error code?

    how do you invoke this script? can You give an example?

    giovedì 28 giugno 2012 13:38
  • here is output:

    PS C:\Users\dvorak.ruda\Desktop> .\SETIPBYMAC.ps1 -networkinterface "LAN" -ip "192.168.0.2" -mask "255.255.255.0" -gateway "192.168.0.1" -dns1 "8.8.8.8" -dns2 "8.8.4.4"
    Cannot find an overload for "SetDNSServerSearchOrder" and the argument count: "2".
    At C:\Users\dvorak.ruda\Desktop\SETIPBYMAC.ps1:15 char:46
    +         $NetInterface.SetDNSServerSearchOrder <<<< ($dns1,$dns2)
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
     
    
    
    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     : 
    __DYNASTY        : __PARAMETERS
    __RELPATH        : 
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         : 
    __NAMESPACE      : 
    __PATH           : 
    ReturnValue      : 0
    
    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     : 
    __DYNASTY        : __PARAMETERS
    __RELPATH        : 
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         : 
    __NAMESPACE      : 
    __PATH           : 
    ReturnValue      : 0

    giovedì 28 giugno 2012 13:46
  • Hi,
    Try that:

    param(
            [Parameter(Mandatory=$true)][string]$networkinterface,
      [Parameter(Mandatory=$true)][string]$ip,
      [Parameter(Mandatory=$true)][string]$mask,
      [Parameter(Mandatory=$true)][string]$gateway,
      [Parameter(Mandatory=$true)][string[]]$dns
    )
    
    #Start writing code here
    $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)

    and run it like that:

    .\SETIPBYMAC.ps1 -networkinterface "LAN" -ip "192.168.0.2" -mask "255.255.255.0" -gateway "192.168.0.1" -dns "8.8.8.8","8.8.4.4"

    Edit.
    Sorry for late comment, but earlier I was hurry so I put same code.

    Your error is that you try put two arguments to SetDNSServerSearchOrder method but it requires only one. Try put there one object like array of string for example directly as a script parameter (like in example), or if you need your version then run method like that $($dns1,$dns2) - this bind it to one object.

    $NetInterface.SetDNSServerSearchOrder($($dns1,$dns2))
    

    giovedì 28 giugno 2012 14:12
  • Thank you for help, but i am idiot i think. But i tried your solutions and it is still not working.
    domenica 1 luglio 2012 12:33