Categories

TCP/IP Alternative Configurations: pt III set the alternative configuration

 

We have seen how to set the NIC to use DHCP to get its address. This post shows how to set the alternative configuration on the NIC. If you just  want APIPA then do nothing – other wise use this script

$HKLM = 2147483650 #HKEY_LOCAL_MACHINE            
            
$index = 7            
$nic = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index=$index"            
            
$key = "SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\$($nic.SettingID)"            
            
Invoke-WmiMethod -Class StdRegprov -Name SetDWORDvalue -ArgumentList $hklm, $key, "AddressType", 2            
            
Invoke-WmiMethod -Class StdRegprov -Name SetMULTISTRINGvalue -ArgumentList $hklm, $key, "Alternate_$($nic.SettingID)", "ActiveConfigurations"            
Invoke-WmiMethod -Class StdRegprov -Name SetSTRINGvalue -ArgumentList $hklm, $key, "10.10.54.202", "DhcpIpAddress"            
Invoke-WmiMethod -Class StdRegprov -Name SetSTRINGvalue -ArgumentList $hklm, $key, "10.10.54.201", "DhcpNameServer"            
Invoke-WmiMethod -Class StdRegprov -Name SetSTRINGvalue -ArgumentList $hklm, $key, "255.255.255.0", "DhcpSubnetMask"


Again I’m cheating by defining the NIC in terms of its Index number



The registry key is derived from the SettingID property of the NIC



We then need to set a number of registry values. The AddressType sets the alternative configuration to use our informations rather than APIPA. The ActiveCinfigurations value is set using the SettingID property of the NIC



The address, subnetmask, and names server are set.



If you look carefully at the lines where we use the SetMULTISTRINGvalue and SetSTRINGvalue methods you will notice that we give the hive, key, value and then registry value name  whereas the SetDWORDvalue method we give hive, key, value name and then value.



This is a quirk of Invoke-WmiMethod



The WMI documentation for SetMULTISTRINGvalue and SetSTRINGvalue methods state the parameters should be:



  • hive
  • registry key
  • registry value name
  • value


This order is constant across the Set* methods of the StdRegProv class -  see http://msdn.microsoft.com/en-us/library/windows/desktop/aa393600(v=vs.85).aspx



If we do some investigation



PS> ([wmiclass]"StdRegprov").GetMethodParameters('SetSTRINGvalue')




__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 4
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
hDefKey          : 2147483650
sSubKeyName      :
sValue           : hello
sValueName       :



This clearly shows we need to give the value before the value name.



the same holds true if we investigate using Get-CimClass in PowerShell v3



Get-CimClass -ClassName StdRegProv |
select -ExpandProperty CimClassMethods |
where Name -eq "SetStringValue" |
select -ExpandProperty Parameters



produces



Name                                         CimType Qualifiers
----                                         ------- ----------
hDefKey                                      UInt32 {ID, IN}
sSubKeyName                                  String {ID, IN}
sValue                                       String {ID, in}
sValueName                                   String {ID, in}



if we use Invoke-CimMethod its not to much of a problem as we have to provide the value name and value pairs as a hash table we are not relying on argument order.



I’ll post an alternative listing using the CIM cmdlets another time

Leave a Reply