用于列出/ 24子网的所有服务器的脚本

时间:2016-01-17 22:48:57

标签: powershell-v3.0

我正在尝试编写一个脚本,它将为我提供一个列出服务器名称和子网掩码的.csv,但仅限于子网掩码等于255.255.255.0(/ 24)。理想情况下,我希望以电子表格格式提供此功能,其中服务器位于一列中,而SM位于下一列中。我想我今天已经在这个问题上工作了太长时间,而且我正在使用" scripter"。如果我能回答任何其他问题,请告诉我,这是我到目前为止的代码。我不是在找人自动为我解决这个问题!

我正在努力学习,我只是陷入困境,不知道为什么剧本是"挂"在某一点上,当它遇到它将查询网络适配器(Get-WmiObject)的部分时,它似乎挂起。感谢您帮助我指出正确的方向。

<#
  .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

1 个答案:

答案 0 :(得分:1)

如果Get-WmiObject命令挂起/失败,请隔离失败的服务器。你能在循环中立即添加Write-Verbose吗?只是为了看看循环在服务器列表中走了多远,以及每次都挂在同一个服务器上。

该功能还存在一些支架问题:

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

应该是:

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

在本节中,在else之前缺少一个大括号,在函数末尾有一个额外的}

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

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