Hi Experts,
I Need powershell script to Start & stop the CIM server service with host and Run the service. Script which have to check all available ESXi host in the vCenter and Start and Run the CIM server service. If the settings already enabled and running it have to print it was already enabled and running. Also the script needs to print in which esxi host it enables the CIM service. I have attched the Screenshot for the reference,
I am looking forward to hearing from you.
Thanks in advance
Sorry for living in another TZ than you and going to sleep at night.
Try with this
Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
$obj = New-Object -TypeName PSObject -Property ([ordered]@{
VMHost = $esx.Name
VMHostStatus = $esx.ConnectionState
CIMServiceRunning = ''
CIMServiceStarted = ''
CIMServiceOldPolicy = ''
CIMServiceNewPolicy = ''
})
if ($esx.ConnectionState -eq "Connected") {
Get-VMHostService -VMHost $esx | where { $_.Label -eq "CIM Server" } |
ForEach-Object -Process {
$obj.CIMServiceRunning = $_.Running
$obj.CIMServiceStarted = $false
$obj.CIMServiceOldPolicy = $_.Policy
$obj.CIMServiceNewPolicy = $_.Policy
if (-not $_.Running) {
$obj.CIMServiceStarted = $true
Start-VMHostService -HostService $_ -Confirm:$false | Out-Null
}
if ($_.Policy -ne 'On') {
Set-VMHostService -HostService $_ -Policy 'On' -Confirm:$false | Out-Null
$obj.CIMServiceNewPolicy = 'On'
}
}
}
$obj
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Something like this?
Get-VMHost -PipelineVariable esx |
Get-VMHostService | Where {$_.Label -eq "CIM Server"} |
ForEach-Object -Process {
$obj = New-Object -TypeName PSObject -Property ([ordered]@{
VMHost = $esx.Name
CIMServiceRunning = $_.Running
CIMServiceStarted = $false
CIMServiceOldPolicy = $_.Policy
CIMServiceNewPolicy = $_.Policy
})
if(-not $_.Running){
$obj.CIMServiceStarted = $true
Start-VMHostService -HostService $_ -Confirm:$false
}
if($_.Policy -ne 'Automatic'){
Set-VMHostService -HostService $_ -Policy Automatic -Confirm:$false
$obj.CIMServiceNewPolicy = 'Automatic'
}
$obj
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD
Hi Lucd,
Thanks much for your code. Its great.
There are 3 things I have noticed. That's needs to be change. Need your help on it. Please help me to fix it.
1) Instead of setting "Start and stop with host", It was set to "Start and stop with port usage" Expected should be "Start and stop with host" Attached the screenshot.
2) Once after I ran the comments, there are some more values printed in the PowerShell screen. I have attached the screenshot and highlighted in blue box. Those things needs to be hide out from the output of PowerShell screen.
3) Incase if some ESXi server in maintenance mode or not reachable. It have to print that's particular server was in maintanance mode or not reachable, and script to skip those esxi servers and execute in the other available esxi servers.
If you help me on this it really means a lot.
I am looking forward to hearing from you.
@LucD
Hi Expert,
Could you please help on this.?
I am looking forward to hearing you
Sorry for living in another TZ than you and going to sleep at night.
Try with this
Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
$obj = New-Object -TypeName PSObject -Property ([ordered]@{
VMHost = $esx.Name
VMHostStatus = $esx.ConnectionState
CIMServiceRunning = ''
CIMServiceStarted = ''
CIMServiceOldPolicy = ''
CIMServiceNewPolicy = ''
})
if ($esx.ConnectionState -eq "Connected") {
Get-VMHostService -VMHost $esx | where { $_.Label -eq "CIM Server" } |
ForEach-Object -Process {
$obj.CIMServiceRunning = $_.Running
$obj.CIMServiceStarted = $false
$obj.CIMServiceOldPolicy = $_.Policy
$obj.CIMServiceNewPolicy = $_.Policy
if (-not $_.Running) {
$obj.CIMServiceStarted = $true
Start-VMHostService -HostService $_ -Confirm:$false | Out-Null
}
if ($_.Policy -ne 'On') {
Set-VMHostService -HostService $_ -Policy 'On' -Confirm:$false | Out-Null
$obj.CIMServiceNewPolicy = 'On'
}
}
}
$obj
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD
Hi Expert Lucd,
Oops.. Got it we are from different time zone. I understood.
First of all I would like to thank you.
Thanks Lucd.
I have tried with your latest code. I am not getting any output or error message. Also still its reflecting as "Start and stop with port usage". Expected output : "Start and stop with host" is not getting changed.
I have attached the output file.
I left my test ESXi node in there for testing.
Code above is corrected, please try again
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD
Hi LucD,
Thank you LucD.
No doubt why everyone calling you as legend. You are true legend. Your script working like a charm.
I am sorry to ask you. I need one last favor. Its not printing any message if the required settings already enabled and also In the end of the execution, I just want one message needs print It got executed successfully in the available hosts.
Once again thanks much
@LucD
Hi LucD
Thank you.
Sorry if was bothered you.
Marked your answer as right answer.
You can see the setting before and after with the CIMServiceOldPolicy and CIMServiceNewPolicy properties.
When those properties show different values the setting was changed.
Or do you want to see something else?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD
Hi LucD,
I am sorry. After given upvote to your answer. I mean marked your answer as Correct answer. I haven't logged in. Today only I logged in community after that.
I dont want to see anything else. Thanks for asking.
You are amazing,
Thanks again.