VMware Cloud Community
Vimal348
Enthusiast
Enthusiast
Jump to solution

Powercli to connect multiple esx hosts in a notepad file

Hello,

Can someone please help me:

I have a txt file that has the information of 15 esx hosts (6.7) (Lets say that txt file is in the folder: "E:\Script\esx.txt ". Those 15 esx hosts are in different different vCenters. My aim is to disable snmpd / enable ntpd / configure syslog only for those esx hosts in that txt file.

Is it possible to perform this action via powercli ? like call all those esx hosts with its root credentials and perform the task?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could try something like this

$user = 'root'

$pswd = 'password'


$ntpServer = 'srv1.domain','srv2.domain'

$snmpUri = 'udp://syslog.domain:514'


Get-Content -Path 'E:\Script\esx.txt' -PipelineVariable esxName |

ForEach-Object -Process {

    Connect-VIServer -Server $esxName -User $user -Password $pswd


    Get-VMHostSnmp -Server $global:defaultVIServer |

    Set-VMHostSnmp -Enabled $false -Confirm:$false |

    Stop-VMHost -Confirm:$false


    Get-VMHostService -VMHost $esxName | where{$_.Key -eq 'snmpd'} |

    Set-VMHostService -Policy Off -Confirm:$false |

    Stop-VMHostService -Confirm:$false


    Add-VMHostNtpServer -VMHost $esxName -NtpServer $ntpServer

    Get-VMHostService -VMHost $esxName | where{$_.Key -eq 'ntpd'} |

    Set-VMHostService -Policy Automatic -Confirm:$false |

    Start-VMHostService -Confirm:$false


    Set-VMHostSysLogServer -SysLogServer $snmpUri -Confirm:$false

    Get-VMHostService -VMHost $esxName | where{$_.Key -eq 'vmsyslogd'} |

    Set-VMHostService -Policy Automatic -Confirm:$false |

    Start-VMHostService -Confirm:$false


    Disconnect-VIServer -Server $esxName -Confirm:$false

}


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

View solution in original post

0 Kudos
7 Replies
scott28tt
VMware Employee
VMware Employee
Jump to solution

Moderator: Thread moved to the PowerCLI area.


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos
NathanosBlightc
Commander
Commander
Jump to solution

For example if you want to search inside the hostlist.txt file and start ntpd service ...

$hosts = Get-content -path ".\hostlist.txt"

foreach ($host in $hosts)

{

Connect-VIServer $esx -User  $root -Password $Passwd

Get-VMHost | Get-VMHostService | Where-Object {$_.key -eq "ntpd" } | Start-VMHostService

}

Sadly I don't have access to any of my vcenter servers and couldn't test it anyway, so I'am not ensure about using against multiple vCenter however. For enabling the SNMP service, you can find a good solution in the following community link:

Set SNMP thru PowerCLI

For more information please check the following links

Run a command on ESXi using PowerCLis

Also for the syslog configuration I think it's better to run the "esxcli system syslog config set ... " via the PowerCLI. To achieve this, you can see this link too.

Please mark my comment as the Correct Answer if this solution resolved your problem
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could try something like this

$user = 'root'

$pswd = 'password'


$ntpServer = 'srv1.domain','srv2.domain'

$snmpUri = 'udp://syslog.domain:514'


Get-Content -Path 'E:\Script\esx.txt' -PipelineVariable esxName |

ForEach-Object -Process {

    Connect-VIServer -Server $esxName -User $user -Password $pswd


    Get-VMHostSnmp -Server $global:defaultVIServer |

    Set-VMHostSnmp -Enabled $false -Confirm:$false |

    Stop-VMHost -Confirm:$false


    Get-VMHostService -VMHost $esxName | where{$_.Key -eq 'snmpd'} |

    Set-VMHostService -Policy Off -Confirm:$false |

    Stop-VMHostService -Confirm:$false


    Add-VMHostNtpServer -VMHost $esxName -NtpServer $ntpServer

    Get-VMHostService -VMHost $esxName | where{$_.Key -eq 'ntpd'} |

    Set-VMHostService -Policy Automatic -Confirm:$false |

    Start-VMHostService -Confirm:$false


    Set-VMHostSysLogServer -SysLogServer $snmpUri -Confirm:$false

    Get-VMHostService -VMHost $esxName | where{$_.Key -eq 'vmsyslogd'} |

    Set-VMHostService -Policy Automatic -Confirm:$false |

    Start-VMHostService -Confirm:$false


    Disconnect-VIServer -Server $esxName -Confirm:$false

}


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

0 Kudos
Vimal348
Enthusiast
Enthusiast
Jump to solution

I tried both scripts and I am getting the below error:
https://snipboard.io/sYJ2fU.jpg

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is not from the script I posted.


Place the code in a .ps1 file and run the .ps1 file.

Don't copy the code line by line to a PS prompt


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There seems to have been a copy/paste issue with my code.

I corrected the code above.
Please try again.


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

0 Kudos