Learning PowerCli!

6. How to store user credentials using the PowerCli?

   $cred = Get-Credential

The above code will save the credentials (if you vCenter credentials are not same as your windows FQDN).

There after you can connect to your vCenter just by typing the below command.

Connect-VIServer 192.168.145.163 -Credential $cred

 

7. How to connect to ESX host ?

Connect-VIServer “192.168.145.163” -Credential $cred

To connect to ESX host directly you need to alter the PowerCli proxy configuration.

To check your PowerCli proxy configuration

Get-PowerCLIConfiguration

To change the proxy configuration to “No Proxy” use the following command.

Set-PowerCLIConfiguration -ProxyPolicy NoProxy

 

8. How to disconnect from connected ESX or vCenter server?

disconnect-VIServer

To disconnect from all connect host / VC  you can supply the below command.

disconnect-VIServer *

 

9. PowerCli Cmdlets

 

10.  How to get list of command by verb and noun?

Get-Vicommand | group-object noun

Get-Vicommand | group-object verb

To sort by count

Get-Vicommand | group-object verb | sort count

11. How to declare a variable?

$vm = Get-VM Win_XP

Now the $VM inherits the get-vm command for virtual machine Win_XP, we can perform task like

$VM.name or $vm |Get-VMguest

12. How export data in a spread sheet?

Get-Vm | Export-Csv  -path data.csv -notypeinformation

to read the file the following command can be used.

Invoke-Item data.csv

13. How to fetch report of only column which we want?

Get-Vm | select-object name, Memorymb, numcpu|Export-Csv  -path data.csv -notypeinformation

Alias for Invoke Item is “ii”

ii data.csv

14. How to get the member data (Column names)?

Get-VM | Get-Member

15. How get the table output in an HTML format?

Get-VM | select-object name,MemoryMb, numcpu | ConvertTo-HTML | set-content data.html

To execute the code its

 ii data.html

16. How to read data in list instead of table view.

Get-VM | Format-List

17. How to use the . before its properties.

$vm.powerstate

18. How to identify VM which has less than 1 GB RAM configured?

$ram = Get-VM | Where-object {$_.memoryMB -lt 1024}

$_  accepts what every object comes through the pipeline.

19. To identify to which ESX host a VM is connected, the below command can be used.

Get-VM Win_XP | Get-VMHost

 

20. How to add a ESX / ESXi host to VCenter server  using PowerCli?

Add-VMHost -Name 192.168.145.164 -user root -pass Admin123 -location $dc -RunAsync -Force

$dc is mapped to Get-Datacenter and Force is used to ignore host SSL certificate validation.

21. How to add new VMHostAccount?

New-VMHostAccount -Id newuser -Password Admin123 -Description “hey there” -Gr

antShellAccess:$false

You need to directly connected to host to perform this task. The granstshell access represent SSH access.

22. How to find list of available commands for the command lets.

Get-Command *VMhostservice

 

23. GCM is the short form of get command.

gcm  Get-VM

 

24. How to overcome if you receive  error message something like “it’s not digitally signed” while running a PowerCli script.

Run this command

Set-ExecutionPolicy Unrestricted

3970 Total Views 6 Views Today

Leave a Reply