powershell win32_networkadapterconfiguration -filter WINSPrimaryServer?

If this is the incorrect place to post this question, I appologize in advance and if you could let me know where is a better place to post this question I would greatly apprecicate it.

The end goal here is to write script to change the WINS setting on like a 300 servers. Since there are so many and it will be an production enviroment, I want to include some fail safes. I've written one in Powershell that should work just fine if I could get the syntax right I would be good to go.

The issue that I'm running into is my "-filter". The idea is IF WINS isn't set I don't want to enable it or change/set it to the new WINS server IP. Below is the script I have written, you can just ingore all the comments if you want. I do it for two reason 1st to help me understand the code/syntax better and second incase I want to reuse the script later it helps me rememeber what it does.

# here is where I read from the list of servers, this makes the entire list a variable
$Server_List = Get-Content C:\Scripts\PS-Scripts\WINS\Test.txt
foreach ($Server in $Server_List) # here is where I break the list into individual variables 
{  
# Since there could be more than one NIC I'm going to do the same for each NIC on the server
# I'm going to put in a filter as well to make sure the NIC is enabled, DHCP is disabled, and WINS is enabled"
# My list shouldn't include any servers with this but I'm going to include this filter for a failsafe, just in case. 
$Every_NIC_on_Server = Get-wmiObject Win32_networkadapterconfiguration -computername $Server -Filter "IPEnabled=TRUE and DHCPEnabled=FALSE and WINSPrimaryServer='0.0.0.0' and WINSSecondaryServer='0.0.0.0'"
$Every_NIC_on_Server = Get-wmiObject Win32_networkadapterconfiguration -computername $Server -Filter "IPEnabled=TRUE and DHCPEnabled=FALSE and WINSEnableLMHostsLookup=TRUE and WINSPrimaryServer='' and WINSSecondaryServer=''" 
$Every_NIC_on_Server = Get-wmiObject Win32_networkadapterconfiguration -computername $Server -Filter "IPEnabled=TRUE and DHCPEnabled=FALSE and WINSEnableLMHostsLookup=TRUE and WINSPrimaryServer=$null and WINSSecondaryServer=$null"
 # http://social.technet.microsoft.com/Forums/en-US/6dc7f2b3-a906-4556-844b-9ac030ee6f24/remove-wins-entries-via-powershell?forum=winserverpowershell
 foreach ($Individual_NIC in $Every_NIC_on_Server) 
        {
        # http://www.powershellpro.com/powershell-tutorial-introduction/powershell-wmi-methods/
        $Individual_NIC.SetWINSServer("123.123.123.123","123.123.123.123")
        }
}

Thanks in adavance for any and all help.

January 29th, 2014 4:19pm

You should be using DHCP to set all of this.

Why would you want to enable WINS it is pretty much obsolete except if you are running certain aspects of WordPerfect or Perfect Office which is also obsolete.

You are trying to get adapters but your filter syntax is bad.

It is not really possible to discover what you are trying to do.

Here is the simpleay to check all adapters on a server remotely and quickly

Get-wmiObject Win32_networkadapterconfiguration -computername $Server -Filter 'IPEnabled=TRUE and DHCPEnabled=FALSE' |
     ForEach-Object{
          if($_.WINSPrimaryServer){
              $_.SetWINSServer('123.123.123.123','123.123.123.123')
          }
     }

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2014 6:08pm

jrv,

Thanks for the reply. Well the reason I'm even trying to set the WINS server settings is because we are retiring WINS. I just have to move our current servers that have this WINS set to a new WINS server before WINS can be retired all together. I realized that the Filter I had in place was a problem, hence the reason for the post. Now I could be wrong but won't your script example just populate the a primary and secondary WINS server? If WINS isn't set on the server already I don't want to set it with my script. That is also the reason I'm trying to figure out what the correct syntax is for filtering out if there are no WINS setting

As far as just discoverying what the current enviroment is I'm pretty good on that part. Below is the script to used for that.

Thanks in Adavance




########################################################################  
# http://powershell.com/cs/media/p/4998.aspx
# Objective: Retreive Network Adapter settings for Windows Host Machines  
########################################################################  
# here is where I read from the list of servers I want, this makes the enitre list a variable
$Server_List = Get-Content C:\Scripts\PS-Scripts\WINS\WINS-Server-List.txt
  
# Opens Excel and creates an spreadsheet as well as formats the Infomation
$Excel = New-Object -Com Excel.Application  
# When this script is run Excel will open up and begin to populate with the identified information
$Excel.visible = $True  
$Excel = $Excel.Workbooks.Add()  
# here is the list of information I want its it starts on the 1st worksheet because of "$Sheet = $Excel.WorkSheets.Item(1)"
# It start to populate information in Row 1 and Column 1 because of $Sheet.Cells.Item(1,1)
$Sheet = $Excel.WorkSheets.Item(1)  
$Sheet.Cells.Item(1,1) = Server Name  
$Sheet.Cells.Item(1,2) = DNS Name  
$Sheet.Cells.Item(1,3) = DNS Domain  
$Sheet.Cells.Item(1,4) = IP Address v4 & v6  
$Sheet.Cells.Item(1,5) = Subnet Mask  
$Sheet.Cells.Item(1,6) = Default Gateway  
$Sheet.Cells.Item(1,7) = MAC Address 
$Sheet.Cells.Item(1,8) = "Description"
$Sheet.Cells.Item(1,9) = DNS Domain Suffix Available  
$Sheet.Cells.Item(1,10) = "DHCPEnabled"
$Sheet.Cells.Item(1,11) = DHCP Server  
$Sheet.Cells.Item(1,12) = WINS Server Primary  
$Sheet.Cells.Item(1,13) = WINS Server Secondary  
$Sheet.Cells.Item(1,14) = DNS Servers  
$Sheet.Cells.Item(1,15) = WINS Enable LMHosts Lookup"
$Sheet.Cells.Item(1,16) = WINS Host Lookup File"
$Sheet.Cells.Item(1,17) = Tcpip Netbios Options"
$Sheet.Cells.Item(1,18) = "DNS Enabled For WINS Resolution"
# This is part of the script is just for formatting and to make the report
# This is just the heading on the report make bold, black font, gray cell etc
# http://dmcritchie.mvps.org/excel/colors.htm
$WorkBook = $Sheet.UsedRange  
$WorkBook.Interior.ColorIndex = 15 
$WorkBook.Font.ColorIndex = 1  
$WorkBook.Font.Bold = $True  
# Now that have all the headings for my script I'm going to move to row 2 to populate it with the information I want
$intRow = 2  
  
  
foreach ($Server in $Server_List) # here is where I break the list into individual variables 
# For each server in the list I'm going to gather the following information and populate the excel spreadsheet with that information
{  
# Since there could be more that one NIC I'm going to do the same for each NIC in the server list
$NIC_Info = Get-wmiObject Win32_networkadapterconfiguration -computername $Server | Where-Object{$_.IPENabled -eq "True"}  
# the only filter I'm going to include for now is if the NIC is enabled
# Here are some examples that I could use
# -Filter "IPEnabled=TRUE and DHCPEnabled=FALSE"
#   $QueryString = Gwmi Win32_NetworkAdapterConfiguration -Comp $Server
#   (($QueryString.TcpipNetbiosOptions -eq 0) -or ($QueryString.WINSEnableLMHostsLookup -eq "True") -or ($QueryString.WINSPrimaryServer) -or ($QueryString.WINSSecondaryServer))
    foreach ($objItem_From_NIC_Class in $NIC_Info) 
    {  #Here where I'm actually pulling the information on the specific object from the WMI NIC Class 
    $Sheet.Cells.Item($intRow,1) = $Server  
    $Sheet.Cells.Item($intRow,2) = $objItem_From_NIC_Class.DNSHostName
    $Sheet.Cells.Item($intRow,3) = $objItem_From_NIC_Class.DNSDomain
    $Sheet.Cells.Item($intRow,4) = $objItem_From_NIC_Class.IPAddress
    $Sheet.Cells.Item($intRow,5) = $objItem_From_NIC_Class.IPSubnet
    $Sheet.Cells.Item($intRow,6) = $objItem_From_NIC_Class.DefaultIPGateway
    $Sheet.Cells.Item($intRow,7) = $objItem_From_NIC_Class.MACAddress
    $Sheet.Cells.Item($intRow,8) = $objItem_From_NIC_Class.Description
    $Sheet.Cells.Item($intRow,9) = $objItem_From_NIC_Class.DNSDomainSuffixSearchOrder
    $Sheet.Cells.Item($intRow,10) = $objItem_From_NIC_Class.DHCPEnabled
    $Sheet.Cells.Item($intRow,11) = $objItem_From_NIC_Class.DHCPServer 
    $Sheet.Cells.Item($intRow,12) = $objItem_From_NIC_Class.WINSPrimaryServer  
    $Sheet.Cells.Item($intRow,13) = $objItem_From_NIC_Class.WINSSecondaryServer 
    $Sheet.Cells.Item($intRow,14) = $objItem_From_NIC_Class.DNSServerSearchOrder
    $Sheet.Cells.Item($intRow,15) = $objItem_From_NIC_Class.WINSEnableLMHostsLookup
    $Sheet.Cells.Item($intRow,16) = $objItem_From_NIC_Class.WINSHostLookupFile
    $Sheet.Cells.Item($intRow,17) = $objItem_From_NIC_Class.TcpipNetbiosOptions
    $Sheet.Cells.Item($intRow,18) = $objItem_From_NIC_Class.DNSEnabledForWINSResolution
 
    $intRow = $intRow + 1 # After I collect the information I want for each NIC on the server I'm just going to the next line with this code
    }  
}  
$WorkBook.EntireColumn.AutoFit() # I'm just making the information that I collected and put in Excel is fitted properly into the speadsheet

January 29th, 2014 6:30pm

If you don't have a current WINS set why set a WINS. WINS is unnecessary. DNS resolves everything in a DNS domain. WINS is only needed if you still have Windows 98 or earlier.

If you only want too set servers that are not set just reverse the logic.

if(-not ($_.WINSPrimaryServer) ){

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2014 7:07pm

#This creates a spreadsheet in one line.
Get-wmiObject Win32_networkadapterconfiguration -computername $Server -filter 'IPENabled=True'
     Select properties list to select |
     Export-Csv filename.csv

January 29th, 2014 7:10pm

Jrv,

Again thanks for the reply. I apparently I'm not being clear. I'm not trying to discuss the why DNS is better than WINS or why you would or wouldn't need WINS when DNS is an option. The point is to build in a fail safe to the script "in case" WINS is not set. However an IF statement might be a better approach

# here is where I read from the list of servers, this makes the entire list a variable
$Server_List = Get-Content C:\Scripts\PS-Scripts\WINS\Test.txt
foreach ($Server in $Server_List) # here is where I break the list into individual variables 
{  
# Since there could be more than one NIC I'm going to do the same for each NIC on the server
 
$Every_NIC_on_Server = Get-wmiObject Win32_networkadapterconfiguration -computername $Server
# My list shouldn't include any servers with this but I'm going to include this for a failsafe, just in case.
# My fail safe here make sure that the NIC is enabled, DHCP is disabled, and nothing is set for WINS"
	if($Every_NIC_on_Server = "WINSPrimaryServer -ne $null" -or "WINSSecondaryServer -ne $null" -and "IPEnabled=TRUE" -and "DHCPEnabled=FALSE")
	{	
 	# http://social.technet.microsoft.com/Forums/en-US/6dc7f2b3-a906-4556-844b-9ac030ee6f24/remove-wins-entries-via-powershell?forum=winserverpowershell
 	foreach ($Individual_NIC in $Every_NIC_on_Server) 
        {
        # http://www.powershellpro.com/powershell-tutorial-introduction/powershell-wmi-methods/
        $Individual_NIC.SetWINSServer("134.133.32.11","134.133.96.59")
        }
	}	
}


<#
How ever when I run I get the following error:
Method invocation failed because [System.Boolean] doesn't contain a method named 'SetWINSServer'.
At C:\Users\N67570\AppData\Local\Temp\3704f628-4e95-4a3d-8a60-52aca04dd8f2.ps1:19 char:9
+         $Individual_NIC.SetWINSServer("134.133.32.11","134.133.96.59")
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
#>

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2014 7:26pm

I guess you do not understand what I am doing.

This is all you need:

$servers=<get all servers as an array from AD or a file>

$servers=<get all servers as an array from AD or a file>
Get-wmiObject Win32_networkadapterconfiguration -computername $Servers -Filter 'IPEnabled=TRUE and DHCPEnabled=FALSE' |
     ForEach-Object{
          if(-not ($_.WINSPrimaryServer)){
              $_.SetWINSServer('123.123.123.123','123.123.123.123')
          }
     }

January 29th, 2014 7:45pm

You could wrap a try/catch block and report on any failures.

There is no foolproof way to do this. This way is the fastest and it will only set the WINS when it is not set already.

You do not need to create those long funny named variables.  They help nothing.  To a technician or programmer what I have written is completely understandable and gets ALL nics that are IPEnabled and not managed by DHCP.  Thisis what you asked for. Adding the filter for a WINS may not work as expected andis not really necessary.

You have not made it clear why you are interested inWINSLMHostsLookup. This should never be used in a domain but it doesn't seennecessary to filter on it.

Part o the issue seems to be that you are not clearly stating your needs.  Forget "how" and describe the intended outcome including initial dependencies without using code or technical language.  This is always where we start.  We choose the technical solution only after we have a clear, non-technical description.

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2014 7:52pm

jrv,

Thanks for the reply. Well the reason I'm even trying to set the WINS server settings is because we are retiring WINS. I just have to move our current servers that have this WINS set to a new WINS server before WINS can be retired all together. I realized that the Filter I had in place was a problem, hence the reason for the post. Now I could be wrong but won't your script example just populate the a primary and secondary WINS server? If WINS isn't set on the server already I don't want to set it with my script. That is also the reason I'm trying to figure out what the correct syntax is for filtering out if there are no WINS setting

As far as just discoverying what the current enviroment is I'm pretty good on that part. Below is the script to used for that.

Thanks in Adavance




########################################################################  
# http://powershell.com/cs/media/p/4998.aspx
# Objective: Retreive Network Adapter settings for Windows Host Machines  
########################################################################  
# here is where I read from the list of servers I want, this makes the enitre list a variable
$Server_List = Get-Content C:\Scripts\PS-Scripts\WINS\WINS-Server-List.txt
  
# Opens Excel and creates an spreadsheet as well as formats the Infomation
$Excel = New-Object -Com Excel.Application  
# When this script is run Excel will open up and begin to populate with the identified information
$Excel.visible = $True  
$Excel = $Excel.Workbooks.Add()  
# here is the list of information I want its it starts on the 1st worksheet because of "$Sheet = $Excel.WorkSheets.Item(1)"
# It start to populate information in Row 1 and Column 1 because of $Sheet.Cells.Item(1,1)
$Sheet = $Excel.WorkSheets.Item(1)  
$Sheet.Cells.Item(1,1) = Server Name  
$Sheet.Cells.Item(1,2) = DNS Name  
$Sheet.Cells.Item(1,3) = DNS Domain  
$Sheet.Cells.Item(1,4) = IP Address v4 & v6  
$Sheet.Cells.Item(1,5) = Subnet Mask  
$Sheet.Cells.Item(1,6) = Default Gateway  
$Sheet.Cells.Item(1,7) = MAC Address 
$Sheet.Cells.Item(1,8) = "Description"
$Sheet.Cells.Item(1,9) = DNS Domain Suffix Available  
$Sheet.Cells.Item(1,10) = "DHCPEnabled"
$Sheet.Cells.Item(1,11) = DHCP Server  
$Sheet.Cells.Item(1,12) = WINS Server Primary  
$Sheet.Cells.Item(1,13) = WINS Server Secondary  
$Sheet.Cells.Item(1,14) = DNS Servers  
$Sheet.Cells.Item(1,15) = WINS Enable LMHosts Lookup"
$Sheet.Cells.Item(1,16) = WINS Host Lookup File"
$Sheet.Cells.Item(1,17) = Tcpip Netbios Options"
$Sheet.Cells.Item(1,18) = "DNS Enabled For WINS Resolution"
# This is part of the script is just for formatting and to make the report
# This is just the heading on the report make bold, black font, gray cell etc
# http://dmcritchie.mvps.org/excel/colors.htm
$WorkBook = $Sheet.UsedRange  
$WorkBook.Interior.ColorIndex = 15 
$WorkBook.Font.ColorIndex = 1  
$WorkBook.Font.Bold = $True  
# Now that have all the headings for my script I'm going to move to row 2 to populate it with the information I want
$intRow = 2  
  
  
foreach ($Server in $Server_List) # here is where I break the list into individual variables 
# For each server in the list I'm going to gather the following information and populate the excel spreadsheet with that information
{  
# Since there could be more that one NIC I'm going to do the same for each NIC in the server list
$NIC_Info = Get-wmiObject Win32_networkadapterconfiguration -computername $Server | Where-Object{$_.IPENabled -eq "True"}  
# the only filter I'm going to include for now is if the NIC is enabled
# Here are some examples that I could use
# -Filter "IPEnabled=TRUE and DHCPEnabled=FALSE"
#   $QueryString = Gwmi Win32_NetworkAdapterConfiguration -Comp $Server
#   (($QueryString.TcpipNetbiosOptions -eq 0) -or ($QueryString.WINSEnableLMHostsLookup -eq "True") -or ($QueryString.WINSPrimaryServer) -or ($QueryString.WINSSecondaryServer))
    foreach ($objItem_From_NIC_Class in $NIC_Info) 
    {  #Here where I'm actually pulling the information on the specific object from the WMI NIC Class 
    $Sheet.Cells.Item($intRow,1) = $Server  
    $Sheet.Cells.Item($intRow,2) = $objItem_From_NIC_Class.DNSHostName
    $Sheet.Cells.Item($intRow,3) = $objItem_From_NIC_Class.DNSDomain
    $Sheet.Cells.Item($intRow,4) = $objItem_From_NIC_Class.IPAddress
    $Sheet.Cells.Item($intRow,5) = $objItem_From_NIC_Class.IPSubnet
    $Sheet.Cells.Item($intRow,6) = $objItem_From_NIC_Class.DefaultIPGateway
    $Sheet.Cells.Item($intRow,7) = $objItem_From_NIC_Class.MACAddress
    $Sheet.Cells.Item($intRow,8) = $objItem_From_NIC_Class.Description
    $Sheet.Cells.Item($intRow,9) = $objItem_From_NIC_Class.DNSDomainSuffixSearchOrder
    $Sheet.Cells.Item($intRow,10) = $objItem_From_NIC_Class.DHCPEnabled
    $Sheet.Cells.Item($intRow,11) = $objItem_From_NIC_Class.DHCPServer 
    $Sheet.Cells.Item($intRow,12) = $objItem_From_NIC_Class.WINSPrimaryServer  
    $Sheet.Cells.Item($intRow,13) = $objItem_From_NIC_Class.WINSSecondaryServer 
    $Sheet.Cells.Item($intRow,14) = $objItem_From_NIC_Class.DNSServerSearchOrder
    $Sheet.Cells.Item($intRow,15) = $objItem_From_NIC_Class.WINSEnableLMHostsLookup
    $Sheet.Cells.Item($intRow,16) = $objItem_From_NIC_Class.WINSHostLookupFile
    $Sheet.Cells.Item($intRow,17) = $objItem_From_NIC_Class.TcpipNetbiosOptions
    $Sheet.Cells.Item($intRow,18) = $objItem_From_NIC_Class.DNSEnabledForWINSResolution
 
    $intRow = $intRow + 1 # After I collect the information I want for each NIC on the server I'm just going to the next line with this code
    }  
}  
$WorkBook.EntireColumn.AutoFit() # I'm just making the information that I collected and put in Excel is fitted properly into the speadsheet

  • Edited by John-Barrett Wednesday, January 29, 2014 11:27 PM
January 30th, 2014 2:24am

jrv,

Thank you for the all your help on this I really appreciate it and I do appologize for not being clear. The reason I have all those long funny varibles is for me only. It just helps me understand and learn the code/syntax better along with all the comments I put in my scripts. Since you can accomplish the same result in many different ways its, I look at it as a matter of style. Now I'm not a programmer by any means, but I do enjoy writting scripts I just want to understand them and not just ask for a script or find one on-line some where and just run it without knowing exactly what it does.

In the orginal post the reason I had all those other filters including the WINSLMHostsLookup was I was just trying other option without much success. I kind of looked at it as a troubleshooting step, more than anything.

I did end up using your code so I'll give you the credit for answering this post. I did change it slightly and tested it out and it did perform how I wanted it to in the end. I do want to thank you for time/help with this. Here is the end script for incae someone in the furture has to do this hopefully it will save them some head scratching.

# here is where I read from the list of servers, this makes the entire list a variable
$servers = Get-Content C:\Scripts\PS-Scripts\WINS\Test.txt
Get-wmiObject Win32_networkadapterconfiguration -computername $servers -Filter 'IPEnabled=TRUE and DHCPEnabled=FALSE' | # This filter will make sure the NIC is enabled and DHCP is disabled
     ForEach-Object{ # here is where the list goes through individual objects (or servers)
          IF($_.WINSPrimaryServer -ne $null)
          { # My list shouldn't include any servers with the Primary WINS server set but as a fail-safe this IF statement will catch it if it set 
          $_.SetWINSServer("123.123.123.123","123.123.123.123") # If a server has meets all the requirements Here is where I change set the New WINS server address
          }
     } 

Free Windows Admin Tool Kit Click here and download it now
January 30th, 2014 12:13pm

Good.  I know you think using all of those variables helps you to learn but it really doesn't.  It only adds complexity and error to your script.

Abandon those crutches and rely on you brain and you memory to lead you.  Clean lean code is always a better place to start.  Write code in little pieces and get the little piece to work well. Be sure you understand in your head what is happening.

I have had this conversation with dozens of new programmers over the years.  They all finally realize that clean is mean.

The structure of your code layout is the best prompt as to what is happening.  White space helps to visually follow the code.  These are to backbone edicts of all programming in all languages.  The design of the PowerShell syntax was purposely chosen to aid this.

This line is more confusing to most experienced users and is unnecessary:

 IF($_.WINSPrimaryServer -ne $null)

This is the logical test for null.  Not null is just adding the -not to invert.

if($_.WINSPrimaryServer){  ...

becomes

if(-not ($_.WINSPrimaryServer)){  ...

PowerShell is also specifically desined to work this way.

if(0)
if('')
if(@())
etc..

All of these are tests for a specific state of the variable that we normally use.

This is also a much used test:

if($p=Get-Process -Name browser){ 

This both assigns and tests in one step.  We have been using that construct for 40 years.

The extra comments you have added are redundant and only serve to confuse.  The code says what it does because it is based on language.  It is for techs to view and a tech should look up what is happening if they do not understand the code.  Try to only use comments to show what is not already obvious.  Your following comment is identical to the code it follows.

-Filter 'IPEnabled=TRUE and DHCPEnabled=FALSE' |
 # This filter will make sure the NIC is enabled and DHCP is disabled
     ForEach-Object{ # here is where the list goes through individual objects (or

I think all techs can understand the code without the comment.

January 30th, 2014 12:25pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics