PowerShell: get network settings and export to csv

Ответить
M.K
Новичок
Сообщения: 9
Зарегистрирован: 04 дек 2014 16:52

PowerShell: get network settings and export to csv

Сообщение M.K » 28 апр 2016 06:31

# скрипт по перечню серверов из файла "servers_list.txt", лежащему в "C:\Program Files (x86)\Windows_Logs\link_speed", проходит и вытаскивает сетевые настройки в файл "C:\Program Files (x86)\Windows_Logs\link_speed\Adapter_configuration.csv

C:\Program Files (x86)\Windows_Logs\link_speed - папка должна быть создана

Код: Выделить всё

$path = "C:\Program Files (x86)\Windows_Logs\link_speed"
$Adapter_configuration  = "servers_list.txt"


$table = @()
$timestamp = Get-Date -UFormat %d%m%y
$hostnames = Get-Content $path\$Adapter_configuration |? {Test-Connection $_ -Quiet -Count 1}

$report_table = New-Object system.Data.DataTable
                
      $DeviceID = New-Object system.Data.DataColumn "DeviceID",([string])
	  $AdapterName = New-Object system.Data.DataColumn "Name",([string])
      $Adapter = New-Object system.Data.DataColumn "Adapter",([string])
	  $DNS = New-Object system.Data.DataColumn "Dns",([string])
	  $MAC = New-Object system.Data.DataColumn "MAC",([string])
      $IP_Address= New-Object system.Data.DataColumn "IP_Address",([string])
	  $Subnet_Mask= New-Object system.Data.DataColumn "Subnet_Mask",([string])
	  $Default_Gateway = New-Object system.Data.DataColumn "Default_Gateway",([string])
	  $DNS_Domain = New-Object system.Data.DataColumn "DNS_Domain",([string])
	  $DNS_Server= New-Object system.Data.DataColumn "DNS_Server",([string])
	  $WINS_primary= New-Object system.Data.DataColumn "WINS_Primary",([string])
	  $WINS_secondary= New-Object system.Data.DataColumn "WINS_Secondary",([string])
        
        
        # Add Columns to Table
        $report_table.columns.add($DeviceID)
        $report_table.columns.add($AdapterName)
        $report_table.columns.add($Adapter)
        $report_table.columns.add($DNS)
        $report_table.columns.add($MAC)
        $report_table.columns.add($IP_Address)
        $report_table.columns.add($Subnet_Mask)
        $report_table.columns.add($Default_Gateway)
        $report_table.columns.add($DNS_Domain)
        $report_table.columns.add($DNS_Server)
        $report_table.columns.add($WINS_primary)
        $report_table.columns.add($WINS_secondary)


foreach ($hostname in $hostnames)
 {
 
    $netAdapters = Get-wmiobject -cl "Win32_NetworkAdapter" -ComputerName $hostname -filter "AdapterType = 'Ethernet 802.3' AND NetConnectionStatus = 2" #> 0"
        
     
    
    if( $netAdapters )
			{
			ForEach( $adapter in $netAdapters )
				{
				$adpConf = @($adapter.GetRelated('Win32_NetworkAdapterConfiguration'))[0]
				$DeviceID = $adapter.DeviceID
				$Manufacturer = $adapter.Manufacturer
				$Name = $adapter.Name
				$Adapter = $adapter.AdapterType
				$Service = $adapter.ServiceName
				$DNS = $adpConf.DNSHostName
				$MAC = $adpConf.MACAddress
				
				if( $adpConf.IPEnabled )
					   
					{
						$IP_Address =$adpConf.IPAddress
						$Subnet_Mask = $adpConf.IPSubnet
						$Default_Gateway = $adpConf.DefaultIPGateway
						$DNS_Domain = $adpConf.DNSDomain
						$DNS_Server = $adpConf.DNSServerSearchOrder
						$WINS_primary = $adpConf.WINSPrimaryServer
						$WINS_secondary = $adpConf.WINSSecondaryServer
					}
			  $row = $REport_table.NewRow()
			  $row.DeviceID = $DeviceID 
			  $row.Name = $Name 
			  $row.Adapter    = $Adapter 
			  $row.DNS        = 	$DNS 
			  $row.MAC = 	$MAC 
			  $row.IP_Address = [string]::join(";",($IP_Address))
			  $row.Subnet_Mask = 	[string]::join(";",($Subnet_Mask))
			  $row.Default_Gateway = [string]::join(";",($Default_Gateway)) 
			  $row.DNS_Domain = [string]::join(";",($DNS_Domain ))
			  $row.DNS_Server = [string]::join(";",($DNS_Server))
			  $row.WINS_primary = $WINS_primary
			  $row.WINS_secondary = $WINS_secondary
		  $REport_table.Rows.Add($row)
				}
			}
 }
 $REport_table | Export-Csv $path\Adapter_configuration.csv -NoTypeInformation  -Delimiter ";"    

Ответить

Вернуться в «Powershell»