I'm trying to export the output for the below command unfortunately the result is not as desired
I need the output as a Table and I'mnot sure where I'm doing a mistake in this syntax, can someone help on this ?
Get-VMHost |Sort Name|Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}}, @{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}}, @{N="IncomingPorts";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).IncomingPorts}}, @{N="OutgoingPorts";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).OutgoingPorts}}, @{N="Protocols";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).Protocols}}, @{N=“StartupPolicy“;E={($_ | Get-VmHostService | Where-Object {$_.key-eq “ntpd“}).Policy}} | Out-String | ForEach-Object { $_.Trim() } > "$PathH\Test.txt"
Instead of 'Out-String | ForEach-Object { $_.Trim() } > "$PathH\Test.txt"'
try
Out-file "$PathH\Test.txt"
Let me know if it looks better.
got the below error
Select-Object : A positional parameter cannot be found that accepts argument 'System.Object[]'.
At script.ps1:115 char:26
+ ... Sort Name | Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}} ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
Any assistance on this, please?
What is the latest code you are using?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Get-VMHost |Sort Name|Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}}, @{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}}, @{N="IncomingPorts";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).IncomingPorts}}, @{N="OutgoingPorts";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).OutgoingPorts}}, @{N="Protocols";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).Protocols}}, @{N=“StartupPolicy“;E={($_ | Get-VmHostService | Where-Object {$_.key-eq “ntpd“}).Policy}} | Out-String | ForEach-Object { $_.Trim() } > "$PathH\Test.txt"
You have some strange double quotes in there.
This works for me
Select Name, @{N="NTPServer";E={$_ |Get-VMHostNtpServer}},
@{N="ServiceRunning";E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq "ntpd"}).Running}},
@{N="IncomingPorts";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).IncomingPorts}},
@{N="OutgoingPorts";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).OutgoingPorts}},
@{N="Protocols";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).Protocols}},
@{N="StartupPolicy";E={($_ | Get-VmHostService | Where-Object {$_.key-eq "ntpd"}).Policy}} |
Out-String | ForEach-Object { $_.Trim() } > "$PathH\Test.txt"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Btw, not sure what you mean by "output as a table"?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
my bad I didn't explained well what I need:smileyblush::smileyblush:
the output that I obtain with the scriptabove is like this
Name | : esx01.domain.local |
NTPServer | pool1.domain.local, pool2.domain.local |
ServiceRunning | True |
IncomingPorts | |
OutgoingPorts | 123 |
Protocols | UDP |
StartupPolicy | On |
and the desired output is like this
Name | NTPServer | ServiceRunning | IncomingPorts | OutgoingPorts | Protocols | StartupPolicy |
esx01.domain.local | ||||||
esx02.domain.local | ||||||
You get the output in List format because the PowerShell output engine has rules which tell it do so when the output exceeds specific values.
You can compose lines yourself, thus bypassing the PS output engine, and then write those lines to a .txt file.
Something like this for example.
But this also shows an issue when you are trying to do output yourself, there is no automatic alignment!
Select Name, @{N="NTPServer";E={$_ |Get-VMHostNtpServer}},
@{N="ServiceRunning";E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq "ntpd"}).Running}},
@{N="IncomingPorts";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).IncomingPorts}},
@{N="OutgoingPorts";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).OutgoingPorts}},
@{N="Protocols";E={($_ | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"}).Protocols}},
@{N="StartupPolicy";E={($_ | Get-VmHostService | Where-Object {$_.key-eq "ntpd"}).Policy}}
$report = @()
$report += "Name NTPServer ServiceRunning IncomingPorts OutgoingPorts Protocols StartupPolicy"
$result | ForEach-Object -Process {
$report += "{0} {1} {2} {3} {4} {5}" -f $_.Name,$_.NTPServer,$_.ServiceRunning,$_.IncomingPorts,$_.OutgoingPorts,
$_.Protocols,$_.StartupPolicy
}
$report | Set-Content -Path .\report.txt
You could consider making the column sufficiently wide and thus calculating the correct alignment. But that is some work that is not really suited for automation.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference