Solved

Change Powershell Script Output Format

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

16 Experts available now in Live!

Get 1:1 Help Now