0

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.

3
  • 1
    In the current version of your code you're using $NetConfigItem before it's defined. Are you defining it outside the function? Commented Jun 18, 2012 at 23:31
  • This is the entire project. I am basically creating this function as an independent file as it was originally supposed to be used as a commandline tool for snagging remote IP information. It has since morphed to a mass info gathering tool for groups of machines. As for using $NetConfigItem before it is defined, I had not noticed that and will see about cleaning that up. Thanks for pointing that out Andy. Commented Jun 19, 2012 at 14:16
  • I see where the confusion came from now. I said it was the stable portion of the code in the original question. I simply meant that I had torn out all of the crap that I had tried that didn't work and gotten back to where the code did what I expected. Commented Jun 19, 2012 at 14:26

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

This seems to be closer to what I was looking for. I will give this a shot and post the results back here. Thanks.
This worked! I am still unable to get the full list of IP/SUB pairs to go to the CSV though. I get the first pair, but it seems that my for loop to add the additional pairs is not working. For ( $s=0; $s -lt $CountRange; $s++ ) {$OutputObject | Add-Member -Name "IPSubnet" -MemberType NoteProperty -Value $IPSub} This caused issues because the object type of $CountRange was not an int. My ForEach attempt to dump this in throws some Add-Member hinkiness. Regardless, I consider this question answered and will follow up with another if need be. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.