alstar
 New Member Posts:17

 |
| 21 Apr 2011 03:28 AM |
|
Hi Guys,
I am trying to run the following cmdlet within powershell and it works fine, its gives me what I need but when I export it to Excel as a .xls file the output format is just horrible coz everything is in one coloumn, is there a way I can export to exel with the data in seperate coloumns?
get-wmiobject win32_bios -computer (cat c:\servers.txt) | ft __server,name,@{label='Release Date';expression={$_.ConvertToDateTime($_.releasedate)}} | out-file c:\server.xls
Can anyone help me out on this? Just to get the formatting correct in Excel otherwise the command let is fine!
PS: I have tried using Format-wide and most of the format cmdlets with no luck :-(
Thanks in Advance A |
|
|
|
|
ChevyNovaLN
 Basic Member Posts:121

 |
| 21 Apr 2011 04:03 AM |
|
Instead of using Format Tabel (FT), use 'Select' and then do an 'export-csv' instead of out-file
Get-WmiObject win32_bios -computer (cat c:\servers.txt) | select __server,name,@{label='Release Date';expression={$_.ConvertToDateTime($_.releasedate)}} | Export-Csv c:\test.csv
|
|
| Brian / ChevyNovaLN |
|
|
alstar
 New Member Posts:17

 |
| 21 Apr 2011 04:09 AM |
|
Yeah I did try the CSV part I should have mentioned that too in my question.. my bad! When i do that I get a junk output in the file which looks like this ================================================= ===#TYPE Microsoft.PowerShell.Commands.Internal.Format.FormatStartData ClassId2e4f51ef21dd47e99d3c952918aff9cd 033ecb2bc07a4d43b5ef94ed5a35d280 9e210fe47d09416682b841769c78b8a3 27c87ef9bbda4f709f6b4002fa4af63c 4ec4f0187cb04f4cb6973460dfe252df cf522b78d86c486691226b40aa69e95c ============================================== I want to use an XLS file rather than a CSV file
|
|
|
|
|
ChevyNovaLN
 Basic Member Posts:121

 |
| 21 Apr 2011 04:25 AM |
|
This will output the data to the screen.... but requires Excel be installed. it opens Excel and populates the data. I just tried this code EXACTLY as is... with c:\servers.txt containing 2 machine names. I can't take credit for the 'out-excel' function below, It was in part of the code i helped someone with yesterday.
function out-excel
{param ([string[]]$property,[switch]$raw)
begin {
# start Excel and open a new workbook
$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
# initialize our row counter and create an empty hashtable
# which will hold our column headers
$Row = 1
$HeaderHash = @{}
}
process {
if ($_ -eq $null) {return}
if ($Row -eq 1) {
# when we see the first object, we need to build our header table
if (-not $property) {
# if we haven’t been provided a list of properties,
# we’ll build one from the object’s properties
$property=@()
if ($raw) {
$_.properties.PropertyNames | %{$property+=@($_)}
} else {
$_.PsObject.get_properties() | % {$property += @($_.Name.ToString())}
}
}
$Column = 1
foreach ($header in $property) {
# iterate through the property list and load the headers into the first row
# also build a hash table so we can retrieve the correct column number
# when we process each object
$HeaderHash[$header] = $Column
$Sheet.Cells.Item($Row,$Column) = $header.toupper()
$Column ++
}
# set some formatting values for the first row
$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 19
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
$WorkBook.HorizontalAlignment = -4108
}
$Row ++
foreach ($header in $property) {
# now for each object we can just enumerate the headers, find the matching property
# and load the data into the correct cell in the current row.
# this way we don’t have to worry about missing properties
# or the “ordering” of the properties
if ($thisColumn = $HeaderHash[$header]) {
if ($raw) {
$Sheet.Cells.Item($Row,$thisColumn) = [string]$_.properties.$header
} else {
$Sheet.Cells.Item($Row,$thisColumn) = [string]$_.$header
}
}
}
}
end {
# now just resize the columns and we’re finished
if ($Row -gt 1) { [void]$WorkBook.EntireColumn.AutoFit() }
}
}
Get-WmiObject win32_bios -computer (cat c:\servers.txt) | select __server,name,@{label='Release Date';expression={$_.ConvertToDateTime($_.releasedate)}} | out-excel
|
|
| Brian / ChevyNovaLN |
|
|
alstar
 New Member Posts:17

 |
| 21 Apr 2011 04:30 AM |
|
I guess the internet is indeed a small place!! :-) I did come across that blog here http://pathologicalscripter.wordpre...out-excel/ and was hoping der would be an alternative solution but let me ask u this I ran the comamnd but the output I get is again just the "Junk" value.. I wish I would attach the output file here so that u could see it :( |
|
|
|
|
ChevyNovaLN
 Basic Member Posts:121

 |
| 21 Apr 2011 04:41 AM |
|
I dont understand how it could be junk.. i ran the same script, exactly as i pasted it, and i got 2 nicely formatted rows with bold headers. (3 rows total)
What does the contents of your c:\servers.txt look like ?
|
|
| Brian / ChevyNovaLN |
|
|
alstar
 New Member Posts:17

 |
| 21 Apr 2011 04:43 AM |
|
My servers.txt has 2 of my servers dxbexch301 dxbfile300 Also I had to change the last but from "Select" to "FT" coz i get an error stating "Select-Object: Illegal Key label" |
|
|
|
|
alstar
 New Member Posts:17

 |
| 21 Apr 2011 04:45 AM |
|
CLASSID2E4F51EF21DD47E99D3C952918AFF9CD PAGEHEADERENTRY PAGEFOOTERENTRY AUTOSIZEINFO SHAPEINFO GROUPINGENTRY 033ecb2bc07a4d43b5ef94ed5a35d280 Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo 9e210fe47d09416682b841769c78b8a3 27c87ef9bbda4f709f6b4002fa4af63c 27c87ef9bbda4f709f6b4002fa4af63c 4ec4f0187cb04f4cb6973460dfe252df cf522b78d86c486691226b40aa69e95c
|
|
|
|
|
alstar
 New Member Posts:17

 |
| 21 Apr 2011 04:52 AM |
|
I have PS Version 2 and if u change the Select-object to Ft u wont get the error message |
|
|
|
|
ChevyNovaLN
 Basic Member Posts:121

 |
| 21 Apr 2011 04:53 AM |
|
Not that i think it matters, but are you running PowerShell v2 or is this still on 1.0 ?
|
|
| Brian / ChevyNovaLN |
|
|
alstar
 New Member Posts:17

 |
| 21 Apr 2011 04:56 AM |
|
I have PS Version 2 and if u change the Select-object to Ft u wont get the error message |
|
|
|
|
ChevyNovaLN
 Basic Member Posts:121

 |
| 21 Apr 2011 04:57 AM |
|
hah! thats the problem. I just ran the same script on a Windows XP box i have sitting next to me that only has Powershell 1.0 and i get the same message you did about the 'illegal key label'. This stuff must require Powershell v2.
|
|
| Brian / ChevyNovaLN |
|
|
ChevyNovaLN
 Basic Member Posts:121

 |
| 21 Apr 2011 05:07 AM |
|
I promise you, Powershell v2 does not get the error regarding 'Illegal key label' . how do I know? :) I just installed Powershell v2 on my XP machine that up until 5 min ago still had v1.0 and was getting the 'illegal key label' when using 'SELECT' AFTER i updated to powershell v2, i ran the same script and now i've got a nice looking spreadsheet. I uninstalled Powershell v2, put it back to Powershell v1.0 and then used the FT instead of SELECT, and i get the same mess of garbage text that you're getting now.
|
|
| Brian / ChevyNovaLN |
|
|
halr9000 PowerShell MVP, Site Admin
 Advanced Member Posts:565

 |
| 21 Apr 2011 05:57 AM |
|
The calculated properties feature had a wierd quirk in powershell v1. Even though the two cmdlets supported the ability to create properties on the fly, the format-table cmdlet used the "label" keyword, and select-object used the "name" keyword. The syntax was the same otherwise. In v2, Microsoft made this more consistent. I haven't looked at your script, but I bet it would work on v1 if you just renamed that keyword. Of course, there are plenty of reasons to use v2. |
|
Community Director, PowerShellCommunity.org Co-host, PowerScripting Podcast Author, TechProsaic |
|
|
ChevyNovaLN
 Basic Member Posts:121

 |
| 21 Apr 2011 06:13 AM |
|
I tried the script using FT on my Powershell v2 install and I still get the garbled unreadable text that the OP was getting. If I switch to select, all is well.... again, assuming you're using v2.
|
|
| Brian / ChevyNovaLN |
|
|
halr9000 PowerShell MVP, Site Admin
 Advanced Member Posts:565

 |
| 21 Apr 2011 07:42 AM |
|
Again, I haven't looked at the original script due to time, but what you guys are seeing is "as designed". You don't ever want to put Format-Table in a pipeline. The purpose of this cmdlet is to alter the appearance of text strictly to be output in the powershell console. The objects that it outputs are not the objects it receives, but rather formatting instructions for the console. All of the Format* cmdlets work this way. The select-object cmdlet however is used to manipulate the objects passed to it as input. It can alter them or change which ones you will see (e.g. "the first ten"). Read the help for the two cmdlets to see more examples of what I'm talking about. HTH |
|
Community Director, PowerShellCommunity.org Co-host, PowerScripting Podcast Author, TechProsaic |
|
|
alstar
 New Member Posts:17

 |
| 22 Apr 2011 12:26 AM |
|
@ChevyNovaLN I am using PS V2.0 (CTP3) version, is that the same one you are using ? coz u seem to be working perfectly fine with the "SELECT" command let and I am struglglin!
|
|
|
|
|
halr9000 PowerShell MVP, Site Admin
 Advanced Member Posts:565

 |
|
alstar
 New Member Posts:17

 |
| 22 Apr 2011 03:32 AM |
|
Allright! Something weird happened.. well atleat I think its Odd :-) .. I un-installed PS V2.0 CTP3 and wanted to install it again so downloaded the install file from the MS website "Update for Windows XP (KB968930)" and tried installaing it but when it finished and I looked under "C:\winnt\system32\windowspowershell" folder I saw the v1.0 folder created?? I thought i had installed V2.0 but wasnt the case. Anyways I ran the script again with the "SELECT' command and I see the beautiful excel sheet which u were talking about Chevy!! I have 3 coloums and 2 rows looking right at me!!! So it works with PS V1.0 and why cant i get v2.0 is beyond me even though i downloaded the correct update! Can we automatically save the excel file to the C: drive when the output is done or does this have to be a manual process?
|
|
|
|
|
alstar
 New Member Posts:17

 |
|
halr9000 PowerShell MVP, Site Admin
 Advanced Member Posts:565

 |
|
alstar
 New Member Posts:17

 |
| 22 Apr 2011 04:09 AM |
|
@halr9000 Thanks! a bunch for clearing that out for me!! I was kinda confused about that for a while :-) Lastly do u know how I can modify the ConvettoDateTime fucntion in PS to strip off the Time in the result and keep only the Date? |
|
|
|
|
ChevyNovaLN
 Basic Member Posts:121

 |
| 22 Apr 2011 04:30 AM |
|
You're not crazy. Powershell v2 is actually installed in the same location as Powershell v1.0 which is completely stupid, but it is what it is.
1.0 and 2.0 are both in c:\%windir%\system32\WindowsPowershell\v1.0\.........
If you want to make sure which version you're running, you can type this:
$host.version
I do not believe this works in v1.0, but if it comes back with a version number, like below, you're running v2.
PS H:\> $host.version
Major Minor Build Revision ----- ----- ----- -------- 2 0 -1 -1
|
|
| Brian / ChevyNovaLN |
|
|
ChevyNovaLN
 Basic Member Posts:121

 |
| 22 Apr 2011 04:36 AM |
|
If you give us an example of your usage of ConvertToDate i can be more specific... Regardless of how you're doing it.... usually it would be something like this, assuming $DateVariable contains your date. $DateVaraible | get-date -f MM-dd-yyyy You can take any 'date' and pipe it to "get-date" and format it however you wish using this.
|
|
| Brian / ChevyNovaLN |
|
|
halr9000 PowerShell MVP, Site Admin
 Advanced Member Posts:565

 |
| 22 Apr 2011 04:50 AM |
|
Guys, it's good manners to create a new thread when you have a new topic rather than to add things onto an existing thread. This helps a lot when others come along later to find information. |
|
Community Director, PowerShellCommunity.org Co-host, PowerScripting Podcast Author, TechProsaic |
|
|
alstar
 New Member Posts:17

 |
| 22 Apr 2011 05:10 AM |
|
@Halr9000 Sure lemme fire up a new thread for this part of my question! Thanks for to suggestion!
|
|
|
|
|
alstar
 New Member Posts:17

 |
| 22 Apr 2011 05:11 AM |
|
*the |
|
|
|
|
alstar
 New Member Posts:17

 |
| 22 Apr 2011 05:11 AM |
|
*the |
|
|
|
|