Search code examples
powershellpowershell-2.0powershell-3.0powershell-4.0powershell-remoting

Get system information off a remote PC and write it to a local file (Powershell)


So im trying to write a script that collects system info off of a computer on the network, then writes the information collected to a HTML file in my C:\ drive. Here's what I have so far.

Invoke-Command -computername WIN-I9EKPIP774N -credential Administrator -ScriptBlock { 
$OS= (Get-WmiObject -class Win32_OperatingSystem).Caption; 
$PK= powershell "(Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey"; 
$MN= (Get-WmiObject -Class:Win32_ComputerSystem).Manufacturer;  
$SN= Get-WmiObject win32_bios | Select SerialNumber; 
$CPU= Get-WMIObject win32_Processor | select name; 
$RAM= (systeminfo | Select-String 'Total Physical Memory:').ToString().Split(':')[1].Trim(); 
$GPU= Get-WmiObject win32_VideoController|select videoProcessor; Write-host "GPU: " 
    $Result= $GPU, $OS, $PK, $SN, $CPU, $RAM
        $Result|out-file C:/SYSINFO.HTML -computername DESKTOP-TA132FJ}

The command collects the information on the remote computer fine, but im having trouble sending it back to write the info to my local file. I added the machines to trustedhosts respectively, both by IP and hostname, but still cannot get it to connect back to my local machine and write to the file. Any help would be much appreciated.


Solution

  • Turns out if you create a variable and store the invoke command in that variable, then pipe the variable to out-file, you get your results. Also, instead of using the computer name, I ended up adding the systems to the trusted host file by IP address. The resulting code looked like this.

    $OVERALL= Invoke-Command -computername 172.16.50.50 -credential Administrator -ScriptBlock { 
    $OS= (Get-WmiObject -class Win32_OperatingSystem).Caption; 
    $PK= powershell "(Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey"; 
    $MN= (Get-WmiObject -Class:Win32_ComputerSystem).Manufacturer;  
    $SN= Get-WmiObject win32_bios | Select SerialNumber; 
    $CPU= Get-WMIObject win32_Processor | select name; 
    $RAM= (systeminfo | Select-String 'Total Physical Memory:').ToString().Split(':')[1].Trim(); 
    $GPU= Get-WmiObject win32_VideoController|select videoProcessor;
    $Result= $GPU, $OS, $PK, $SN, $CPU, $RAM;
    $Result};
    $OVERALL| Out-file C:\SYSINFO.HTML
    

    This successfully sent a request for sys information from the localhost to a remote machine, and wrote the sys information of the remote host to a html file on the localhost. Cool learning experience!