Skip to content

Instantly share code, notes, and snippets.

@rismoney
Created January 21, 2015 18:26
Show Gist options
  • Save rismoney/dd7b2fba161425346fd9 to your computer and use it in GitHub Desktop.
Save rismoney/dd7b2fba161425346fd9 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Script to dump nic configuration to YAML
.PARAMETER Servername
.EXAMPLE
1. get-nic.ps1 servername
2. import-csv .\svr-ou.csv | Where-Object {$_.name -like "I*"} | select name | foreach {get-nic -computer $_.name}
.NOTES
AUTHOR: Rich Siegel
LASTEDIT: 7/27/2012
Covered by the Beerware License
Requires failoverclusters Module
#>
function Get-Nic {
[cmdletBinding()]
param(
[parameter(ValueFromPipeline=$true)]
[string[]]$ComputerName)
process {
$filename=".\nic.yml"
$defSubnetmask="255.255.255.0"
$defNetbios="enabled"
$defDNSRegister="True"
$defFullDNSRegister="False"
# this was a copy paste from an external module
function Get-MyModule {
[CmdletBinding(DefaultParametersetName="isLoaded")]
Param([parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
[string] $module,
[parameter(Mandatory=$false,ParameterSetName="isLoaded")]
[alias("l")]
[switch] $isLoaded,
[parameter(Mandatory=$false, ParameterSetName="isAvailable")]
[alias("a")]
[switch] $isAvailable)
$moduleLoaded = Get-Module -name $module
$moduleAvailable = Get-Module -ListAvailable | Where-Object { $_.Name -eq $module }
return (($moduleLoaded -and $moduleAvailable) -or ($isAvailable -and $moduleAvailable))
}
if (-not(Get-MyModule("failoverclusters") -isLoaded)) {
if (-not (Get-MyModule("failoverclusters") -isAvailable)) {
Write-Host "Please install (failoverclusters)"
}
else {
Import-Module failoverclusters
$clustered = $True
}
}
foreach ($hostn in $computerName) {
out-file $filename -inputobject "$hostn`:" -encoding default -append
if ($clustered) {
$clustervips=get-cluster $hostn |Get-ClusterResource |where-object {$_.ResourceType -match "IP Address"} |Get-ClusterParameter -Name Address |select value
}
# vip finder for future reference
# foreach ($vip in $clustervips) {
# write-host "vip1" $vip.value
# }
try
{
$NICs = Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE" -ComputerName $hostn -ErrorAction Stop
}
catch {continue}
#loop over each adapter
if ($NICs) {
foreach ($NIC in $NICs) {
Write-Verbose "Processing NIC $($NIC.Description)"
$NetConnectionId = (Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapter WHERE Index = $($NIC.Index)" -ComputerName $hostn -ErrorAction Stop).NetConnectionId
$MACAddress =$NIC.MACAddress
$MACAddress = $MACAddress.replace(':','')
$IPAddress = $NIC.IPAddress
$Description = $NIC.Description
$DefaultIPGateway =$NIC.DefaultIPGateway
$IPSubnet =$NIC.IPSubnet
$Netbiosoption =$NIC.TcpipNetbiosOptions
#convert netbios 0,1,2 into english
switch ($netbiosoption) {
0 {$netbios="dhcp"}
1 {$netbios="enabled"}
2 {$netbios="disabled"}
}
$DNSReg=$NIC.DomainDNSRegistrationEnabled
$FullDNSReg=$NIC.FullDNSRegistrationEnabled
#omit entry for failover cluster virtual adapter
if ($Description -ne "Microsoft Failover Cluster Virtual Adapter") {
out-file $filename -inputobject " $netconnectionid`:" -encoding default -append
out-file $filename -inputobject " description: `"$description`"" -encoding default -append
$i=0
foreach ($ip in $ipaddress) {
#loop through each ip address and lookup clustervips array to see if match. if not a vip output to yaml
try {
$index = 0..($clustervips.Count - 1) | Where {$clustervips[$_].value -eq $ip}
}
catch {
"missing failovercluster"
}
if (!$index) {
#out-file $filename -inputobject " <<: *defaults" -encoding default -append
out-file $filename -inputobject " ipaddress: $ip" -encoding default -append
#if ($($ipsubnet[$i-1]) -ne $defSubnetmask) {
out-file $filename -inputobject " subnetmask: $($ipsubnet[$i-1])" -encoding default -append
#}
if ($i -ge 0) {
if ($defaultipgateway) {
out-file $filename -inputobject " defaultgateway: $defaultipgateway" -encoding default -append
}
}
}
$i++
}
#if ($netbios -ne $defNetbios) {
out-file $filename -inputobject " netbios: $netbios" -encoding default -append
#}
#if ($dnsreg -ne $defDNSRegister) {
out-file $filename -inputobject " dnsregister: $dnsreg" -encoding default -append
#}
#if ($fulldnsreg -ne $defFullDNSRegister) {
out-file $filename -inputobject " fulldnsregister: $fulldnsreg" -encoding default -append
#}
out-file $filename -inputobject " macaddress: $MACAddress" -encoding default -append
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment