Reputation: 515
No matter what I do, the output to the CSV file is blank. Please advise what I am doing wrong. I just need to find out why the output does not get to the CSV file.
$servers = Get-Content "C:\temp\servers.txt"
foreach ($Server in $servers) {
Get-IPAddress -ComputerName $servers -IPV4only
}
Select-Object Name, IP | Export-CSV c:\temp\final.txt
The function Get-IPAddress
is implemented like this:
function global:Get-IPAddress {
#Requires -Version 2.0
[CmdletBinding()]
Param (
[Parameter(Position=1,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String[]]$ComputerName = $env:COMPUTERNAME,
[Switch]$IPV6only,
[Switch]$IPV4only
)#End Param
Begin {
Write-Verbose "`n Checking IP Address . . .`n"
$i = 0
}#Begin
Process {
$ComputerName | ForEach-Object {
$HostName = $_
Try {
$AddressList = @(([net.dns]::GetHostEntry($HostName)).AddressList)
} Catch {
"Cannot determine the IP Address on $HostName"
}
If ($AddressList.Count -ne 0) {
$AddressList | ForEach-Object {
if ($IPV6only) {
if ($_.AddressFamily -eq "InterNetworkV6") {
New-Object psobject -Property @{
IPAddress = $_.IPAddressToString
ComputerName = $HostName
} | Select ComputerName,IPAddress
}
}
if ($IPV4only) {
if ($_.AddressFamily -eq "InterNetwork") {
New-Object psobject -Property @{
IPAddress = $_.IPAddressToString
ComputerName = $HostName
} | Select ComputerName,IPAddress
}
}
if (!($IPV6only -or $IPV4only)) {
New-Object psobject -Property @{
IPAddress = $_.IPAddressToString
ComputerName = $HostName
} | Select ComputerName,IPAddress
}
}#Foreach-Object(IPAddress)
}#IF
}#Foreach-Object(ComputerName)
}#Process
}#Get-IPAddress
Upvotes: 1
Views: 120
Reputation: 200193
You don't provide anything to select from (you run Get-IPAddress
in a foreach
loop, but the output is not passed into the Select-Object
cmdlet). Also, your function produces objects with a ComputerName
property, but you try to select a property Name
.
Change your code to this:
Get-Content 'C:\temp\servers.txt' | ForEach-Object {
Get-IPAddress -ComputerName $servers -IPV4only
} | Select-Object ComputerName, IP | Export-Csv 'c:\temp\final.txt' -NoType
As a side note, you may want to make the switches -IPV4only
and -IPV6only
mutually exclusive by putting them into different parameter sets:
[CmdletBinding(DefaultParameterSetName='all')]
Param (
[Parameter(Position=1,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(ParameterSetName='v6')]
[Switch]$IPV6only,
[Parameter(ParameterSetName='v4')]
[Switch]$IPV4only
)
Upvotes: 2
Reputation: 174435
Looking at your snippet, the two statements (the foreach(...)
construct and the Select-Object|Export-Csv
expression) are totally disconnected.
Assign the output from the expression inside foreach
to a variable, and pipe that to Select-Object
instead
$servers = Get-Content "C:\temp\servers.txt"
$Output = foreach ($Server in $Servers)
{
Get-IPAddress -ComputerName $server -IPV4only |Select IP,@{Name="Name";Expression={$server}}
}
$Output | Select-Object Name, IP | Export-CSV c:\temp\final.txt
Upvotes: 2