LilithGoddess
LilithGoddess

Reputation: 165

Script for listing all servers with /24 subnet

I am trying to write a script that will provide me with a .csv that will list the server name and subnet mask, but ONLY if the subnet mask is equal to 255.255.255.0 (/24). Ideally I would like to have this in a spreadsheet format with servers in one column and SMs in the next. I think I have just been working on this for too long today and am having "scripter's block". Please let me know if I can answer any other questions, here is the code I have so far. I am NOT looking for someone to automagically fix this for me!

I am trying to learn and I am just stuck and do not know why the script is "hanging" at a certain point, it seems to hang when it hits the part where it would be querying the network adapters (Get-WmiObject). Thank you for any assistance in just pointing me in the right direction.

<#
  .NOTES
  ===========================================================================
   Created on:     01/15/2016 8:06 PM
   Created by:
   Organization:
   Filename:       Get-PSPSubnetMask.ps1
  ===========================================================================
  .DESCRIPTION
#>

[CmdletBinding()]
param (
  [Parameter(Mandatory = $true)]
  [System.String]
  $CommandLineFilePath
)

function Get-PSPSubnetMask {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true)]
    [System.String]
    $FilePath
  )

  Write-Verbose "Retrieving server names from text file..."

  #Retrieve a list of PSP servers from text file and set to $serverNames
  $serverNames = Get-Content $FilePath

  Write-Verbose "Gathering network information..."

  #Iterate through each of the server names
  foreach ($serverName in $serverNames) {
    #Check if the server is online before doing the remote command
    if (Test-Connection -ComputerName $serverName -Quiet -count 1) {
      $ethernetAdapters = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "ipenabled = 'true' and description like 'Intel(R) Ethernet%'" -ComputerName $serverName;

      foreach ($ethernetAdapter in $ethernetAdapters) {
        $SubnetMask  = $ethernetAdapter.IPSubnet;

        if ($SubnetMask -eq "255.255.255.0") {
          New-Object -TypeName PSCustomObject -Property @{
            Server = $serverName
            SubnetMask = $SubnetMask |
              Export-Csv C:\Temp\PSPSubnetMask.csv -Append
          }

          Write-Verbose "$serverName has /24 configuration, logged"

        else {
            Write-Verbose "$serverName already has a /23 configuration"
        } #Else
      } #foreach ($ethernetAdapter in $ethernetAdapters) {
    } #If (Test-Connection -ComputerName $serverName -Quiet) {
  } #foreach ($serverName in $serverNames) {
} #function Get-PSPSubnetMask {
}

Write-Verbose "Script completed successfully"

#Run the Get-PSPSubnetMask function using the text file path that was passed to this script
Get-PSPSubnetMask -FilePath $CommandLineFilePath -Verbose

Upvotes: 1

Views: 357

Answers (1)

xXhRQ8sD2L7Z
xXhRQ8sD2L7Z

Reputation: 1716

If the Get-WmiObject command is hanging/failing, isolate the server which it fails on. Can you add a Write-Verbose immediately inside the loop? Just to see how far the loop gets through the list of servers and if it's hanging on the same one each time.

There are also some bracket problems in the function:

New-Object -TypeName PSCustomObject -Property @{
  Server = $serverName
  SubnetMask = $SubnetMask |
    Export-Csv C:\Temp\PSPSubnetMask.csv -Append
}

Should be:

New-Object -TypeName PSCustomObject -Property @{
  Server = $serverName
  SubnetMask = $SubnetMask
} | Export-Csv C:\Temp\PSPSubnetMask.csv -Append

In this section, there is a brace missing before else and an extra } at the end of the function:

   Write-Verbose "$serverName has /24 configuration, logged"

 else {
     Write-Verbose "$serverName already has a /23 configuration"
 } #Else

Upvotes: 1

Related Questions