Arif99
Contributor
Contributor

How to run script in Power CLI

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
0 Kudos
5 Replies
scott28tt
VMware Employee
VMware Employee

Moderator: Thread moved to the PowerCLI area.


-------------------------------------------------------------------------------------------------------------------------------------------------------------
VMware Training & Certification blog
0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
Arif99
Contributor
Contributor

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

0 Kudos
Arif99
Contributor
Contributor

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/

0 Kudos
LucD
Leadership
Leadership
0 Kudos