Search code examples
powershellvariablesscriptingremote-access

Script to capture info from remote AD systems using a list PS 5.1


I am attempting to build a script that will request information (Hostname, MAC, IP, Caption (os version), and serial number using a list of computers pulled from AD.

This works but it creates multiple lines/rows when instead I need all this information on one row. Yes I am a noob at this.. I can write a script for a single machine just fine but getting that same script to work with a list remotely eludes me, this script allows me to get the information but not on the same row.!!

I am using PW version 5.1

Here it is;

Function Get-CInfo {

    $ComputerName = Get-Content C:\Users\scott.hoffman.w.tsc\Desktop\scripts\get-cinfo-tools\comp-list.txt

    $ErrorActionPreference = 'Stop'

    foreach ($Computer in $ComputerName) {

        Try {

            gwmi -class "Win32_NetworkAdapterConfiguration" -cn $Computer | ? IpEnabled -EQ "True" | select DNSHostName, MACAddress, IPaddress | FT -AutoSize 
            gwmi win32_operatingsystem -cn $computer | select Caption | FT -Autosize
            Get-WmiObject win32_bios -cn $computer | select Serialnumber | FT -Autosize

        }

        Catch {

            Write-Warning "Can't Touch This : $Computer"

        }

    }#End of Loop

}#End of the Function

Get-CInfo > comp-details.txt

the comp-list.txt file is just;

computername01 computername02

I would love to use csv from input to output but I get lost.

Thanks for your help/input/kick in the pants!


Solution

  • Do yourself a huge favour and learn how to create custom objects:

    # Function is more useful if you remove specific filepaths from inside it
    
    # Using a parameter and set it to accept pipeline input
    Function Get-CInfo {
        [CmdletBinding()]
        Param (
            [parameter(Mandatory = $true,ValueFromPipeline = $true)]$ComputerList
        )
        Begin {
            $ErrorActionPreference = 'Stop'
            Write-Host "Processing:"
        }
        Process {
            foreach ($Computer in $ComputerList) {
                Try {
                    Write-Host $Computer
            # Gather data
                    $NetAdapter = gwmi -class "Win32_NetworkAdapterConfiguration" -cn $Computer | ? IpEnabled -EQ "True" | select DNSHostName, MACAddress, IPaddress
                    $OStype = gwmi win32_operatingsystem -cn $computer | select Caption
                    $Serial = Get-WmiObject win32_bios -cn $computer | select Serialnumber
            # Output custom object with required properties
                    [pscustomobject]@{
                        Computer = $Computer
                        DNSHostName = $NetAdapter.DNSHostName;
                        MACAddress = $NetAdapter.MACAddress;
                        IPAddress = $NetAdapter.IPAddress;
                        OperatingSystem = $OSType.Caption;
                        Serial = $Serial.Serialnumber;
                        Error = ''
                    }
    
                }
                Catch {
                # Within the catch section $_ always contains the error.
                     [pscustomobject]@{
                        Computer = $Computer
                        DNSHostName = '';
                        MACAddress = '';
                        IPAddress = '';
                        OperatingSystem = '';
                        Serial = '';
                        Error = $_.Exception.Message
                    }
                }
            }#End of Loop
        }
        End {
            Write-Host "Done"
        }
    }#End of the Function
    
    # Pipe list to function and store to '$Results'
    $Results = Get-Content C:\Users\scott.hoffman.w.tsc\Desktop\scripts\get-cinfo-tools\comp-list.txt | Get-CInfo
    
    # Output and formatting should almost always be the last thing you do
    # Now that you have an object ($Results) with your data, you can use it however you like
    
    # Format and output to text file
    $Results | ft -AutoSize > comp-details.txt
    # Or send to csv
    $Results | Export-Csv -Path comp-details.csv -NoTypeInformation