ConvertTo-Html is not displaying data correctly - powershell

I have a very simple script where I am listing all the websites on my server. I am using this output to create custom columns in Format-Table to show required information. Then I want to represent this whole info in a HTML file using ConvertTo-Html cmdlet.
But when I run below code, it just creates garbage. However, if I do an Out-File to a .txt (no HTML conversion), it records correct information. Also, if I only display the results on screen, it still shows correct information.
cls
Import-Module WebAdministration
Get-Website |
Format-Table #{n = 'Site_Name'; e = {$_.Name}},
#{n = 'Physical Path'; e = {$_.physicalpath}},
#{n = 'Version'; e = {($_.physicalpath.split("\"))[-1]}} -AutoSize |
ConvertTo-Html |
Out-File -FilePath D:\test.html
Any help will be greatly appreciated.

Format-Table creates formatted output that is suitable for displaying it in the console, but not for passing it to ConvertTo-Html.
Replace Format-Table with Select-Object:
Get-Website |
Select-Object #{n='Site_Name';e={$_.Name}},
#{n='Physical Path';e={$_.physicalpath}},
#{n='Version';e={($_.physicalpath.split("\"))[-1]}} |
ConvertTo-Html |
Out-File -FilePath D:\test.html
and the problem will disappear.
You can see the difference if you pass the Format-Table and the Select-Object output into Get-Member:
... | Format-Table ... -AutoSize | Get-Member
... | Select-Object ... | Get-Member

Related

Keeping output on the same line

I want to keep the output of this command on the same line:
((Get-ADUser -filter {employeetype -eq "Employee"}).SamAccountName) | Sort-Object | Get-ADUser | ForEach-Object {$_.Name,$_.Department,$_mail}
Currently the output shows up like this:
James Roberts
Accounting
jroberts#email.tld
But, I need to have it show up like:
James Roberts Accounting jroberts#email.tld
I've also tried using (based on a suggestion I found):
((Get-ADUser -filter {employeetype -eq "Employee"}).SamAccountName) | Sort-Object | Get-ADUser | ForEach-Object {$_.Name;$_.Department;$_mail}
but, I get the same three lines of output and not one line.
If you want to create customized objects:
… | Select-Object -Property Name,Department,Mail
If you want to make strings:
… | ForEach-Object { $_.Name,$_.Department,$_.Mail -join " " }
If you just want to display the table nicely (not going to use this output afterwards):
… | Format-Table -Property Name,Department,Mail -AutoSize

Append results or text to text file by using Powershell

I have a collection of objects with properties: ProductName and PartName. The content of collection is output to a file first:
$colProducts | sort-object ProductName | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-File myProducts.txt
So far so good. However, I have trouble to append a text message to the result file like this:
Add-Content myProducts.txt "`nParts in more than one Product`n"
I found that the appended text is not readable at the end. One thing I notice is that the output of the first collection to a file is in Unicode, and the second one code (add-content) is in ASCII if only to a new file.
After this, I would like to continue to add the following information the same result file:
$colProducts | Group-object PartName | sort-object PartName | `
Where-Object {$_.Count -gt 1 } | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-File myProducts.txt
The above codes will overwrite to the result file. I need to append to the file. Greatly appreciate help!
Update: It is good to know -Append option. How about Add-Content? It seems adding some unreadable chars to the file after Out-File from collection.
I would first try:
$colProducts | Group-object PartName | sort-object PartName | `
Where-Object {$_.Count -gt 1 } | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-File -Append myProducts.txt
And then look at this to get a feel for what you were encountering.
Essentially, Out-File (and Out-File -Append) gives you Unicode by default and Add-Content gives ASCII by default. My advice would be stick to the same command and you shouldn't have a problem.
And, of course, help Out-File -Detailed! Always check out the powershell examples because they help a great deal, not just to figure out their common usage, but to grok them as well.
Try:
$colProducts | Group-object PartName | sort-object PartName | `
Where-Object {$_.Count -gt 1 } | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-File -Append myProducts.txt
Another option:
$colProducts | sort-object ProductName | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-String | Add-Content myProducts.txt
Add-Content myProducts.txt "`nParts in more than one Product`n"

Object Value not exporting to CSV file - Powershell

(Updated Title as I wrote it on the fly)
Got very stumped by this so far and relatively new to powershell.
I have the below script:
cd e:
Import-CSV e:\UserList.csv | Foreach-Object {Get-MailboxFolderPermission -Identity "$($_.account):\Kalender" -User Default}
| select-object User,AccessRights | export-csv AccessList.csv
However whenever I run it I get the Object Type returned for the AccessRights parameter e.g.:
User,"AccessRights"
Default,"System.Collections.ObjectModel.Collection`1[Microsoft.Exchange.Management.StoreTasks.MailboxFolderAccessRight]"
Default,"System.Collections.ObjectModel.Collection`1[Microsoft.Exchange.Management.StoreTasks.MailboxFolderAccessRight]"
Anyone know how I can get the value out as it prints to powershell? e.g.:
User AccessRights
---- ------------
Default {AvailabilityOnly}
Not quite sure why it's behaving that way, but this seems to be a usable workaround:
Import-CSV e:\UserList.csv |
Foreach-Object {
Get-MailboxFolderPermission -Identity "$($_.account):\Kalender" -User Default
} | select-object User,#{l='AccessRights';e={$_.AccessRights}} |
export-csv AccessList.csv

format-table outputting blank?

What I'm trying to do is look at some processes and get a list of the user of these processes. ANd the following code worked fine for me.
get-wmiobject win32_process |
where{$_.name -like "*notepad*"}|sort {$_.CommandLine}|
select #{n="User";e={$_.getowner().user}},#{n="ProcessID";e={$_.ProcessID}},{$_.CommandLine} |ft -AutoSize |Out-String -Width 300 >> C:\ListUsers.txt
Somehow I wanted to split the $_.CommandLine string in the middle of way, and output some of the split arrary(see the following code for a better idea, although the code is wrong). But the updated code just output nothing into the text file. I think I must be using the select-object or fommat-object wrong, but i don't know how i can fix it.
get-wmiobject win32_process |
where{$_.name -like "*notepad*"}|sort {$_.CommandLine}|
%{
$split = $_.CommandLine.split("\")
select #{n="User";e={$_.getowner().user}},#{n="ProcessID";e={$_.ProcessID}},#{n="Ihub";e=$split[3]},#{n="version";e=$split[3]},#{n="version";e=$split[3]} |
ft -AutoSize |Out-String -Width 300 >> C:\ListUsers.txt
}
Can anyone advise? Thanks!!
You don't need to use select and then format-table. Format-table can create calculated properties too. Also, you forgot to wrap the $split[3] in a scriptblock. I removed the two "version" properties because they were identical to "Ihub".
Try this(untested):
get-wmiobject win32_process |
where{$_.name -like "*notepad*"}|sort {$_.CommandLine} |
Format-Table -Property #{n="User";e={$_.getowner().user}},#{n="ProcessID";e={$_.ProcessID}},#{n="Ihub";e={($_.CommandLine.split("\"))[3]}} -AutoSize |
Out-String -Width 300 >> ListUsers.txt

How to get Export-Csv to output same information as screen output?

Get-WmiObject -list | where-object {$_.name -match "win32"} | Select-Object
name,methods,properties
This displays the name, methods and properties of each Win32 class and I wanted to get this information into a CSV file.
The following however outputs doesn't output the same information.
Get-WmiObject -list | where-object {$_.name -match "win32"} | Select-Object
name,methods,properties | Export-CSV "c:\output.csv"
How can I do this?
(Updated my script as it had an error.)
You need to do some extra manual work and make sure you expand the names and join them by some delimiter:
$methods = #{n='Methods';e={ ($_.Methods | select -expand Name) -join ';'}}
$properties = #{n='Properties';e={ ($_.Properties | select -expand Name) -join ';'}}
Get-WmiObject -List |
Where-Object {$_.Name -like "Win32_*"} |
Select-Object Name,$methods,$properties |
Export-Csv .\win32.csv -NoTypeInformation
The problem here is that each WMI Object has properties that themselves are arrays and Output-CSV can't really handle that.
To fix this, you'd need to handle the arrays of arrays explicitly.
WHat specifically do you want to be output?