Don't understand why I'm seeing this

100 views
Skip to first unread message

Kurt Buff, GSEC/GCIH/PCIP

unread,
Dec 10, 2020, 11:42:04 AM12/10/20
to ntpowe...@googlegroups.com
All,

Why am I seeing RunspaceId? I didn't ask for it, yet there it is, just as if I had said it's name three times while looking into a mirror...

Invoke-Command -ComputerName $DHCPServer -scriptblock { Get-DhcpServerv4Scope | select ScopeId,Name,State,Description,PSComputerName }
ScopeId        : 10.1.1.0
Name           : Signs
State          : Inactive
Description    :
PSComputerName : wgf-server
RunspaceId     : c4dbf369-3836-45d1-a8c5-29800164b683

Michael B. Smith

unread,
Dec 10, 2020, 12:29:32 PM12/10/20
to ntpowe...@googlegroups.com

It’s one of several pseudo-attributes that may get added during hydration of serialized data.

 

Either do the select-object (again) on the results of Invoke-Command, or update FormatData/TypeData for Invoke-Command.

--
You received this message because you are subscribed to the Google Groups "ntpowershell" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntpowershell...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ntpowershell/CADy1Ce7jBbszUEWNj68H8JMNC%3DTx2J4VtwkLv5Cgt9iKK1146A%40mail.gmail.com.

Kurt Buff, GSEC/GCIH/PCIP

unread,
Dec 10, 2020, 12:33:32 PM12/10/20
to ntpowe...@googlegroups.com
Weird, but simple enough to work around.

Thanks!

Kurt

Markus Klocker

unread,
Dec 11, 2020, 3:43:46 AM12/11/20
to ntpowe...@googlegroups.com
Any difference wen you use "-Property" parameter?
I don't have a DHCP to try it :)   
    Markus

Kurt Buff, GSEC/GCIH/PCIP

unread,
Dec 11, 2020, 12:40:37 PM12/11/20
to ntpowe...@googlegroups.com
No change when looking at properties.

I've been putting this together in my few spare moments between other tasks, so that I can audit our DHCP servers (all but one of which are not DCs). I'm working on getting DHCP servers audited and consistent in their settings, and also comparing them to a couple of locations where (maddeningly) DHCP is handled by the firewall.

I've run through Carl Webster's DHCP documentation script, and found it so confusing I gave up on it - and I want CSV output anyway, so that comparison between DHCP servers will be easier.

Here's the basic code I'm trying get to work, after hammering on it yesterday while listening to a 5-hour web conference on cloud security. The "sort:-unique" below is because of stupid reasons - a number of the servers have two NICs in them, each assigned with a separate address on the same subnet. So many bad decisions have been made in this network. Anyway, enough kvetching. The last line of code below, which uses $ScopeId, emits the following error - I've tried several different approaches to populating the $ScopeIds variable, and nothing seems to work for getting the values from it into the next line:

Cannot process argument transformation on parameter 'ScopeId'. Cannot convert value "" to type "System.Net.IPAddress". Error: "An invalid IP address was specified."
    + CategoryInfo          : InvalidData: (:) [Get-DhcpServerv4OptionValue], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-DhcpServerv4OptionValue
    + PSComputerName        : server1.example.com


$ServerOptions = @()
$Scopes = @()
$ScopeIDs = @()
$ScopeOptions = @()

$DHCPServers = Get-DhcpServerInDC | sort -Unique DNSName | Select -Expand DNSName

ForEach ($DHCPServer in $DHCPServers)
   {
   $ServerOptions += Invoke-Command -ComputerName "$DHCPServer" -scriptblock { Get-DhcpServerv4OptionValue | select OptionId,Name,Value,PSComputerName }
   $Scopes += Invoke-Command -ComputerName "$DHCPServer" -Scriptblock { Get-DhcpServerv4Scope | select ScopeId,Name,State,Description,PSComputerName }
   $ScopeIds = ForEach ($Scope in $Scopes) { ($Scope.ScopeId).ToString() }
   $ScopeOptions += ForEach ($ScopeId in $ScopeIds) { Invoke-Command -ComputerName "$DHCPServer" -scriptblock { Get-DhcpServerv4OptionValue -ScopeId "$ScopeId" | select OptionId,Name,Value,PSComputerName } }
   }



Kurt

Michael B. Smith

unread,
Dec 11, 2020, 2:24:22 PM12/11/20
to ntpowe...@googlegroups.com

I wrote much of the original code in that DHCP documentation script 😊

 

I take feedback – what do you find confusing about it?

 

From: ntpowe...@googlegroups.com <ntpowe...@googlegroups.com> On Behalf Of Kurt Buff, GSEC/GCIH/PCIP

Xavier Belanger

unread,
Dec 11, 2020, 2:41:20 PM12/11/20
to ntpowe...@googlegroups.com, Kurt Buff, GSEC/GCIH/PCIP
Hi,

Kurt Buff, GSEC/GCIH/PCIP wrote:

> I've been putting this together in my few spare moments between other
> tasks, so that I can audit our DHCP servers (all but one of which are
> not DCs). I'm working on getting DHCP servers audited and consistent in
> their settings, and also comparing them to a couple of locations where
> (maddeningly) DHCP is handled by the firewall.
If you (and anyone else on the list) are interested I can share with
you a PowerShell script of mine that generate a pseudo "dhcpd.conf"
configuration file for a given DHCP server. So that way you can
easily review and compare the configuration between servers (I usually
rely on the 'Compare' function in Notepad++ for this).

Here is an example of the output, just for one scope (the same
script can target one scope or a full DHCP server):

PS C:\dhcp\scripts> .\Get-DhcpScopesConfiguration.ps1 -ComputerName
dhcp-srv-1.cua.edu -ScopeAddress 10.1.0.0
dhcp-srv-1-10.1.0.0-config-20201211-1428.log has been generated
PS C:\dhcp\scripts> Get-Content .\dhcpprd1-10.1.0.0-config-20201211-1428.log

Global DNS Servers: 10.1.2.3 10.1.2.4 10.1.2.5
Global DNS Domain Name: cua.edu
Vendor Classes:

Name Data
AsciiData Description
---- ----
--------- -----------
airespace 0x4169726573----------415031323030
Airespace.AP1200 1010
Airespace 2700 0x436973636f----------32373030 Cisco
AP c2700 Cisco Aironet 2700 Series
Cisco Aironet 1200 0x436973636f----------31323030 Cisco
AP c1200 Vendor Class Indentifier For ...
Mitel IP Phone Options 0x697070686f----------74656c2e636f6d00
Mitel-specific options for IP...
PXEClient (BIOS) 0x505845436c----------417263683a3030303030
PXEClient:Arch:00000 PXEClient (BIOS)
PXEClient (UEFI x64) 0x505845436c----------417263683a3030303037
PXEClient:Arch:00007 PXEClient (UEFI x64)
Vendor Classes Options Definitions:

VendorClass Name Description
OptionId
----------- ---- -----------
--------
airespace Airespace IP provision 1010
102
Airespace 2700 Airespace 2700 IP provision IP Delivery Option
Airespace 2700 102
Cisco Aironet 1200 Option 43 WLAN Controller IP
Adresses 241
Phone Options Phone Config Configuration string
43

Scope : 10.1.0.0
Name : Worksations
Description : Workstations Network
Range : 10.1.0.10 - 10.1.0.250
Subnet Mask : 255.255.255.0
Router : 10.1.0.1
DNS Servers : Global option is used
DNS Domain Name : Global option is used
DDNS Enabled : Always
Lease Duration : 08:00:00
Reservations Count : 2
Reservations Details : 10.1.0.14 - aa-bb-cc-11-22-33 - client-1.cua.edu
10.1.0.15 - 11-22-33-aa-bb-cc - client-2.cua.edu
Vendor Option (local) : 10.20.1.2 [0x00 0xF1 0x04 0x0A 0x14 0x01 0x02]
Vendor Option (global) : None
PXE BIOS : None
PXE UEFI : None


PS C:\dhcp\scripts>

Just for you to know, if you are working on your own script,
some DHCP pieces of data are stored in different formats, and
there is a fair amount of conversion/formatting required
to get a decent output.

Sincerely,
--
Xavier Belanger
Network Engineer III - CISSP - Technology Services
The Catholic University of America

Michael B. Smith

unread,
Dec 11, 2020, 3:12:05 PM12/11/20
to ntpowe...@googlegroups.com

The problem with your script is twofold: [1] you don’t pass a necessary param to one of your invoke-commands, and [2] you are checking all scopes for all servers scanned. I expect that it would work with the first server and fail with all subsequent servers, once [1] is fixed.

 

The below works (limited testing: my lab and two prod environments).

 

BTW, it would be much faster to:

 

[1] create a PSSession for each DHCPServer so that PSRemoting is only instantiated once for each DHCPServer, OR

[2] have a more complex scriptblock that returns a PSCustomObject containing all the results from each DHCPServer (again, PSRemoting is only instantiated once for each DHCPServer).

 

$ServerOptions = @()

$Scopes = @()

$ScopeIDs = @()

$ScopeOptions = @()

 

$DHCPServers = Get-DhcpServerInDC | Sort-Object -Unique DNSName | Select-Object -Expand DNSName

 

ForEach ($DHCPServer in $DHCPServers)

{

    $localServerOptions = @( Invoke-Command -ComputerName "$DHCPServer" -Scriptblock { Get-DhcpServerv4OptionValue | Select-Object OptionId,Name,Value,PSComputerName } )

    $localServerScopes  = @( Invoke-Command -ComputerName "$DHCPServer" -Scriptblock { Get-DhcpServerv4Scope | Select-Object ScopeId,Name,State,Description,PSComputerName } )

    $localScopeIds      = @( foreach( $scope in $localServerScopes ) { $scope.ScopeId.ToString() } )

    $localScopeOptions  = @( foreach( $scopeId in $localScopeIds ) { 

        Invoke-Command -ArgumentList $scopeId -ComputerName "$DHCPServer" -Scriptblock {

            Param( $scopeId )

            Get-DhcpServerv4OptionValue -ScopeId "$scopeId" | Select-Object OptionId,Name,Value,PSComputerName 

        } 

    } )

 

    $ServerOptions += $localServerOptions

    $Scopes        += $localServerScopes

    $ScopeIds      += $localScopeIds

    $ScopeOptions  += $localScopeOptions

}

 

 

From: ntpowe...@googlegroups.com <ntpowe...@googlegroups.com> On Behalf Of Kurt Buff, GSEC/GCIH/PCIP
Sent: Friday, December 11, 2020 12:40 PM
To: ntpowe...@googlegroups.com
Subject: Re: [ntpowershell] Don't understand why I'm seeing this

 

No change when looking at properties.

Markus Klocker

unread,
Dec 11, 2020, 3:28:40 PM12/11/20
to ntpowe...@googlegroups.com
I call that awesomeness and greatness at the same time!
I'm just feeling the level up!

At the same time I have to say not all the parts you might need are in the reach of one.
Getting more info is for me the best way to getting more useful!

Thank You!
    Markus

Michael B. Smith

unread,
Dec 11, 2020, 3:42:42 PM12/11/20
to ntpowe...@googlegroups.com

I’m not sure what you mean? I think this is functionally equivalent and measurably faster:

 

$ServerOptions = @()

$Scopes = @()

$ScopeIDs = @()

$ScopeOptions = @()

 

$DHCPServers = Get-DhcpServerInDC | Sort-Object -Unique DNSName | Select-Object -Expand DNSName

 

ForEach ($DHCPServer in $DHCPServers)

{

    $result = Invoke-Command -ComputerName $DHCPServer {

        $options   = @( Get-DhcpServerv4OptionValue | Select-Object OptionId, Name, Value, PSComputerName )

        $scopes    = @( Get-DhcpServerv4Scope | Select-Object ScopeId, Name, State, Description, PSComputerName )

        $scopeIds  = @( foreach( $scope in $scopes ) { $scope.ScopeId.ToString() } )

        $scopeOpts = @( foreach ( $scopeId in $scopeIds ) { Get-DhcpServerv4OptionValue -ScopeId $scopeId | Select-Object OptionId, Name, Value, PSComputerName } )

 

        [PSCustomObject] @{

            ServerOptions = $options

            ServerScopes  = $scopes

            ScopeIds      = $scopeIds

            ScopeOptions  = $scopeOpts

        }

    }

 

    $ServerOptions += $result.ServerOptions

    $Scopes        += $result.ServerScopes

    $ScopeIds      += $result.ScopeIds

    $ScopeOptions  += $result.ScopeOptions

Kurt Buff, GSEC/GCIH/PCIP

unread,
Dec 11, 2020, 3:57:43 PM12/11/20
to ntpowe...@googlegroups.com
First, thank you again for the help you give.
Second, my feedback on Carl's script is that my understanding is so basic, and my time so constrained, that I when I started to try to grok it, my brain just went into gibbering monkey mode. I will see if I can dedicate some more time to looking it over, and see if I can get more coherent feedback than that.

I see that you changed things quite a bit - and I just *knew* that my approach was horribly inefficient, to make repeated connections to each machine to fetch bits of data that should all come in one query, but I tried to accomplish what I could understand.

The biggest surprise (which means most learning, of course) is the passing of the argumentlist for the Invoke-Command statement along with the parameter inside the scriptblock.

The script does not error out, and gives me data, so that's a major win.

However, the data in $ScopeOptions data doesn't have the ScopeId - I'll bet that this is my clue to use a PSSession and return as PSCustomObject as you noted.

I'll rework the script and see what I can come up with.

Kurt Buff, GSEC/GCIH/PCIP

unread,
Dec 11, 2020, 4:02:19 PM12/11/20
to ntpowe...@googlegroups.com
Well, now I have my example to study.😀

Kurt

Michael B. Smith

unread,
Dec 11, 2020, 4:38:37 PM12/11/20
to ntpowe...@googlegroups.com

Your Select-Object for Get-DhcpServerv4OptionValue doesn’t include ScopeId. Of course it isn’t in the list. 😊

 

Computers are funny that way. 😊

 

But after checking – it’s not even available, so it has to be a constructed attribute.

 

 

Like this (change line 14 from the v2 script):

 

        $scopeOpts = @( foreach ( $scopeId in $scopeIds ) { Get-DhcpServerv4OptionValue -ScopeId $scopeId | Select-Object @{ N = 'ScopeId'; E = { $scopeid } }, OptionId, Name, Value, PSComputerName } )

 

In regards to scriptblock parameters – it isn’t obvious and OFTEN BUT NOT ALWAYS you can avoid that by using a special “scope modifier” called “using:”. Because it doesn’t always work, I use ArgumentList instead. It always works. In your specific case, you could’ve said $using:scopeId instead of ArgumentList, but you had to do one or the other.

 

For more information on this topic (both about using: and ArgumentList) see “help about_remote_variables”.

Kurt Buff, GSEC/GCIH/PCIP

unread,
Dec 11, 2020, 7:17:28 PM12/11/20
to ntpowe...@googlegroups.com
Outstanding.

Weirdly, however,, PSComputerName doesn't come through for either $ServerOptions or $ScopeOptions.

I'm going to look at that over the weekend and see if I can figure that out.

Thanks again,
Kurt

Reply all
Reply to author
Forward
0 new messages