1

I'm a newcomer to Powershell, and I want to run everything in my Custom Object which $info for all of the computers in $computerName in order to get their information. In this code, I've named them A, B, C for general reference. I tried implementing a foreach loop to write-host of $info so that it would accomplish the task but it's not working. I also tried just just running $info.OSInfo for the CustomObjects, but nothing happens when I run the file in VS Code. Would appreciate your help.

Function Get-SystemInfo {

    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $computerName
    )
    $compSysProps = @{ Property = @('Name','Model','NumberOfProcessors','Manufacturer','Domain','NumberOfLogicalProcessors','TotalPhysicalMemory'); }
    $OsInfoProps = @{ Property = @('Property', 'InstallDate', 'Caption', 'Version');}
    $BiosProps = @{ Property = @('SerialNumber'); }
    $ProcessorProps = @{ Property = @('Name', 'NumberofCores','NumberofLogicalProcessors', 'SocketDesignation');}
    $DiskProps = @{ Property = @('Caption', 'Size', 'FreeSpace'); }


    $computerName = @('A','B','C')

    foreach ($computer in $computerName)
    {
        Write-Host $info
    }


    $info = [PSCustomObject]@{
        OSInfo  = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computerName | Select-Object  @OsInfoProps
        BIOSInfo = Get-CimInstance -ClassName Win32_bios -ComputerName $computerName | Select-Object @BiosProps
        ComputerSystemInfo = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computerName | Select-Object @compSysProps
        ProcessorInfo = Get-CimInstance -Class Win32_processor -ComputerName $computerName| Select-Object @ProcessorProps
        DiskInfo = Get-CimInstance -Class Win32_LogicalDisk -ComputerName $computerName | Select-Object @DiskProps
    }
}
Jojo
  • 11
  • 1

1 Answers1

1

You're calling printing before you set the value for $info, which means that it will be printing nothing. There was also an issue where it was $computerName instead of $computer when getting info. Restructured code in StackOverflow below:

Function Get-SystemInfo {

    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $computerName
    )
    $compSysProps = @{ Property = @('Name','Model','NumberOfProcessors','Manufacturer','Domain','NumberOfLogicalProcessors','TotalPhysicalMemory'); }
    $OsInfoProps = @{ Property = @('Property', 'InstallDate', 'Caption', 'Version');}
    $BiosProps = @{ Property = @('SerialNumber'); }
    $ProcessorProps = @{ Property = @('Name', 'NumberofCores','NumberofLogicalProcessors', 'SocketDesignation');}
    $DiskProps = @{ Property = @('Caption', 'Size', 'FreeSpace'); }


    $computerName = @('A','B','C')

    foreach ($computer in $computerName)
    {
        $info = [PSCustomObject]@{
            OSInfo  = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computer | Select-Object  @OsInfoProps
            BIOSInfo = Get-CimInstance -ClassName Win32_bios -ComputerName $computer | Select-Object @BiosProps
            ComputerSystemInfo = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer | Select-Object @compSysProps
            ProcessorInfo = Get-CimInstance -Class Win32_processor -ComputerName $computer | Select-Object @ProcessorProps
            DiskInfo = Get-CimInstance -Class Win32_LogicalDisk -ComputerName $computer | Select-Object @DiskProps
        }
        Write-Host $info
    }  
}
Riley Carney
  • 804
  • 5
  • 18
  • Also not sure how `$info` handles `$info.toString()` being called on it, if it doesn't process the information well you may need to add your own info to make a clearer write to console. – Riley Carney May 31 '20 at 03:42