I have this code below, when I run it the output file has additional columns like Comment, RowError, RowState, Table, ItemArray, HasErrors
$dataSet.Tables | Select-Object -Expand Rows |
ConvertTo-HTML -head $a –body $body |
Out-File $OutputFile
I want to get rid of these columns but dont know how to. I have checked other online resources but couldn't help. Is there anything am doing wrong here??
Try to exclude the columns you don't want with -ExcludeProperty:
$dataSet.Tables |
Select-Object -Expand Rows |
Select-Object * -ExcludeProperty Comment, RowError, RowState, Table, ItemArray, HasErrors |
ConvertTo-HTML -head $a –body $body |
Out-File $OutputFile
You will be better off to just include the properties you want than exclude the once you don't want:
$dataSet.Tables | Select-Object -Expand Rows | Select-Object Colum1, Colum2 |
ConvertTo-HTML -head $a –body $body | Out-File $OutputFile
Using the -Property parameter works for me
$dataSet.Tables | Select-Object -Expand Rows |
ConvertTo-HTML -head $a –body $body -Property Name,Date,Address |
Out-File $OutputFile
Related
I have a txt files (semicolon separated) containing over 3 million records where columns 1 to 4 have some general information. Columns 5 and 6 have detailed information. There can be up to 4 different detailed information for the same general information in columns 1 to 4.
My sample input:
Server;Owner;Company;Username;Property;Value
Srv1;Dave;Sandbox;kwus91;Memory;4GB
Srv1;Dave;Sandbox;kwus91;Processes;135
Srv1;Dave;Sandbox;kwus91;Storage;120GB
Srv1;Dave;Sandbox;kwus91;Variant;16
Srv2;Pete;GWZ;aiwq71;Memory;8GB
Srv2;Pete;GWZ;aiwq71;Processes;234
Srv3;Micael;P12;mxuq01;Memory;16GB
Srv3;Micael;P12;mxuq01;Processes;239
Srv3;Micael;P12;mxuq01;Storage;160GB
Srv4;Stefan;MTC;spq61ep;Storage;120GB
Desired output:
Server;Owner;Company;Username;Memory;Processes;Storage;Variant
Srv1;Dave;Sandbox;kwus91;4GB;135;120GB;16
Srv2;Pete;GWZ;aiwq71;8GB;234;;
Srv3;Micael;P12;mxuq01;16GB;239;160GB;
Srv4;Stefan;MTC;spq61ep;;;120GB;
If a values doesn't exist for general information (Columns 1-4) it has to stay blank.
My current code:
$a = Import-csv .\Input.txt -Delimiter ";"
$a | FT -AutoSize
$b = #()
foreach ($Server in $a.Server | Select -Unique) {
$Props = [ordered]#{ Server = $Server }
$Owner = ($a.where({ $_.Server -eq $Server})).Owner | Select -Unique
$Company = ($a.where({ $_.Server -eq $Server})).Company | Select -Unique
$Username = ($a.where({ $_.Server -eq $Server})).Username | Select -Unique
$Props += #{Owner = $Owner}
$Props += #{Company = $Company}
$Props += #{Username = $Username}
foreach ($Property in $a.Property | Select -Unique){
$Value = ($a.where({ $_.Server -eq $Server -and
$_.Property -eq $Property})).Value
$Props += #{ $Property = $Value }
}
$b += New-Object -TypeName PSObject -Property $Props
}
$b | FT -AutoSize
$b | Export-Csv .\Output.txt -NoTypeInformation -Delimiter ";"
After a lot of trying and getting errors: My script works.
But it takes a lot of time.
Is there a possibility to make performance better for around 3 Million lines in txt file? I'm calculating with more or less 2.5 Million unique values for $Server.
I'm running Windows 7 64bit with PowerShell 4.0.
try Something like this:
#Import Data and create empty columns
$List=import-csv "C:\temp\file.csv" -Delimiter ";"
#get all properties name with value not empty
$ListProperty=($List | where Value -ne '' | select property -Unique).Property
#group by server
$Groups=$List | group Server
#loop every rows and store data by group and Property Name
$List | %{
$Current=$_
#Take value not empty and group by Property Name
$Group=($Groups | where Name -eq $Current.Server).Group | where Value -ne '' | group Property
#Add all property and first value not empty
$ListProperty | %{
$PropertyName=$_
$PropertyValue=($Group | where Name -eq $PropertyName | select -first 1).Group.Value
$Current | Add-Member -Name $PropertyName -MemberType NoteProperty -Value $PropertyValue
}
$Current
} | select * -ExcludeProperty Property, Value -unique | export-csv "c:\temp\result.csv" -notype -Delimiter ";"
I'm using this code which I have modified to remove some items that I don't need and I am trying to combine the output into a single table. I have gotten this far:
$SCVMs | ForEach-Object {
$VMName = Get-SCVirtualMachine $_.Name | Select -Expand Name
$ReportData = $ReportData + (Get-SCVirtualMachine $_.Name |
Get-SCVirtualHardDisk |
Select #{Label="VM Name";Expression={$VMName}},
#{Label="VHD Name";Expression={$_.Name}},
#{Label="VHD Location";Expression={$_.Location}},
#{Label="Max Disk Size (GB)";Expression={($_.MaximumSize/1GB)}},
#{Label="Disk Space Used (GB)";Expression={"{0:N2}" -f ($_.Size/1GB)}},
#{Label="Disk Space Used (%)";Expression={[math]::Round((($_.Size/1GB)/($_.MaximumSize/1GB))*100)}},
#{Label="Free Disk Space (GB)";Expression={"{0:N2}" -f (($_.MaximumSize/1GB) - ($_.Size/1GB))}} |
ConvertTo-Html -as Table -Fragment)
}
The report displays the hard drives for a particular VM in the same table but it creates a separate table for each VM. I would like to generate a single table for all the VMs with a separate row for each hard drive in the VM.
I believe the trick lies in how I select the objects and pipe them along but I'm just not experienced enough to see how to do it.
I don't need the report in HTML, CSV would be fine.
You pipe each object into ConvertTo-Html, so you get a table fragment for each object. Instead of doing that (and appending in a loop on top of that) put ConvertTo-Html outside the ForEach-Object loop.
Change this:
$SCVMs | ForEach-Object {
$VMName = ...
$ReportData = $ReportData + (Get-SCVirtualMachine $_.Name |
Get-SCVirtualHardDisk |
Select ... |
ConvertTo-Html -as Table -Fragment)
}
into this:
$ReportData = $SCVMs | ForEach-Object {
$VMName = ...
Get-SCVirtualMachine $_.Name |
Get-SCVirtualHardDisk |
Select ...
} | ConvertTo-Html -as Table -Fragment
or this (if you need to append to $ReportData):
$ReportData += $SCVMs | ForEach-Object {
$VMName = ...
Get-SCVirtualMachine $_.Name |
Get-SCVirtualHardDisk |
Select ...
} | ConvertTo-Html -as Table -Fragment
To switch to CSV output you just replace ConvertTo-Html with ConvertTo-Csv or Export-Csv.
Just take out the intermediate variables and remove the ConvertTo-Html from inside your loop. Better still export direct to CSV. Something like this:
$SCVMs | ForEach-Object {
$VMName = Get-SCVirtualMachine $_.Name | Select -Expand Name
$ReportData = $ReportData + (Get-SCVirtualMachine $_.Name | Get-SCVirtualHardDisk | Select `
#{Label="VM Name";Expression={$VMName}}, `
#{Label="VHD Name";Expression={$_.Name}}, `
#{Label="VHD Location";Expression={$_.Location}}, `
#{Label="Max Disk Size (GB)";Expression={($_.MaximumSize/1GB)}}, `
#{Label="Disk Space Used (GB)";Expression={"{0:N2}" -f ($_.Size/1GB)}}, `
#{Label="Disk Space Used (%)";Expression={[math]::Round((($_.Size/1GB)/($_.MaximumSize/1GB))*100)}}, `
#{Label="Free Disk Space (GB)";Expression={"{0:N2}" -f (($_.MaximumSize/1GB) - ($_.Size/1GB))}} | ConvertTo-HTML -as Table -Fragment) }
I need to decide the columns' orders of my table. My actual command is that one:
$tab | Sort-Object "Pourcentage" -Descending |
Format-Table -AutoSize |
Out-String -Width 4000 |
Out-File -Append O:\sigdci\public\parcoursArborescence\bilanAnalyse.txt
It gives me that order:
Derniere modification Categorie recherchee Dernier acces Dossier Pourcentage
But I need "Dossier" to be first, then "Categorie recherchee" and "Pourcentage" shall be 2nd and 3rd. How shall I proceed?
Specify the column headers in the desired order:
$tab | Sort-Object "Pourcentage" -Descending |
Format-Table 'Dossier', 'Categorie recherchee', 'Pourcentage',
'Derniere modification', 'Dernier acces' -AutoSize |
Out-String -Width 4000 |
Out-File -Append 'O:\sigdci\public\parcoursArborescence\bilanAnalyse.txt'
If you need to dynamically determine the column names you could do it like this:
$headers = $tab[0].PSObject.Properties |
Where-Object MemberType -eq NoteProperty |
Select-Object -Expand Name
However, you'd have to bring that list into your desired order somehow. Perhaps you could do it like this:
$allHeaders = 'Dossier', 'Categorie recherchee', 'Pourcentage',
'Derniere modification', 'Dernier acces'
$actualHeaders = $tab[0].PSObject.Properties |
Where-Object { MemberType -eq NoteProperty } |
Select-Object -Expand Name
$headers = $allHeaders | Where-Object { $actualHeaders -contains $_ }
$allHeaders is an array that contains all headers in the correct order. Then you remove all items that aren't present in $actualHeaders from that list, preserving the order of the remaining headers.
I have the following code,
Get-AdGroup -filter * | select Name, sAMAccountName | Foreach-Object{
New-Object PSObject -Property #{
oldAccount = $_.Name
newAccount = "c:0-.t|adfs-2|" + $_.sAMAccountName
}
} | Export-CSV "ADGroups.csv" -NoTypeInformation
This works as designed and everything comes out as it should but they do not comeout in the order I need. I believe they come out in alphabetical order so the newAccount is always first. How can I make newAccount the second column?
You could add a Select-Object prior to the export, that will define the order.
Get-AdGroup -filter * | select Name, sAMAccountName | Foreach-Object{
New-Object PSObject -Property #{
oldAccount = $_.Name
newAccount = "c:0-.t|adfs-2|" + $_.sAMAccountName
}
} | select oldAccount, newAccount | Export-CSV "ADGroups.csv" -NoTypeInformation
Well, you could write your own CSV
"`"oldAccount`",`"newAccount`"" | Out-File "ADGroups.csv"
Get-AdGroup -filter * | select Name, sAMAccountName | Foreach-Object{
"`"$_.Name`",c:0-.t|adfs-2|$_.sAMAccountName" | Out-File "ADGroups.csv" -append
}
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"