Parsing a csv file - I swear I've seen this before

32 views
Skip to first unread message

Kurt Buff

unread,
Oct 11, 2024, 4:16:42 PM10/11/24
to ntpowe...@googlegroups.com
But I can't remember the solution.

I've got a csv file of DHCP lease info - it's ASCII-encoded and tab-delimited, looking like this:

"DHCPServer" "HostName" "IPAddress" "ClientId" "AddressState" "LeaseExpiryTime"
"dc01.csww.lan" "PC1.example.com" "10.13.20.100" "d0-8e-79-06-94-f5" "Active" "2024-10-18 19:36:07"

I'm trying to extract the ClientId data, but it's truncating the first character of the MAC address, so the output is like this:
0-8e-79-06-94-f5
This happens whether I use select or select -expand

I tested a couple of other fields, and it's truncating the first character in those as well, including the header text if I don't expand.

This doesn't happen if I don't specify ASCII.

I find this to be a bit unexpected - anyone have a clue as to what's going on?

Thanks,
Kurt

Michael B. Smith

unread,
Oct 11, 2024, 4:41:38 PM10/11/24
to ntpowe...@googlegroups.com

Are you SURE it’s ASCII? That behavior indicates to me that it’s probably UTF.

--
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/CADy1Ce7%2BiNQ5LkROn7uJOS1mZcJEwNbKnUKB4M3DNHEbT0ckug%40mail.gmail.com.

Kurt Buff

unread,
Oct 11, 2024, 4:49:41 PM10/11/24
to ntpowe...@googlegroups.com
Checking again.

And dammit, notepad++ it's not, so why isn't the script that scrapes the leases doing what I think it should be doing?

Kurt

$DHCPServers = Get-DhcpServerInDC | select -expand DnsName
$AllLeases = @()
$AllErrors = @()
Foreach ($DHCPServer in $DHCPServers)
   {
      TRY
         {
         $Scopes = Get-DhcpServerv4Scope –ComputerName $DHCPServer
         foreach ($Scope in $Scopes)
            {
            $DHCPServer
            $IPOfScope = $scope.ScopeID.IPAddressToString
            $IPOfScope
            $ScopeLeases = Get-DhcpServerv4Lease -ComputerName $DHCPServer -ScopeId $IPOfScope -AllLeases | SELECT @{Name="DHCPServer"; Expression={$DHCPServer}},HostName, IPAddress, ClientId, AddressState, LeaseExpiryTime
            $AllLeases += $ScopeLeases
            }
         }
      CATCH
         {
            $ThisError  =  $error[0] | SELECT @{Name="ServerName"; Expression={$s}}, @{Name="ErrorType"; Expression={$_.Exception.GetType().FullName}}, @{Name="Error"; Expression={$_.Exception.Message}}
            $AllErrors += $ThisError
         }
   }
$AllLeases | Export-Csv -notype C:\Temp\DHCPLeases.csv -encoding ascii -delim "`t"
$AllErrors | Export-Csv -notype C:\Temp\DHCPLeaseErrors.csv -encoding ascii -delim "`t"

Michael B. Smith

unread,
Oct 11, 2024, 5:24:26 PM10/11/24
to ntpowe...@googlegroups.com

I would drop using encoding and let PS handle it.

 

Have you seen DHCP_Inventory that Web and I wrote over the last decade? It does all this and then much more besides.

Kurt Buff

unread,
Oct 11, 2024, 5:40:21 PM10/11/24
to ntpowe...@googlegroups.com
I'm just trying to gather the MAC addresses to submit to the wireshark OUI lookup page.

I want to check on a number of hosts that are unfamiliar to me, and this was a quick way to do that.

But yes, I've used the script you mention, and a few others. They're good stuff.

Kurt

Reply all
Reply to author
Forward
0 new messages