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?
You could try something like this
$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
Moderator: Thread moved to the PowerCLI area.
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:
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.
You could try something like this
$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
I tried both scripts and I am getting the below error:
https://snipboard.io/sYJ2fU.jpg
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
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