dnsserverresourcerecord - all expanded properties and zone - powershell

Get-DnsServerResourceRecord -ComputerName server -ZoneName zone.com
I want to run this command and get the HostName, RecordType, ZoneName and All RecordData sets.
I have something like so far:
Get-DnsServerResourceRecord zone.com -ComputerName server |
select hostname, recordType, name,
#{Name='ARecordData';Expression={$_.RecordData.IPv4Address}},
#{Name='CNameRecordData';Expression={$_.RecordData.HostnameAlias}}
My issue is two fold.
I need to know how to get the ZoneName into the record set so if I wanted to pass multiple zones I can keep that data separate.
The above example will create different columns for each RecordData. As a DNS record will only have one of these values is there a way to combine them into one column through PowerShell?

You can add the zone name as a calculated property just like you do with the record data.
foreach ($zone in $zone_list) {
Get-DnsServerResourceRecord -ZoneName $zone -ComputerName server |
Select-Object hostname, #{n='ZoneName';e={$zone}}, recordType, ...
}
Don't create individual properties, instead use a switch statement to select the relevant data depending on the record type in a single property.
... | Select-Object hostname ..., #{n='Data';e={
$rr = $_
switch ($rr.RecordType) {
'A' {$rr.RecordData.IPv4Address}
'CNAME' {$rr.RecordData.HostnameAlias}
...
}
}}

Related

I want to export computer data using Get-CimInstance to a csv but also include the computer name

Right now I'm running
Get-CimInstance -ComputerName $i.DNSHostName -Class CIM_Processor | Select-Object "Name", "NumberOfCores" | Export-Csv -Path .\test.csv -NoTypeInformation -Append`
This will put the CPU name and core count in a CSV fine but how can I add in the hostname as another column?
This is a good case for using a PsCustomObject, which allows you to dynamically create an object with arbitrary properties/values.
$cimProcessor = Get-CimInstance -ComputerName $hostName -Class CIM_Processor
$row = [PSCustomObject]#{
ComputerName = $hostName
Name = $cimProcessor.Name
NumberOfCores = $cimProcessor.NumberOfCores
}
$row | Export-Csv -Path test.csv -NoTypeInformation -Append
In the case where you want to retrieve information from a remote machine first, then you can skip the calculated property and simply select the PSComputerName property instead:
Note: The ellipses ... indicate the code before or after from your original sample.
... | Select-Object PSComputerName, Name, NumberOfCores | ...
Any cmdlet which connects to remote systems via WinRM should have this property automatically set when data is returned over a remote session.
If you are running this from a local session, you could use Select-Object to create a calculated property, then call the hostname command to populate its value:
... | Select-Object "Name", "NumberOfCores", #{
Name = 'ComputerName';
Expression = { hostname };
} | ...
This solution is often suitable for cross platform scripts since a hostname binary is available out of the box on Windows, MacOS, and most major distributions of Linux.
Explaining Calculated Properties
Calculated properties work by defining a hashtable in a specific format and providing the hashtable as a property to be computed just as you would use a string for a real property on the object:
$property = #{
Name = 'PropertyName';
Expression = { 'ScriptBlock' };
}
# Note that the hashtable can be specified inline as shown above
# or as a variable like shown here
[PSCustomObject]#{ Name = 'Bender'; Loves = 'Bending' } |
Select-Object Name, Loves, $property
Keep in mind that within the Expression ScriptBlock, $PSItem/$_ is set to the current object in the pipeline. Use this to reference static or instance properties from the current object you are selecting information from.

How Can I Get DNS Records from a List?

I'm trying to get a list of DNS records on certain subnets. I can get the command to work for 1 or several subnets (by using -like and -or), but I will have 10+ subnets to get records for.
I tried adding a variable and tried to use this with foreach but it just returns an error unexpected token in statement.
I feel like I may have this wrong on where this foreach statement is, but I'm not sure.
This is a little above my head with nesting.
This is what I am able to get to work:
Get-DnsServerResourceRecord -ZoneName zone.local -RRType A |
foreach ($subnet in $subnets) {
where-object {$_.Recorddata.ipv4address -like $subnet}} |
where-object {$_.Recorddata.ipv4address -like "10.1.*.*" -or $_.Recorddata.ipv4address -like "192.168.*.*"} |
Select-Object HostName, RecordType ,#{Name='IPv4Address';Expression={$_.RecordData.IPv4Address}
}
What I tried:
$subnets = get-content .\zonedata.txt
get-dnsserverresourcerecord -zonename "zone.local" -RRType A |
foreach ($subnet in $subnets) {
where-object {$_.REcorddata.ipv4address -like $subnet}} |
Select-Object HostName, RecordType ,#{Name='IPv4Address';Expression={$_.RecordData.IPv4Address}
}
on the code that I am able to get to work, it returns all records within that zone, which is great, but like I said, I'm going to need this for 10+ different zones and I don't want to have to change the script for every zone and have different files for each zone.

Get-CimInstance win32_networkadapterconfiguration to return MAC and IP adress in powershell

I am trying to write a function that returns the MAC and IP addresses from the local network card with the use of "Get-CimInstance win32_networkadapterconfiguration" in a PowerShell function, but it's not returning anything.
function test{
Get-CimInstance win32_networkadapterconfiguration
}
First you need to filter down the results to just return adapters that have an IP address assigned (where {$_.IPAddress -ne $null}). Then, optionally, use select to just get the two properties you want (select MACAddress, IPAddress).
function test{
return Get-CimInstance win32_networkadapterconfiguration | where {$_.IPAddress -ne $null} | select MACAddress, IPAddress
}
This is going to return an object, not a string, so to access the properties individually you would do something like the following:
$Config = test
$Config.MACAddress
$Config.IPAddress
If you have more than one adapter with an IP address, you'll get an array of objects, and you'll need to loop through them, or use a different WHERE filter to further limit the results.

Print (2) columns from powershell array

I created this array with:
$arecords = Get-DnsServerResourceRecord -ZoneName "mydomain.local" -RRType "A"
If I do $arecords.HostName I get a list of the hostnames. If I do $arecords.RecordData I get a list of the IP's. How can print both columns AWK style? Like $arecords.Hostname.RecordData?
The A records are CIM objects, and the default output formatting provided with the DnsServer module must know how to format them, but converting them to string just gets you the DnsServerResourceRecordA text.
You need to get the .RecordData.IPv4Address property out, with a calculated property, e.g.
$arecords| Select-Object -Property hostname, #{name='IP'; Expression={ $_.RecordData.IPv4Address }}
aka
$arecords| select hostname, #{n='IP';E={$_.RecordData.IPv4Address}}

As of PSCustomObject remove extra values

I have command:
Get-VM | Where-Object {$_.NetworkAdapters.NetworkName -eq 'VLAN180'} | Select-Object Name, {$_.Guest.IPAddress}
return the following data:
ss4.work {10.8.0.6, fe80::dd2a:a7d4:7de0:e64d, fe80::fd66:1962:4009:501a, 192.168.180.18}
I need the ip value - 192.168.180.*, but the problem is that $_.Guest.IPAddress it PSCustomObject and I do not understand how to use it.
$_.Guest.IPAddress is an array of objects, possibly PSObjects, possibly IPAddress objects, but the point is that there are several IPs in there and you have to select the ones you want. You also have a one to many relationship (Name to IP) so you have to decide how you want that; I'll assume that you're assuming there will only be one IP that matches the criteria:
Maybe something like:
Get-VM |
Where-Object {
$_.NetworkAdapters.NetworkName -eq 'VLAN180'
} |
Select-Object Name, {$_.Guest.IPAddress.Where({$_.ToString() -like '192.168.180.*'})}