I've been working on a script which will take IPs/DNSes and create a 2-column output. While it's working inside of powershell, as soon as I try to export to CSV it return system.object. How can I fix it? Here's my code:
function Resolve-IPDNS {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string[]]$IPDNS,
[string]$DNSServer = "1.1.1.1"
)#param
BEGIN {}
Process {
foreach ($addrs in $IPDNS ) {
#Resolve IP or DNS
IF ($addrs -as [ipaddress]) {
$resolve_params = @{'Name' = $addrs
'Server' = $DNSServer
'Type' = 'PTR'}
} ELSE {
$resolve_params = @{'Name' = $addrs
'Server' = $DNSServer
'Type' = 'A'}
}
$resolve = Resolve-DnsName @resolve_params
#Create props
IF ($addrs -as [ipaddress]) {
$props = @{'IP' = $resolve.Name
'DNS' = $resolve.NameHost}
} ELSE {
$props = @{'IP' = $resolve.IPAddress
'DNS' = $resolve.Name}
}#IF
#Output data
$obj = New-Object -TypeName psobject -Property $props
Write-Output $obj
}#foreach
}#Process
END {}
}#function
Resolve-IPDNS
So if try to provide bbc.com I get result:
IP DNS
-- ---
{151.101.0.81, 151.101.64.81, 151.101.128.81, 151.101.192.81} {bbc.com, bbc.com, bbc.com, bbc.com}
Is there a way to expand/unwrap while keeping the object-way?
Make sure you iterate over each result from Resolve-DnsName
:
function Resolve-IPDNS {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string[]]$IPDNS,
[string]$DNSServer = "1.1.1.1"
)
Process {
foreach($addrs in $IPDNS) {
if($addrs -as [ipaddress]) {
$resolve_params = @{
'Name' = $addrs
'Server' = $DNSServer
'Type' = 'PTR'
}
}
else {
$resolve_params = @{
'Name' = $addrs
'Server' = $DNSServer
'Type' = 'A'
}
}
foreach($resolve in Resolve-DnsName @resolve_params) {
if($addrs -as [ipaddress]) {
$props = @{
'IP' = $resolve.Name
'DNS' = $resolve.NameHost
}
}
else {
$props = @{
'IP' = $resolve.IPAddress
'DNS' = $resolve.Name
}
}
#Output data
$obj = New-Object -TypeName psobject -Property $props
Write-Output $obj
}
}
}
}