VMware Cloud Community
Arif99
Contributor
Contributor
Jump to solution

How to run script in Power CLI

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Reply
0 Kudos
5 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
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

First, I strongly suggest to upgrade your PowerShell and PowerCLI versions.

What error are you getting?

How did you run the script?


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

Reply
0 Kudos
Arif99
Contributor
Contributor
Jump to solution

How to update powerCLI version?

Error is as below.

PowerCLI C:\> & '.\Setting up DNS, NTP and domain in bulk-modified.ps1'

abc.com : The term 'abc.com' is not recognized as the name of a

cmdlet, function, script file, or operable program. Check the spelling of the

name, or if a path was included, verify that the path is correct and try again.

At C:\Setting up DNS, NTP and domain in bulk-modified.ps1:10 char:15

+ $domainname = abc.com

+               ~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (abc.com:String) [], Comman

   dNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

ntp.ga.adient.com : The term 'ntp.ga.adient.com' is not recognized as the name

of a cmdlet, function, script file, or operable program. Check the spelling of

the name, or if a path was included, verify that the path is correct and try

again.

At C:\Setting up DNS, NTP and domain in bulk-modified.ps1:13 char:11

+ $ntpone = ntp.abc.com

+           ~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (ntp.ga.adient.com:String) [], C

   ommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

esx01.abc.ocm : The term 'esx01.abc.ocm' is not recognized as

the name of a cmdlet, function, script file, or operable program. Check the

spelling of the name, or if a path was included, verify that the path is

correct and try again.

At C:\Setting up DNS, NTP and domain in bulk-modified.ps1:17 char:13

+ $esxHosts = esx01.abc.ocm

+             ~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (esx01.abc.ocm:String) [

   ], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Done!

PowerCLI C:\> $PSVersionTable

Reply
0 Kudos
Arif99
Contributor
Contributor
Jump to solution

I able to fix the error now I can run the script successfully. Script was ok but looks something wrong with PowerCLI. So I followed below article to connect my powershell and load all VMware modules. Now since I can run the script in Powershell, it is working perfect as I expected. But still, you can help to know the procedure of upgrading the PowerCLI?

https://paulgrevink.wordpress.com/2017/12/30/writing-effective-scripts-using-vmware-powercli/

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look at Updating PowerCLI through the PowerShell Gallery


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

Reply
0 Kudos