-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerCLI
97 lines (59 loc) · 3.43 KB
/
PowerCLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
1. Connect-VIServer: How to connect to vCenter
Connect-VIServer -Server [server name] -Protocol [http] -User [user name] -Password [password]
2. Get-VMHost: How to retrieve host information
Get-VMHost
3. Get-VM: How to retrieve VM information
Get-VM
or
Get-VM -Name [vmname] | Format-List
Get-VM Format-List
4. Start-VM & Stop-VM: How to start and stop VM
To start:
Start-VM ‑VM [vmname]
To Stop:
Stop-VM ‑VM [vmname]
5. New-VM: How to create new VM
New-VM -Name [vmname] -VMHost [hostname]
To create a new folder, the basic syntax is:
New-Folder -Name [folder name]
To get the inventory root folder and create a new folder in it:
$folder = Get-Folder -NoRecursion | New-Folder -Name [folder name]
To create a new datacenter in the folder that is specified by the -Location parameter:
New-Datacenter -Location $folder -Name [datacenter name]
6. Move-VM: How to move VM to another location
To live migrate a single VM to another host, run the command:
Move-VM -VM [vmname] -Destination [hostname]
To live migrate a single VM to another datastore, run the command:
Move-VM -VM [vmname] -Datastore [datastore name]
To move all VMs from one host to another host, run the command:
Get-VMHost [hostname] | Get-VM | Move-VM -Destination (Get-VMHost [hostname])
7. New-Snapshot: How to take VM snapshot
To create a new snapshot of the specified VM, the basic syntax is:
New-Snapshot -VM [vmname] -Name [snapshot name]
To create a new snapshot of the powered-on VM and preserves its memory state:
New-Snapshot ‑VM [vmname] ‑Name [snapshot name] -Description [description] ‑Memory $true
To view all the snapshot of the specified VM:
Get-Snapshot -VM [vmname]
or
Get-Snapshot
8. -ToTemplate: How to convert VM to template
To convert a VM to template, run the command:
Set-VM -ToTemplate -Confirm:$false
To retrieve all VM templates in the specified datacenter:
Get-Template -Location [datacenter]
9. Invoke-VMScript: How to run script easier
If your network connection to a VM is lost, or if you need to automate tasks on single or more VMs, Invoke-VMScript can send commands directly to the VM without the need for a normal WinRM or SSH connectivity. For example:
Runs a PowerShell script. In PowerShell, to access environment variables, you must use the following syntax: $env: Also, to run the program, you must specify an ampersand (&) in front of the program path. The outer quotes ($script = '...') are required because this is how you define a string variable in PowerShell. The inner double quotes are required because there are spaces in the path.
$script = '&"$env:ProgramFiles\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" /report "$env:Tmp\inforeport"' Invoke-VMScript -ScriptText $script -VM VM -GuestCredential $guestCredential
10. Get-VICommand: How to view all available commands
To retrieve all available commands of the imported VMware modules:
Get-VICommand
To retrieve all relevant commands for the specified "snapshot":
Get-VICommand *snapshot*
11. Get-Help: How to access official help system
Run the following command to display the basic information of the command, such as synopsis, syntax, and descriptions:
Get-Help [command]
If you want to view all of the help information about this command, including parameters and examples, run the command:
Get-Help [command] -Full
Note: Before you start, you may need to run Update-Help to install the most current help files for Windows PowerShell modules.
Update Get-Help