Share via

Help me write script? for collect information.

Alexandr Serkin 21 Reputation points
2022-06-20T11:58:10.923+00:00

Hello. Help write a script. It is necessary to collect information about hardware from computers, memory, CPU, disk type. Ideally, get a plate with 4 columns

Name, CPU, RAM, HDD, No connect

$win7pc = (Get-ADComputer -SearchBase "OU=Computers,DC=AAA,DC=local" -Filter * -Properties *| where {$_.operatingsystem -like "7"}).name # Collect all computers from OU with WIN7
foreach ($win7pc in $win7pc)
{if ((Test-Connection $win7pc -Count 1 -quiet -WarningAction SilentlyContinue) -eq $True) # Check availability
{Invoke-Command -ComputerName $win7pc -ScriptBlock {Get-WmiObject -Class Win32_DiskDrive} | select model| export-csv path ???}
Else {Write-Host $win7pc -Separator "," -ForegroundColor Red "noconnection" | export-csv path ???}
}

CPU - Get-WmiObject -Class "win32_processor" | select name

RAM - (Get-WmiObject Win32_PhysicalMemory).capacity

Thanks.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2022-06-20T15:16:07.46+00:00

This isn't the most efficient way to do this:

Get-ADComputer -SearchBase "OU=Computers,DC=AAA,DC=local" -Filter -Properties |   
    Where-Object { $_.operatingsystem -like "*7*" } |  
        Select-Object -Expand name |                    # Collect all computers from OU with WIN7  
            ForEach-Object {  
                $r =    [PSCustomObject]@{  
                            Name = $_  
                            CPU = ""  
                            RAM = ""  
                            HDD = ""  
                            OnLine = $false  
                        }  
                if ( (Test-Connection $_ -Count 1 -Quiet -WarningAction SilentlyContinue)) { # Check availability  
                    $r.Online = $true  
                    $r.HDD = (Get-WmiObject -Computer $_ -Class Win32_DiskDrive | Select-Object Expand model)  
                    $r.CPU = (Get-WmiObject -Computer $_ -Class win32_processor | Select-Object Expand name)  
                    $r.RAM = (Get-WmiObject -Computer $_ -Class Win32_PhysicalMemory | Select-Object Expand capacity)  
                    $r  
                }  
                Else {  
                    $r  
                }  
            } | Export-CSV x:dir\name.csv -NoTypeInformation  

A better way to do this would be to use Invoke-Command and provide an array of machines as the argument to the -Computer parameter. The script block would (as in the code above) would return all the information. You would have to redirect then ERROR stream on the Invoke-Command (e.g., 2>&1) and check each of the returned array elements to see if it was an [ErrorRecord] object to identify a machine that cannot be contacted. All the other items in the array would have the information you seek.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alexandr Serkin 21 Reputation points
    2022-06-20T19:02:11+00:00

    Thank you Rich. You gave me the main idea of the solution. I fixed and modernized the script for me. My script looks like this:

    Get-ADComputer -SearchBase "OU=Computers,DC=AAA,DC=local" -Filter * -Properties * |
    Where-Object { ($.operatingsystem -like "*7*") -and ($.whenchanged -gt "5/1/2022") } |
    Select-Object -Expand name | # Collect all computers from OU with WIN7 and Turn On after 5/1/2022
    ForEach-Object {
    $r = [PSCustomObject]@{
    Name = $_
    CPU = ""
    RAM = ""
    HDD = ""
    OnLine = $false
    }
    if ( (Test-Connection $_ -Count 1 -Quiet -WarningAction SilentlyContinue)) { # Check availability
    $r.Online = $true
    $r.HDD = Invoke-Command -ComputerName $_ -scriptblock {Get-WmiObject -Class Win32_DiskDrive | Select-Object -ExpandProperty model}
    $r.CPU = Invoke-Command -Computername $_ -scriptblock {Get-WmiObject -Class win32_processor | Select-Object -ExpandProperty name}
    $r.RAM = Invoke-Command -Computername $_ -scriptblock {(Get-WmiObject -Class Win32_PhysicalMemory | Select-Object -ExpandProperty capacity| measure -Sum).sum / 1GB}
    $r
    }
    Else {
    $r
    }
    } | Export-CSV 'PATHTOFILE' -NoTypeInformation

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.