Modify PS Function

I found this function which someone else wrote, which works really well, however I want to simplify it. It does more stuff that I would like it to do, simplifying will make it more useful for my applicaton. I have attempted to modify it and add a parameter, but my skills in this area are lacking. Hoping someone can help.

Original function:

[pre]

Function Get-TMDhcpDNS {
<#
.SYNOPSIS
The Get-TMDhcpDNS advanced function determines the DNS Servers being used by a DHCP Scope.

.DESCRIPTION
The Get-TMDhcpDNS advanced function determines the DNS Servers being used by a DHCP Scope whether the DNS Servers are assigned by the Scope Options, or Server Options.

.PARAMETER ComputerName
This mandatory parameter is the DHCP Server(s). This function can be invoked against a single DHCP server, or a list of comma-seperated DHCP servers.

.EXAMPLE
Get-TMDhcpDNS -ComputerName ‘dhcpsrv1.mydomain.com’ | Format-Table -AutoSize

This example will return the DHCP Scopes on the dhcpsrv1.mydomain.com DHCP Server. It will include the Name (DHCP Server Name), ScopeName, ScopeId, the DNS IPs, and whether the DNS is being assigned at the Scope or Server level. It will format the data in an autosized table.

.NOTES
NAME: Get-TMDhcpDNS
AUTHOR: Tommy Maynard
WEB: http://tommymaynard.com
VERSION: 1.0.1
LASTEDIT: 4/29/2015
1.0.1:
Added abilty to run against multiple DHCP Servers: Moved first try-catch to Process block (from Begin block), modified variables names, etc.
1.0.2:
Prevented System.Object for DNS when exporting to CSV.
Added embedded try-catch (in 2nd try-catch) when getting the DNS from the server (not from the scope). The catch portion should, in theory, never run.
#>
[CmdletBinding()]
Param (
[Parameter(Position = 0,Mandatory = $true,ValueFromPipeline = $true)]
[string]$ComputerName
)

Begin {
} # End Begin

Process {
Foreach ($Computer in $ComputerName) {
try {
$Scopes = Get-DhcpServerv4Scope -ComputerName $Computer -ErrorAction Stop
$Continue = $true
} catch [Microsoft.Management.Infrastructure.CimException] {
Write-Warning -Message “Cannot reach DHCP Server (ComputerName: $Computer).”
$Continue = $false
} catch {
Write-Warning -Message “Unknown Error.”
$Continue = $false
} # End try-catch.

If ($Continue) {
Foreach ($Scope in $Scopes){
Clear-Variable DNS,ScopeDNS -ErrorAction SilentlyContinue
try {
$DNS = (Get-DhcpServerv4OptionValue -ComputerName $Computer -ScopeID $Scope.ScopeId -OptionID 6 -ErrorAction Stop | Select-Object -ExpandProperty Value) -join ‘,’
$ScopeOrServerDNS = ‘Scope’
} catch {
try {
$DNS = (Get-DhcpServerv4OptionValue -ComputerName $Computer -OptionId 6 | Select-Object -ExpandProperty Value) -join ‘,’
$ScopeOrServerDNS = ‘Server’
} catch {
$DNS = ‘Unknown’
$ScopeOrServerDNS = ‘Unknown’
}
} # End try-catch.

$Object = [PSCustomObject]@{
Name = $Computer
ScopeName = $Scope.Name
ScopeID = $Scope.ScopeId
DNS = $DNS
ScopeOrServerDNS = $ScopeOrServerDNS
}
Write-Output -Verbose $Object
} # End Foreach 2.
} # End If.
} # End Foreach 1.
} # End Process.
}

[/pre]

I made some modifications to get rid of the foreach and if, added a parameter to get the ScopeID. What I would like to be able to do is use the function, specify ComputerName, ScopeID and then return the DNS servers. The custom object I can modify to work for my needs. I just am not sure why my modifications aren’t working. It returns unknown each time I run it.

[pre]

Function Get-TMDhcpDNS {
<#
.SYNOPSIS
The Get-TMDhcpDNS advanced function determines the DNS Servers being used by a DHCP Scope.

.DESCRIPTION
The Get-TMDhcpDNS advanced function determines the DNS Servers being used by a DHCP Scope whether the DNS Servers are assigned by the Scope Options, or Server Options.

.PARAMETER ComputerName
This mandatory parameter is the DHCP Server(s). This function can be invoked against a single DHCP server, or a list of comma-seperated DHCP servers.

.EXAMPLE
Get-TMDhcpDNS -ComputerName ‘dhcpsrv1.mydomain.com’ | Format-Table -AutoSize

This example will return the DHCP Scopes on the dhcpsrv1.mydomain.com DHCP Server. It will include the Name (DHCP Server Name), ScopeName, ScopeId, the DNS IPs, and whether the DNS is being assigned at the Scope or Server level. It will format the data in an autosized table.

.NOTES
NAME: Get-TMDhcpDNS
AUTHOR: Tommy Maynard
WEB: http://tommymaynard.com
VERSION: 1.0.1
LASTEDIT: 4/29/2015
1.0.1:
Added abilty to run against multiple DHCP Servers: Moved first try-catch to Process block (from Begin block), modified variables names, etc.
1.0.2:
Prevented System.Object for DNS when exporting to CSV.
Added embedded try-catch (in 2nd try-catch) when getting the DNS from the server (not from the scope). The catch portion should, in theory, never run.
#>
[CmdletBinding()]
Param (
[Parameter(Position = 0,Mandatory = $true,ValueFromPipeline = $true)]
[string]$ComputerName,
[Parameter(Position = 1,Mandatory = $true,ValueFromPipeline = $true)]
[IPAddress]$ScopeID
)

Begin {
} # End Begin

Process {
#Foreach ($Computer in $ComputerName) {
#try {
#$Scopes = Get-DhcpServerv4Scope -ComputerName $Computer -ErrorAction Stop
#$Continue = $true
#} catch [Microsoft.Management.Infrastructure.CimException] {
#Write-Warning -Message “Cannot reach DHCP Server (ComputerName: $Computer).”
#$Continue = $false
#} catch {
#Write-Warning -Message “Unknown Error.”
#$Continue = $false
#} # End try-catch.

#If ($Continue) {
#Foreach ($Scope in $Scopes){
#Clear-Variable DNS,ScopeDNS -ErrorAction SilentlyContinue
try {
$DNS = (Get-DhcpServerv4OptionValue -ComputerName $ComputerName -ScopeID $ScopeID -OptionID 6 -ErrorAction Stop | Select-Object -ExpandProperty Value) -join ‘,’
$ScopeOrServerDNS = ‘Scope’
} catch {
try {
$DNS = (Get-DhcpServerv4OptionValue -ComputerName $ComputerName -OptionId 6 | Select-Object -ExpandProperty Value) -join ‘,’
$ScopeOrServerDNS = ‘Server’
} catch {
$DNS = ‘Unknown’
$ScopeOrServerDNS = ‘Unknown’
}
} # End try-catch.

$Object = [PSCustomObject]@{
Name = $ComputerName
#ScopeName = $Scope.Name
ScopeID = $Scope
DNS = $DNS
ScopeOrServerDNS = $ScopeOrServerDNS
}
Write-Output -Verbose $Object
#} # End Foreach 2.
#} # End If.
#} # End Foreach 1.
} # End Process.
}

[/pre]

Jesse,

Please provide the errors you are receiving, this will show us what part it’s erroring out on. While you do this I’m reviewing the function.

Doesn’t produce any errors, however it hits the last catch and returns ‘unknown’. If I pull the code out of the function and specify the values for $ComputerName and $ScopeID, it works. Not sure how much that helps.

 

 

Get-TMDhcpDNS -ComputerName FQDN -ScopeID 10.10.11.1

Name ScopeID DNS ScopeOrServerDNS


{FQDN} 10.10.11.1 Unknown Unknown

do you get any error if you run this command Get-DhcpServerv4OptionValue -ComputerName $ComputerName -ScopeID $ScopeID -OptionID 6

-computerName and -IPAddress here for Get-DhcpServerv4OptionValue cmdlet doesn’t take array and those parameters are having array type, so it won’t work without foreach.

@Aapeli

Depends, if I use a scope ID where those values are set inside the scope the command works fine. If I use a scope ID where the values at set on the DHCP server it fails. I would expect this behavior.

Failure:

Get-DhcpServerv4OptionValue : Failed to get option value of 6 on DHCP server servername.
At line:6 char:1

  • Get-DhcpServerv4OptionValue -ComputerName $ComputerName -ScopeID $Sco …
  • CategoryInfo : ObjectNotFound: (6:root/Microsoft/…erv4OptionValue) [Get-DhcpServerv4OptionValue], CimException
  • FullyQualifiedErrorId : DHCP 20010,Get-DhcpServerv4OptionValue

 

@kvprasoon

Is it possible to modify the parameters to accept values without the foreach? I am just trying to be able to call the function with specified values.

 

Thanks.