Powershell main script

From Help
Jump to: navigation, search

This is the "working copy" for my "infrastructure discovery script"

Goals

1. Discover DNS server

2. Get DHCP info

3. Get the zones

4. Find the statics

5. Find the Domain Controllers

6. Find Window Servers in statics

7. Get hardware info off servers

8. Get Roles and Features off servers

Discover DNS server

Get-DnsServerSetting

Return to Top

Get DHCP info

Finding DHCP server: $a=Get-ADDomainController select DefaultPartition

Get-DhcpServerv4Scope

Return to Top

Get DHCP Zones

Get-DhcpServerv4OptionValue -ComputerName realm.tpfnd.cat -ScopeId 10.0.0.0 -All | select Name, optionid, type, value| Format-Table -Property * -AutoSize | Out-String -Width 4096

Return to Top

Get list of real DNS zones

Get-DnsServerZone | Where-Object {$_.ZoneType -eq 'Primary'} | Where-Object {$_.IsAutoCreated -eq $False} | Where-Object {$_.IsReverseLookupZone -eq $False} | Where-Object {$_.ZoneName -ne 'TrustAnchors'}

Return to Top

Find AD controllers

Get-ADDomainController

Return to Top

Current

x = Get-DnsServerSetting
$DNSSERVER = $x.ComputerName
write-host $DNSSERVER

$DNSIP = Get-DnsServerSetting | select -ExpandProperty AllIPAddress

$x = Get-DnsServerSetting
$DNSIP = $x.AllIPAddress
write-host $DNSIP

$DNSIP -match "\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b"
$Scope = ($matches[1..3] -join ".") + ".0"
write-host $Scope

$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;text-align: left;background-color: #4CAF50;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;width: 150px;}"
$a = $a + "</style>"

Get-DnsServerSetting | Select-Object ComputerName, AllIPAddress | ConvertTo-HTML -head $a -body "<H2>DNS Server Settings</H2>" | Out-File C:\PS\DNSServerSettings.htm

Invoke-Expression C:\PS\DNSServerSettings.htm

Get-DhcpServerv4Scope select-object ScopeID

Get-DhcpServerv4Scope -ComputerName $DNSSERVER -ScopeId $Scope

Return to Top

Static IP script

Version 1.0

param ( 
[string]$LDAPFilter = '(name=*)'
)
$wmiQuery = "select IPAddress, DefaultIPGateway from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE and DHCPEnabled=FALSE"

$computers = (Get-ADComputer -LDAPFilter $LDAPFilter)
foreach ($computer in $computers) { 

    $networkAdapters = (Get-WmiObject -ErrorAction SilentlyContinue -ComputerName $computer.DNSHostName -Query $wmiQuery) 
    foreach ($networkAdapter in $networkAdapters) { 
        foreach ($ip in $networkAdapter.IPAddress) 
        { 
            if ($ip -match "\.") 
            { 
                Write-Host $($computer.DNSHostName), $ip 
				$a = $($computer.DNSHostName)
				$staticlist = $($computer.DNSHostName)
				$a = $a + " " + $ip
				out-file "C:\PS\statics.txt" -inputobject $a -encoding ASCII -Append
				out-file "C:\PS\staticlist.txt" -inputobject $staticlist -encoding ASCII -Append
			} 
           }
		} 
}


Version 2

This also gets the OS. It makes three files:

  • C:\PS\statics.txt - a list of PC names and IPs
  • C:\PS\staticlist.txt - a list of ONLY PC names
  • C:\PS\list.cvs - a comma delimited list of PC names, IPs, and OS
# Clean up and prep text
Remove-Item –path "C:\PS\statics.txt"
Remove-Item –path "C:\PS\staticlist.txt"
Remove-Item –path "C:\PS\list.cvs"

param ( 
[string]$LDAPFilter = '(name=*)'
)
$wmiQuery = "select IPAddress, DefaultIPGateway from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE and DHCPEnabled=FALSE"

# Clean up and prep text
$header = "'" + "NAME" + "','" + "IP" + "','" + "OS" + "'"
out-file "C:\PS\list.cvs" -inputobject $header -encoding ASCII -Append

$computers = (Get-ADComputer -LDAPFilter $LDAPFilter)
foreach ($computer in $computers) 
	{ 
    $networkAdapters = (Get-WmiObject -ErrorAction SilentlyContinue -ComputerName $computer.DNSHostName -Query $wmiQuery) 
    foreach ($networkAdapter in $networkAdapters) 
		{ 
        foreach ($ip in $networkAdapter.IPAddress) 
			{ 
            if ($ip -match "\.") 
				{  
				$computerOS = Get-CimInstance CIM_OperatingSystem -computer $computer.DNSHostName
				$os = $computerOS.caption
				$staticlist = $($computer.DNSHostName)
				$a = $($computer.DNSHostName) + " " + $ip
				$cv = "'" + $($computer.DNSHostName) + "','" + $ip + "','" + $os + "'"
				write-host $cv
				out-file "C:\PS\statics.txt" -inputobject $a -encoding ASCII -Append
				out-file "C:\PS\staticlist.txt" -inputobject $staticlist -encoding ASCII -Append
				out-file "C:\PS\list.cvs" -inputobject $cv -encoding ASCII -Append
				} 
			}
		} 
	}


Version 3

# Clean up and prep text
Remove-Item –path "C:\PS\statics.txt"
Remove-Item –path "C:\PS\staticlist.txt"
Remove-Item –path "C:\PS\list.cvs"

param ( 
[string]$LDAPFilter = '(name=*)'
)
$wmiQuery = "select IPAddress, DefaultIPGateway from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE and DHCPEnabled=FALSE"

# Clean up and prep text
$header = "NAME" + "," + "IP" + "," + "OS"
out-file "C:\PS\list.cvs" -inputobject $header -encoding ASCII -Append

$computers = (Get-ADComputer -LDAPFilter $LDAPFilter)
foreach ($computer in $computers) 
	{ 
    $networkAdapters = (Get-WmiObject -ErrorAction SilentlyContinue -ComputerName $computer.DNSHostName -Query $wmiQuery) 
    foreach ($networkAdapter in $networkAdapters) 
		{ 
        foreach ($ip in $networkAdapter.IPAddress) 
			{ 
            if ($ip -match "\.") 
				{  
				$computerOS = Get-CimInstance CIM_OperatingSystem -computer $computer.DNSHostName
				$os = $computerOS.caption
				$staticlist = $($computer.DNSHostName)
				$a = $($computer.DNSHostName) + " " + $ip
				$cv = $($computer.DNSHostName) + ":" + $ip + ":" + $os
				write-host $cv
				out-file "C:\PS\statics.txt" -inputobject $a -encoding ASCII -Append
				out-file "C:\PS\staticlist.txt" -inputobject $staticlist -encoding ASCII -Append
				out-file "C:\PS\list.cvs" -inputobject $cv -encoding ASCII -Append
				} 
			}
		} 
	}

Return to Top

Pull only Server 2012 from text

Version 1.0

$x = Get-DnsServerSetting
$DNSSERVER = $x.ComputerName
$DNSIP = Get-DnsServerSetting | select -ExpandProperty AllIPAddress
$x = Get-DnsServerSetting
$DNSIP = $x.AllIPAddress
$DNSIP -match "\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b"
$Scope = ($matches[1..3] -join ".") + "."
$ServerOS = "Microsoft Windows Server"

$computers = import-csv “C:\PS\list.cvs" –header NAME, IP, OS –delimiter :
ForEach ($computer in $computers)
	{
		$computername = $($computer.NAME)
		$ipaddress = $($computer.IP)
		$os = $($computer.OS)
		if ($ipaddress -Like "$scope*" -And $os -Like "$ServerOS*")
			{
				Write-host $computername $ipaddress $os
			}
	}

Return to Top

Version 2.0

This also will pull Roles and Services and write each to a text file of PCNAME_RolesFeatures.txt

$x = Get-DnsServerSetting
$DNSSERVER = $x.ComputerName
$DNSIP = Get-DnsServerSetting | select -ExpandProperty AllIPAddress
$x = Get-DnsServerSetting
$DNSIP = $x.AllIPAddress
$DNSIP -match "\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b"

$Scope = ($matches[1..3] -join ".") + "."

$ServerOS = "Microsoft Windows Server"

$computers = import-csv “C:\PS\list.cvs" –header NAME, IP, OS –delimiter :

ForEach ($PC in $computers)
	{
		$PCNAME = $($PC.NAME)
		$ipaddress = $($PC.IP)
		$os = $($PC.OS)
		if ($ipaddress -Like "$Scope*" -And $os -Like "$ServerOS*")
			{
				$of = "C:\PS\"+$PCNAME+"_RolesFeatures.txt"
				Get-WindowsFeature -computer $PCNAME | ? {$_.Installed -match “True”} | Select -exp Name | out-file $of -Append
			}
	}
	

Return to Top

References