Answered by:
Get all DHCP server and scope information for a domain

Question
-
I am after some assistance in creating a powershell script to run that will allow me to get all DHCP server and scope information. Below is what I need the script to do. Thank you.
1) It will first run this command:
a) Get-DhcpServerInDC (write the output to a file)
- This will list all DHCP servers in the domain
2) I then need it to loop through each DHCP server in this list (1a) and run
a) Get-DhcpServerv4Scope –ComputerName variable (write the output to a file)
- This will list the scopes of each DHCP server
b) Get-DhcpServerv4OptionValue–ComputerName variable (write the output to a file)
- This will list the server options of each DHCP server
3) I then need it to loop through each DHCP server (1a) and each scope (2a) and run
a) Get-DhcpServerv4OptionValue–ComputerName (1a variable) –ScopeID (2a variable) (write the output to a file)
- This will list the scope options of each scope
Friday, August 11, 2017 1:38 AM
Answers
-
Hi DonTanc
I think the below should work for what you want. This will get a list of all DHCP servers export to a csv, export each servers scope to individual named csv for each dhcp server, export each servers server option to a separate csv and then export each servers scope options to a separate csv.
$DHCPServers = Get-DhcpServerInDC foreach ($computername in $DHCPServers) { ##Export List of DHCP Servers $computername | Export-Csv C:\temp\DHCPServer.csv -Append -NoTypeInformation $scopes = Get-DHCPServerv4Scope -ComputerName $computername.DnsName | Select-Object "Name","SubnetMask","StartRange","EndRange","ScopeID","State" $serveroptions = Get-DHCPServerv4OptionValue -ComputerName $computername.DnsName | Select-Object OptionID,Name,Value,VendorClass,UserClass,PolicyName ForEach ($scope in $scopes) { $DHCPServer = $computername.DnsName ##Export List of scopes on each server $scope | Export-Csv "C:\temp\$DHCPServer-Scopes.csv" -Append -NoTypeInformation ForEach ($option in $serveroptions) { $lines = @() $Serverproperties = @{ Name = $scope.Name SubnetMask = $scope.SubnetMask StartRange = $scope.StartRange EndRange = $scope.EndRange ScopeId = $scope.ScopeId OptionID = $option.OptionID OptionName = $option.name OptionValue =$option.Value OptionVendorClass = $option.VendorClass OptionUserClass = $option.UserClass } $lines += New-Object psobject -Property $Serverproperties $lines | select Name,SubnetMask,StartRange,EndRange,ScopeId,OptionID,OptionName,{OptionValue},OptionVendorClass,OptionUserClass | Export-Csv C:\temp\$dhcpserver-ServerOption.csv -Append -NoTypeInformation } $scopeoptions = Get-DhcpServerv4OptionValue -ComputerName $computername.DnsName -ScopeId "$($scope.ScopeId)" -All | Select-Object OptionID,Name,Value,VendorClass,UserClass,PolicyName ForEach ($option2 in $scopeoptions) { $lines2 = @() $Scopeproperties = @{ Name = $scope.Name SubnetMask = $scope.SubnetMask StartRange = $scope.StartRange EndRange = $scope.EndRange ScopeId = $scope.ScopeId OptionID = $option2.OptionID OptionName = $option2.name OptionValue =$option2.Value OptionVendorClass = $option2.VendorClass OptionUserClass = $option2.UserClass } $lines2 += New-Object psobject -Property $Scopeproperties $lines2 | select Name,SubnetMask,StartRange,EndRange,ScopeId,OptionID,OptionName,{OptionValue},OptionVendorClass,OptionUserClass |Export-Csv C:\temp\$dhcpserver-ScopeOption.csv -Append -NoTypeInformation } } }
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
- Edited by TheSleepyAdmin Saturday, August 12, 2017 4:43 PM Typo in script
- Marked as answer by DonTanc Monday, August 14, 2017 12:29 AM
Saturday, August 12, 2017 3:54 PM
All replies
-
What is your question? We will not write a script for you.
\_(ツ)_/
Friday, August 11, 2017 1:55 AM -
This was the best I could find in my limited time frame:
On each domain, run Get-dhcpserverindc | Export-Csv ./dhcp_servers.csv
Then run the ps1 below and enter the name of each server listed in the csv. If I get a chance later to make it all one script, I'll post.
$computername = Read-Host -Prompt 'Input DHCP Server Name'
$scopes = Get-DHCPServerv4Scope -ComputerName $computername |
Select-Object "Name","SubnetMask","StartRange","EndRange","ScopeID","State"
$lines = @()
$serveroptions = Get-DHCPServerv4OptionValue -ComputerName $computername -All |
Select-Object OptionID,Name,Value,VendorClass,UserClass,PolicyName
ForEach ($scope in $scopes) {
ForEach ($option in $serveroptions) {
$lines += $scope | Select-Object *,@{
"Name"="OptionScope"
"Expression"={ "Server" }},@{
"Name"="OptionID"
"Expression"={ $option.OptionID }},@{
"Name"="OptionName"
"Expression"={ $option.name }},@{
"Name"="OptionValue"
"Expression"={ $option.Value }},@{
"Name"="OptionVendorClass"
"Expression"={ $option.VendorClass }},@{
"Name"="OptionUserClass"
"Expression"={ $option.UserClass }}
}
$scopeoptions = Get-DhcpServerv4OptionValue -ComputerName $computername -ScopeId "$($scope.ScopeId)" -All |
Select-Object OptionID,Name,Value,VendorClass,UserClass,PolicyName
ForEach ($option in $scopeoptions) {
$lines += $scope | Select-Object *,@{
"Name"="OptionScope"
"Expression"={ "Scope" }},@{
"Name"="OptionID"
"Expression"={ $option.OptionID }},@{
"Name"="OptionName"
"Expression"={ $option.name }},@{
"Name"="OptionValue"
"Expression"={ $option.Value }},@{
"Name"="OptionVendorClass"
"Expression"={ $option.VendorClass }},@{
"Name"="OptionUserClass"
"Expression"={ $option.UserClass }}
}
}
$lines | Export-Csv -Path .\dchp_info.csv -NoTypeInformationFriday, August 11, 2017 5:11 AM -
Hi DonTanc
I think the below should work for what you want. This will get a list of all DHCP servers export to a csv, export each servers scope to individual named csv for each dhcp server, export each servers server option to a separate csv and then export each servers scope options to a separate csv.
$DHCPServers = Get-DhcpServerInDC foreach ($computername in $DHCPServers) { ##Export List of DHCP Servers $computername | Export-Csv C:\temp\DHCPServer.csv -Append -NoTypeInformation $scopes = Get-DHCPServerv4Scope -ComputerName $computername.DnsName | Select-Object "Name","SubnetMask","StartRange","EndRange","ScopeID","State" $serveroptions = Get-DHCPServerv4OptionValue -ComputerName $computername.DnsName | Select-Object OptionID,Name,Value,VendorClass,UserClass,PolicyName ForEach ($scope in $scopes) { $DHCPServer = $computername.DnsName ##Export List of scopes on each server $scope | Export-Csv "C:\temp\$DHCPServer-Scopes.csv" -Append -NoTypeInformation ForEach ($option in $serveroptions) { $lines = @() $Serverproperties = @{ Name = $scope.Name SubnetMask = $scope.SubnetMask StartRange = $scope.StartRange EndRange = $scope.EndRange ScopeId = $scope.ScopeId OptionID = $option.OptionID OptionName = $option.name OptionValue =$option.Value OptionVendorClass = $option.VendorClass OptionUserClass = $option.UserClass } $lines += New-Object psobject -Property $Serverproperties $lines | select Name,SubnetMask,StartRange,EndRange,ScopeId,OptionID,OptionName,{OptionValue},OptionVendorClass,OptionUserClass | Export-Csv C:\temp\$dhcpserver-ServerOption.csv -Append -NoTypeInformation } $scopeoptions = Get-DhcpServerv4OptionValue -ComputerName $computername.DnsName -ScopeId "$($scope.ScopeId)" -All | Select-Object OptionID,Name,Value,VendorClass,UserClass,PolicyName ForEach ($option2 in $scopeoptions) { $lines2 = @() $Scopeproperties = @{ Name = $scope.Name SubnetMask = $scope.SubnetMask StartRange = $scope.StartRange EndRange = $scope.EndRange ScopeId = $scope.ScopeId OptionID = $option2.OptionID OptionName = $option2.name OptionValue =$option2.Value OptionVendorClass = $option2.VendorClass OptionUserClass = $option2.UserClass } $lines2 += New-Object psobject -Property $Scopeproperties $lines2 | select Name,SubnetMask,StartRange,EndRange,ScopeId,OptionID,OptionName,{OptionValue},OptionVendorClass,OptionUserClass |Export-Csv C:\temp\$dhcpserver-ScopeOption.csv -Append -NoTypeInformation } } }
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
- Edited by TheSleepyAdmin Saturday, August 12, 2017 4:43 PM Typo in script
- Marked as answer by DonTanc Monday, August 14, 2017 12:29 AM
Saturday, August 12, 2017 3:54 PM -
Brilliant! Thank you. An invaluable tool for all DHCP audit requirements.Monday, August 14, 2017 12:29 AM
-
I am in need some assistance in creating a powershell script which is similar to the question from DonTanc except that I already have the list of the Servers in a csv file. I need script to allow me to get all DHCP server scope information from the csv file.Monday, March 25, 2019 12:25 PM
-
I am in need some assistance in creating a powershell script which is similar to the question from DonTanc except that I already have the list of the Servers in a csv file. I need script to allow me to get all DHCP server scope information from the csv file.
This is not a script request forum. Please do not add your off-topic question to another users question. YOU must start your own question and post your script with any errors.
\_(ツ)_/
Monday, March 25, 2019 2:12 PM -
Hi DonTanc,
Thanks for the valuable suggestion, i followed the same in my environment.
I am getting DHCP Scope details with Option ID, But the values defined in Option ID in Scope is not appearing in Export result. There is a requirement to get the Option ID 242 & 176 Value for all scopes in all DHCP Server in domain.
Can you please test the same at your end and suggest how i can get the required details.
Tuesday, April 2, 2019 11:26 AM