powershell - Log output of ForEach loop -


the code below gets computer info remotely. couldn't send output log file. also, how log unqueried computers in separate log file?

   code:      $arrcomputers =  gc .\computernames.txt    clear-host   foreach ($computer in $arrcomputers)    {  $computersystem = get-wmiobject win32_computersystem -computer $computer $computerbios = get-wmiobject win32_bios -computer $computer $version = get-wmiobject -namespace "root\cimv2" -query "select * win32_computersystemproduct" -computer $computer | select version    write-host "system information for: " $computersystem.name   -backgroundcolor darkcyan     "-------------------------------------------------------"      "model: " + $computersystem.model     "serial number: " + $computerbios.serialnumber     "version: " + $version      ""     "-------------------------------------------------------"        } 

logging straightforward. need store output in variable , use out-file cmdlet:

$arrcomputers = gc .\computernames.txt $outputlog = ".\output.log" # main log $notrespondinglog = ".\notresponding.log" # logging "unqueried" hosts  $erroractionpreference = "stop" # or add '-ea stop' get-wmiobject queries clear-host  foreach ($computer in $arrcomputers)  {     try     {         $computersystem = get-wmiobject win32_computersystem -computer $computer         $computerbios = get-wmiobject win32_bios -computer $computer         $version = get-wmiobject -namespace "root\cimv2" `             -query "select * win32_computersystemproduct" `             -computer $computer | select -expandproperty version     }     catch     {         $computer | out-file -filepath $notrespondinglog -append -encoding utf8         continue     }      $header = "system information for: {0}" -f $computersystem.name      # outputting , logging header.     write-host $header -backgroundcolor darkcyan     $header | out-file -filepath $outputlog -append -encoding utf8      $output = (@" -------------------------------------------------------  model: {0}  serial number: {1}  version: {2}   -------------------------------------------------------  "@) -f $computersystem.model, $computerbios.serialnumber, $version      # ouputting , logging wmi data     write-host $output     $output | out-file -filepath $outputlog -append -encoding utf8 } 

Comments