Reputation: 3
While this is likely not very hard to do, I have hit a bit of a wall. I have very little programming experience in any language really so please be kind.
My objective is to create a simple(working?) function that will snag the IP and Subnet pairs for each interface on a given host. This in itself is a cakewalk and works quite well. The issue I have is that I have many hosts with dozens or hundreds of IP's on them. Displaying this would not be feasible or usable so I figured I would dump it to a CSV file which would offer me the ability to directly manipulate the data from both PS as well as just opening the CSV in whatever program or editor I desire.
My issue is that I am unable to get everything to list correctly. I feel that my logic after getting the information and smashing it together is rather flawed or flat wrong and needs some professional help.
This is the stable portion of the code that seems to work.
function Get-IPConfig
{
$op_file_location = $args[0]
$op_file = (Import-Csv -Header ID $op_file_location)
if (! $op_file_location) {Write-Host "Please try again in the following format: Get-IPConfig c:\full\file\path\here.csv"}
else {
Foreach ($op_file.ID in $op_file)
{
Try {
$SysConfig = @(gwmi -Class Win32_ComputerSystem -ComputerName $op_file.ID)
$NetConfig = @(gwmi -class Win32_NetworkAdapterConfiguration -ComputerName $op_file.ID -Filter "IPEnabled='TRUE'")
}
Catch [System.Exception] {
Write-Host $op_file.ID "is not responding correctly. It may be offline, or you may not have permission to access it."
}
$ResultsIP = $NetConfigItem.IPAddress
$ResultsSubnet = $NetConfigItem.IPSubnet
$Results = $ResultsIP + $ResultsSubnet
Foreach ($NetConfigItem in $NetConfig)
{
Write-Host "Target Description:" $op_file.ID
Write-Host "NIC Description:" $NetConfigItem.Description
Foreach ($IP in $ResultsIP){Write-Host $IP}
Foreach ($IPSub in $ResultsSubnet) {Write-Host $IPSub}
}
}}
}
Beyond the bad coding practices and other scary stuff I am sure to be hearing about soon, how can I get these arrays combined and prepared to be dumped to CSV? I feel that my issue is more to do with how the data is prepared in this case rather than how the data is written. Though I guess they would both have considerable influence. To be clear, I have tried the psObject stuff without much success. I have yet to find a tutorial on that which wasn't overly complex considering my near complete lack of programming knowledge. That being said, I am not against using that if I can find something that will help me wrap my head around it. Any hints or tips that may push me in the right direction would be very much appreciated.
Upvotes: 0
Views: 388
Reputation: 41
Plz verify if everything is ok with your code before post it.
Foreach ($NetConfigItem in $NetConfig)
{
$ResultsIP = $NetConfigItem.IPAddress
$ResultsSubnet = $NetConfigItem.IPSubnet
$Results = $ResultsIP + $ResultsSubnet
Write-Host "Target Description:" $op_file.ID
Write-Host "NIC Description:" $NetConfigItem.Description
Foreach ($IP in $ResultsIP){Write-Host $IP}
Foreach ($IPSub in $ResultsSubnet) {Write-Host $IPSub}
}
The Results section is inside the Foreach code.
The best way is return a Object, it will give you lots of flexbility, then you can easily manipulate the data as you wish.
Inside the foreach you generate a PSCustomObject
Example
$OutputObject = New-Object psobject
$OutputObject | Add-Member -Name "TargetDescription" -MemberType NoteProperty -Value $op_file.ID
$OutputObject | Add-Member -Name "NICDescription:" -MemberType NoteProperty -Value $NetConfigItem.Description
With IP Addresses i would specify a maximum number valor, like 4 IP Addresses or whatever you need, like:
$OutputObject | Add-Member -Name "IPaddress1" -MemberType NoteProperty -Value $IP
Then just add $OutPutObject to a container declared early like: $OutputContainer = @()
and just add it
$OutputContainer += $OutputObject
Hope this help you.
Best Regard
Upvotes: 1