Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

writing output to log file from gwmi

14 views
Skip to first unread message

RG

unread,
Nov 5, 2009, 1:22:01 AM11/5/09
to

I'm trying to output the results from the following powershell script to a
log file:

$strComputer = Read-Host "Enter Machine name"
$strDate = Get-Date -Format M.d.yyyy
$csvFilePath = 'C:\'
$strLog = New-Item -ItemType file $csvFilePath\$strComputer-$strDate.log
-Force
$software = gwmi -Namespace root\CIMV2 -Class Win32_Product -ComputerName
$strComputer | Select Name, Version
Add-Content $strLog $software
What it places in the log file is the following :

@{Name=VMware Tools; Version=3.1.1.3705}
@{Name=MSXML 6 Service Pack 2 (KB954459); Version=6.20.1099.0}
@{Name=Microsoft .NET Framework 3.0 Service Pack 1; Version=3.1.21022}
@{Name=Microsoft .NET Framework 2.0 Service Pack 1; Version=2.1.21022}
@{Name=Quarantine Server; Version=3.5.0}
@{Name=Reporting Server (Symantec Corporation); Version=1.0.197.0}
@{Name=CorpServerTuner; Version=6.0}
@{Name=Windows Rights Management Client Backwards Compatibility SP2;
Version=5.2.70}
@{Name=Microsoft SQL Server Desktop Engine; Version=8.00.2039}
@{Name=Windows Presentation Foundation; Version=3.0.6920.0}

How do I get rid of the @{} stuff. I would just like to show the name of
the software and the version number.


01MDM

unread,
Nov 5, 2009, 4:45:26 AM11/5/09
to

foreach ( $i in $software ) { add-content $strLog ($i.name+"`t"+
$i.version) }

Martin Zugec

unread,
Nov 5, 2009, 6:23:48 AM11/5/09
to
+1 for solution from 01MDM. I always prefer this (more .NET native
format) of code.

Powershell is sometimes little bit too intelligent ;)

Martin

RichS [MVP]

unread,
Nov 5, 2009, 4:43:03 PM11/5/09
to
You could try this


Get-WmiObject -Class Win32_Product | Select Name, version | Export-Csv
sw.txt -NoTypeInformation
Import-Csv sw.txt

Change the file names to yours

--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk

RG

unread,
Nov 5, 2009, 5:26:06 PM11/5/09
to
You can't append informaton to a file using Export-Csv correct? I have a
bunch of other information that I'm collecting in the script that is being
placed into the file and I didn't see how I could use the Export-Csv since
this isn't a one-line type of script.

Martin Zugec

unread,
Nov 5, 2009, 5:49:02 PM11/5/09
to
You can also append - just use -NoClobber switch parameter and it won't
overwrite your file

Martin

"RG" <R...@discussions.microsoft.com> wrote in message
news:34B355C8-F521-4B39...@microsoft.com...

RichS [MVP]

unread,
Nov 6, 2009, 8:16:01 AM11/6/09
to
OK then

this will work

$data = Get-WmiObject -Class Win32_Product | Select Name, version | out-string
add-content sw.txt $data

but some of the long names will be truncated

if you want the full name could try something like this

Get-WmiObject -Class Win32_Product | Select Name, version | foreach {
$data = "{0},{1}" -f $($_.Name), $($_.version)
Add-Content sw.txt $data

0 new messages