Solved

Change Powershell Script Output Format

Posted on 2010-11-17
2
894 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now