VMware Cloud Community
SCharchouf
Hot Shot
Hot Shot

Export issue

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"

9 Replies
jpsider
Expert
Expert

Instead of 'Out-String | ForEach-Object { $_.Trim() } > "$PathH\Test.txt"'

try

Out-file "$PathH\Test.txt"

Let me know if it looks better.

Reply
0 Kudos
SCharchouf
Hot Shot
Hot Shot

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

Reply
0 Kudos
SCharchouf
Hot Shot
Hot Shot

Any assistance on this, please?

Reply
0 Kudos
LucD
Leadership
Leadership

What is the latest code you are using?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
SCharchouf
Hot Shot
Hot Shot

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"

Reply
0 Kudos
LucD
Leadership
Leadership

You have some strange double quotes in there.
This works for me

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"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LucD
Leadership
Leadership

Btw, not sure what you mean by "output as a table"?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

SCharchouf
Hot Shot
Hot Shot

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
NTPServerpool1.domain.local, pool2.domain.local
ServiceRunningTrue
IncomingPorts
OutgoingPorts123
ProtocolsUDP
StartupPolicyOn

and the desired output is like this

Name NTPServerServiceRunningIncomingPortsOutgoingPortsProtocolsStartupPolicy
esx01.domain.local
esx02.domain.local
Reply
0 Kudos
LucD
Leadership
Leadership

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!

$result = 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}}


$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

Reply
0 Kudos