How to inventory DCs Network Configuration via Powershell

I created a powershell script to generate a Report as HTML page (to publish it on sharepoint) with the IP and DNS configuration for all the forest DCs.

$MyForest = "rootdomain.local"
$AllServers=(Get-ADForest $MyForest).Domains | %{Get-ADDomainController-Filter *-Server $_}
$output=@()
$RunDate = Get-DateForEach ($Server in $AllServers){ 
    $Result=Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'"-Property @("IPADDRESS","DNSServerSearchOrder")-ComputerName $Server.Name -ErrorActionSilentlyContinue 
    $props=@{ComputerName=$Server.NameIPAddress=$Result.IPAddressDNSServerSearchOrder=$Result.DNSServerSearchOrder
    }

    $output += new-objectPSObject-Property $props
}
$b = $output | Select-Object ComputerName, @{Name="IP";Expression={$ip="";Foreach($ipDN in $_.IPAddress){$ip += "$ipDN; "};$ip}}, @{Name="DNSClientSetting";Expression={$dns="";Foreach($dnsDN in $_.DNSServerSearchOrder){$dns += "$dnsDN; "};$dns}}
$outfile = $b |ConvertTo-HTML-head $a-body "</pre>
<h2>DCs DNSClient Settings - Inventory</h2>
This page was generated by a Script at $RunDate
<pre>" -PreContent " " -PostContent " "

$outfile | Out-File C:\Data\Working\Production_Environment_DCsDNSClientSetting.html

Remember to change the $MyForest variable with the domain name of your AD forest and the output path on the last script line.

If you want to publish it to Sharepoint, here the code:

function Publish-File {
	param (
		[parameter( Mandatory = $true, HelpMessage="URL pointing to a SharePoint document library (omit the '/forms/default.aspx' portion)." )]
		[System.Uri]$Url,
		[parameter( Mandatory = $true, ValueFromPipeline = $true, HelpMessage="One or more files to publish. Use 'dir' to produce correct object type." )]
		[System.IO.FileInfo[]]$FileName,
		[system.Management.Automation.PSCredential]$Credential
	)
	$wc = new-object System.Net.WebClient
	if ( $Credential ) { $wc.Credentials = $Credential }
	else { $wc.UseDefaultCredentials = $true }
	$FileName | ForEach-Object {
		$DestUrl = "{0}{1}{2}" -f $Url.ToString().TrimEnd("/"), "/", $_.Name
		Write-Verbose "$( get-date -f s ): Uploading file: $_"
		$wc.UploadFile( $DestUrl , "PUT", $_.FullName )
		Write-Verbose "$( get-date -f s ): Upload completed"
	}
	
}

Dir C:\Data\Working\Production_Environment_DCsDNSClientSetting.html | Publish-File -Url "https://teams01.mysharepoint.local/000001/Wiki/Pages/"
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s