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

Time:11-10

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!

CodePudding user response:

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

CodePudding user response:

Thanks to #Scepticalist!

Here is the PS script with which I read a text file line by line into a variable:

text

    ComputerName01
    ComputerName02

Script

function Get-TimeStamp {return "[{0:HH:mm:ss}]" -f (Get-Date)}

$StartTime = Get-Date -Format 'yyyy/MM/dd    HH:mm:ss'

# 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 ""
    Write-Host "Processing now: $StartTime"
}
Process {
    foreach ($Computer in $ComputerList) {
        Try {
            Write-Host "$(Get-TimeStamp) Working on machine: $Computer"
    # Gather data
            $NetAdapter = gwmi -class "Win32_NetworkAdapterConfiguration" -cn $Computer | ? IpEnabled -EQ "True" | select 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;
                
                # Here is the line that I added [0] to the end

                IPAddress = $NetAdapter.IPAddress[0];
                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 ""
Write-Host "*****"
Write-Host ""
Write-Host "Done"
Write-Host ""
}
}#End of the Function

# Pipe list to function and store to '$Results'
$Results = Get-Content .\comp-list.txt | Get-CInfo

# Output and formatting

# Format and output to text file
$Results | ft -AutoSize > comp-details.txt


# Or send to csv
$Results | Export-Csv -Path comp-details.csv -NoTypeInformation

# Output results to console
Get-Content -Path .\comp-details.csv

Here is the CSV output (redacted):

"Computer","MACAddress","IPAddress","OperatingSystem","Serial","Error"
"ComputerName001","xx:xx:xx:xx:xx:xx","123.456.789.000","Microsoft Windows 11 Enterprise","JJJJJJJ",""
  • Related