How to run script in Power CLI

This post was originally published on this site

I have powershell version 4.0 and Power CLI version 6.5 Release build 4624819. If I run one line command like  “Get-VMHostNetwork -VMHost $esx | Set-VMHostNetwork -DomainName $domainname -DNSAddress $dnspri , $dnsalt -Confirm:$false” it runs ok. But I have to make few change for 100+ servers so I have created below script. But, problem is PowerCLI is not understand the EACH loop or variables. Even if I put some variable in PowerCLI like $Test = I am Arif then , it does not understand what is $Test. I then imported Vmware.PowerCLI module to powershell and same problem there. Can someone pls help me urgently.

 

# PowerCLI Script to Configure DNS and NTP on ESXi Hosts

# PowerCLI Session must be connected to vCenter Server using Connect-VIServer.

 

# Prompt for Primary and Alternate DNS Servers

$dnspri = 1.1.1.1

$dnsalt = 2.2.2.2

 

# Prompt for Domain

$domainname = abc.com

 

#Prompt for NTP Servers

$ntpone = NTP.ABC.COM

$ntptwo = 3.3.3.3

$ntpthree = 4.4.4.4

 

$esxHosts = ESX01.abc.com

 

foreach ($esx in $esxHosts) {

 

   Write-Host “Configuring DNS and Domain Name on $esx” -ForegroundColor Green

   Get-VMHostNetwork -VMHost $esx | Set-VMHostNetwork -DomainName $domainname -DNSAddress $dnspri , $dnsalt -Confirm:$false

 

   

   Write-Host “Configuring NTP Servers on $esx” -ForegroundColor Green

   Add-VMHostNTPServer -NtpServer $ntpone , $ntptwo, $ntpthree -VMHost $esx -Confirm:$false

 

   Writ-Host “Setting up SearchDomain Name”

   Get-VMHost $esx |Get-VMHostNetwork |Set-VMHostNetwork -SearchDomain “autoexpr.com”

 

   Write-Host “Configuring NTP Client Policy on $esx” -ForegroundColor Green

   Get-VMHostService -VMHost $esx | where{$_.Key -eq “ntpd”} | Set-VMHostService -policy “on” -Confirm:$false

 

   Write-Host “Restarting NTP Client on $esx” -ForegroundColor Green

   Get-VMHostService -VMHost $esx | where{$_.Key -eq “ntpd”} | Restart-VMHostService -Confirm:$false

 

}

Write-Host “Done!” -ForegroundColor Green

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.