Solved

Change Powershell Script Output Format

Posted on 2010-11-17
2
898 Views
Last Modified: 2012-05-10
Hi there,

I´ve written a Powershell script that gives me back the installed OS, installed service pack, processor architecture and shows of SNMP installed or not. This script works for me so far, but the output in a textfile is not like I would need it to be (I want to import it to Excel).

Can someone point out what needs to be changed to get a good table-based output?
Code is attatched, the script is started as ./getos.ps1 >> "D:\testscript.txt"

Regards
Steffen

$arrComputers = get-Content -path "c:\servers.txt"

foreach ($strComputer in $arrComputers)
{
 $collItems = Get-WmiObject -class "Win32_OperatingSystem" -namespace "root\CIMV2" -computername $strComputer
 foreach ($objItem in $collItems)
 {
   "Computer Name: ;" + $strComputer
   "Operating System: ;" + $objItem.Caption
   "Servicepack: ;" + $objItem.ServicePackMajorVersion
   " "
 }
 
 $collSNMP = Get-WmiObject -class "Win32_service" -namespace "root\CIMV2" -computername $strComputer | Where-Object {$_.Name -eq "SNMP"}
 foreach ($objItem in $collSNMP)
 {
   "SNMP: ;" + $objItem.Name
   
  " "
 }
 
 $collProcArch = Get-WmiObject -class "Win32_processor" -namespace "root\CIMV2" -computername $strComputer | Select-Object Caption
 foreach ($objItem in $collProcArch)
 {
  "Architecture: ;" + $objItem.Caption
   }
  " " 
  "--------------------------------------------------------------"
}

Open in new window

0
Comment
Question by:SSR-IS
2 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 34156338
This will give you a CSV file, which will pop open in Excel nicely enough.

HTH

Chris
Get-Content -path "c:\servers.txt" | ForEach-Object {

  $ComputerName = $_

  Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName | Select-Object `
    @{n='Computer Name';e={ $ComputerName }},
    @{n='Operating System';e={ $_.Caption }},
    @{n='Servicepack';e={ $_.ServicePackMajorVersion }},
    @{n='SNMP';e={ Get-WmiObject Win32_service -ComputerName $ComputerName -Filter "Name='SNMP'" | Select-Object -ExpandProperty Name }},
    @{n='Architecture';e={ Get-WmiObject Win32_Processor -ComputerName $ComputerName | Select-Object -ExpandProperty Caption }}

} | Export-Csv "testscript.csv" -NoTypeInformation

Open in new window

0
 

Author Closing Comment

by:SSR-IS
ID: 34162212
Thanks a lot Chris-Dent! Perfect output-file for my pruposes!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question