VMware Cloud Community
Bean78
Contributor
Contributor
Jump to solution

PowerShell script to determine ESXi firewall service status through vCenter and export its status

I tried to execute this script, i obtain an error "You cannot call a method on a null-valued expression."

The main goal of the script is to read the status of a service in the ESXi firewall through vCenter console and to export the result in to an CSV file. Please assist me on this requirement.

$esx_hosts = Get-VMHost -State Maintenance,Connected

foreach ($esx_host in $esx_hosts)
{
Write-Host $esx_host checking
$esxcli= get-esxcli -VMHost $esx_host -V2
$fw_status = ($esxcli.CIMSPL.firewall.get.invoke()).Enabled
Write-Host $esx_host – $fw_status

if ($fw_status -eq “false”) {
Write-Host Enabling FW -ForegroundColor Green
$arguments = $esxcli.CIMSPL.firewall.set.CreateArgs()
$arguments.enabled = “true”
$esxcli.CIMSPL.firewall.set.invoke($arguments)
}
}

Moderator note by wila: Moved post from vCenter Converter SDK to VMware PowerCLI discussions

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this

Get-VMHost -State Maintenance,Connected -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli= Get-EsxCli -VMHost $esx -V2
    $esxcli.network.firewall.ruleset.list.Invoke().where{$_.Name -eq 'CIMSLP'} |
    Add-Member -MemberType NoteProperty -Name VMHost -Value $esx.Name -PassThru
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

It would help if you showed the error since it includes the lines on which the error occurred. 


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

0 Kudos
Bean78
Contributor
Contributor
Jump to solution

Hi LucD,

Seems i have messed up on that script, do you have script handy to generate a CSV report of a particular service (in my case CIM SPL service) to determine its service status (enabled /disabled) on multiple ESXi servers by authenticating through the vCenter console.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

Get-VMHost -State Maintenance,Connected -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli= Get-EsxCli -VMHost $esx -V2
    $esxcli.network.firewall.ruleset.list.Invoke().where{$_.Name -eq 'CIMSLP'} |
    Add-Member -MemberType NoteProperty -Name VMHost -Value $esx.Name -PassThru
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos
Bean78
Contributor
Contributor
Jump to solution

Thanks Lucd for your help, the script generated the reported as expected.

0 Kudos