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.
$NetConfigItembefore it's defined. Are you defining it outside the function?