user22401
user22401

Reputation: 5

PowerShell and CSV

I'm working on script which collects information about computer (os version, software installed etc.) and I would like to get output in CSV file to be able to import in SQL later.

Currently, I have something like this:

$os = Get-WmiObject Win32_OperatingSystem 
$soft = Get-WmiObject Win32_Product

$customerid = $env:UserName
$osversion = $os.Caption
$osplatform = $os.OSArchitecture

$ossp = $os.servicepackmajorversion
$timestamp = Get-Date -Format "dd.MM.yyyy HH:mm"
 $timestamp,$customerid, $osversion, $osplatform, $ossp, $soft.Name, $soft.Version  | Out-File C:\test.csv

I would prefer output file something like this:

http://s9.postimg.org/5zmwoda4f/image.png

but at the moment I'm gettting all information this way:

http://s12.postimg.org/9gsrvdfz1/image.png

How can I achieve output like in the first image?

Thank you for any help!

Upvotes: 0

Views: 269

Answers (1)

Matt
Matt

Reputation: 46730

What you are seeing is how the pipe is dealing with arrays. To get the output you desire we need to make some other changes since you didn't make any effort to add titles and what not. This output deviates slightly from what you desire since it is easier to have the software title on its own line.

$os = Get-WmiObject Win32_OperatingSystem 
$soft = Get-WmiObject Win32_Product

$customerid = $env:UserName
$osversion = $os.Caption
$osplatform = $os.OSArchitecture
$ossp = $os.servicepackmajorversion
$timestamp = Get-Date -Format "dd.MM.yyyy HH:mm"
$softwareDetails = ($soft | ForEach-Object{",$($_.Name),,,,,$($_.Version)"})


"Time:,$timestamp",
"UserName:,$customerid",
"OS Version,$osversion",
"Architecture,$osplatform",
"Service Pack,$ossp",
"Software Versions",
$softwareDetails | Out-File C:\test.csv

Since we are using arrays for output each entry will be on it's own line. I would have suggested using Export-CSV but your desired output does not match a traditional csv file.

Upvotes: 1

Related Questions