Contents
Install
Prerequisites
Before installing PowerCLI, see Prerequisites for Installing and Running PowerCLIhttps://vdc-repo.vmware.com/vmwb-repository/dcr-public/a63e75d6-490f-4d08-b0ca-cdc2cf155dae/451d164c-c0fd-40c1-80a0-9c2934dda006/GUID-6AD55F02-4EB5-4705-895E-242331021295.html#GUID-6AD55F02-4EB5-4705-895E-242331021295.
- Verify that you have uninstalled PowerCLI 6.5 R1 or earlier from your system.
- Verify that your system is connected to the Internet.
Verify that you have registered the PowerShell Gallery as a local repository.
If you use Windows PowerShell 3.0 or 4.0, install the PowerShellGet and PackageManagement modules from the PowerShell Gallery Web site.
Windows 2008R2
NetFramework4.5.2 or later
- Windows Management Framework 5.1
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
get-host
$PSVersionTable
Install-Module VMware.PowerCLI -Scope CurrentUser
PS C:\Users\Administrator> get-host
Name : ConsoleHost
Version : 5.1.14409.1005
InstanceId : cee5ec9e-f773-452a-b675-00cc8f1d491f
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : zh-CN
CurrentUICulture : zh-CN
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
PS C:\Users\Administrator> Get-PowerCLIVersion
警告: Please consider joining the VMware Customer Experience Improvement Program, so you can help us make PowerCLI a better product. You can join using the following command:
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true
VMware's Customer Experience Improvement Program ("CEIP") provides VMware with information that enables VMware to improve its products and services, to fix problems, and to advise you on how best to deploy and use our products. As part of the CEIP, VMware collects technical
information about your organization抯 use of VMware products and services on a regular basis in association with your organization抯 VMware license key(s). This information does not personally identify any individual.
For more details: type "help about_ceip" to see the related help article.
To disable this warning and set your preference use the following command and restart PowerShell:
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true or $false.
警告: The cmdlet "Get-PowerCLIVersion" is deprecated. Please use the 'Get-Module' cmdlet instead.
PowerCLI Version
----------------
VMware PowerCLI 11.5.0 build 14912921
---------------
Component Versions
---------------
VMware Common PowerCLI Component 11.5 build 14898112
VMware Cis Core PowerCLI Component PowerCLI Component 11.5 build 14898113
VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 11.5 build 14899560
Configration
Set-ExecutionPolicy RemoteSigned Set-PowerCLIConfiguration -ProxyPolicy NoProxy Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
Use
Connect-VIServer -server 192.168.25.60 -Protocol https -Username 'administrator@vsphere.local'
PowerCLI to quickly retrieve the time setting on all the hosts managed by specific vCenter instances
foreach ($esx in (get-vmhost)) {$esx.Name + " -> " + (get-view $esx.ExtensionData.ConfigManager.DateTimeSystem).QueryDateTime().ToLocalTime()}
Script to list VM Network Adapter Type, exporting issue
Get-VM | Get-NetworkAdapter |
Where-object {$_.Type -ne "Vmxnet3"} |
Select @{N="VM";E={$_.Parent.Name}},Name,Type |
export-Csv c:\Network_Interface.csv -NoTypeInformation
Deploy VMs from template PowerCLI Script
#Script by Jose Rodriguez
#Yeah this doesn't comply with any standards but it gets the job done.
#This is by no means perfect. This is the first working version of the script
#Creates VM's based on data from a CSV file
#~I'm just an IT enthusiast with no certs, no degrees, no training, in any of this crap, but I know my around google so that's all that matters. -Jose Rodriguez~
#2/10/2018
$newVMs = import-csv "C:\scripts\auto-create-vms.csv"
$server = '192.168.25.201'
#Get Credentials if not exist
if($cred -eq $null){
$cred = Get-Credential
}
Connect-VIServer -Server $server -Credential $cred
$a = @()
foreach($vm in $newVMs){
Write-host "Deploying VM " -ForegroundColor Green -NoNewline; Write-Host $vm.name -ForegroundColor Yellow
# $vms = New-VM -Name $vm.Name -VMhost $vm.vmhost -Template $vm.template -confirm:$false -Datastore $vm.datastore -Location $vm.location -verbose -RunAsync
$vms = New-VM -Name $vm.Name -VMhost $vm.vmhost -Template $vm.template -confirm:$false -Datastore $vm.datastore -verbose -RunAsync
$vms | Add-Member -type NoteProperty -name VMName -value $vm.name
$a += $vms
}
foreach($task in $a){
$result = get-task -id $task.id
do{
$result = get-task -id $task.id
start-sleep -Seconds 10
}
until ($result.State -eq 'Success'){
}
if($result.State -eq 'Success' -and ((Get-VM -Id $result.Result).PowerState) -eq 'PoweredOff' ){
Get-VM -Id $result.Result | start-vm
}
} PS C:\scripts> C:\scripts\auto-create-vms.ps1 Name Port User ---- ---- ---- 192.168.25.201 443 VSPHERE.LOCAL\Administrator Deploying VM ad-client-test-01 详细信息: Creates a new virtual machine with the specified parameters. 详细信息: 2020/5/25 16:59:18 New-VM Finished execution Deploying VM ad-client-test-02 详细信息: Creates a new virtual machine with the specified parameters. 详细信息: 2020/5/25 16:59:19 New-VM Finished execution Deploying VM ad-client-test-03 详细信息: Creates a new virtual machine with the specified parameters. 详细信息: 2020/5/25 16:59:19 New-VM Finished execution Deploying VM ad-client-test-04 详细信息: Creates a new virtual machine with the specified parameters. 详细信息: 2020/5/25 16:59:20 New-VM Finished execution Deploying VM ad-client-test-05 详细信息: Creates a new virtual machine with the specified parameters. 详细信息: 2020/5/25 16:59:20 New-VM Finished execution
- auto-create-vms.csv
Name,IP,SUBNET,GATEWAY,DNS1,DNS2,vmhost,template,cluster,csm,datastore,location ad-client-test-01,192.168.66.21,255.255.255.0,192.168.66.254,192.168.66.221,192.168.66.222,192.168.25.200,Windows-Tftpd-AD-Server-01,Cluster Physical Server,DNS1,ESXi-25.200,Guanzhou ad-client-test-02,192.168.66.22,255.255.255.0,192.168.66.254,192.168.66.221,192.168.66.222,192.168.25.200,Windows-Tftpd-AD-Server-01,Cluster Physical Server,DNS2,ESXi-25.200,SZ ad-client-test-03,,,,,,192.168.25.200,Windows-Tftpd-AD-Server-01,Cluster Physical Server,,ESXi-25.200, ad-client-test-04,,,,,,192.168.25.200,Windows-Tftpd-AD-Server-01,Cluster Physical Server,,ESXi-25.200, ad-client-test-05,,,,,,192.168.25.200,Windows-Tftpd-AD-Server-01,Cluster Physical Server,,ESXi-25.200,
Create VM
- Net-VM
PS C:\Users\Administrator> New-VM -Name Dnsmasq-DNS-Server-01 -Datastore ESXi-25.200 -DiskGB 20 -DiskStorageFormat Thin -MemoryGB 4 -CD -GuestId rhel7_64Guest -NumCpu 2 -NetworkName "t-auto-net" -VMHost 192.168.25.200
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
Dnsmasq-DNS-Serve... PoweredOff 2 4.000
New-VM -Name Dnsmasq-DNS-Server-02 -Datastore ESXi-25.200 -DiskGB 20 -DiskStorageFormat Thin -MemoryGB 4 -CD -GuestId vmkernel65Guest -NumCpu 2 -NetworkName "t-auto-net" -VMHost 192.168.25.200
New-VM -Name Windows-Tftpd-AD-Server-01 -Datastore ESXi-25.200 -DiskGB 50 -DiskStorageFormat Thin -MemoryGB 8 -CD -GuestId windows8Server64Guest -NumCpu 4 -NetworkName "t-auto-net" -VMHost 192.168.25.200
PS C:\Users\Administrator> Get-VM Dnsmasq-DNS-Server-01 | select *
Name : Dnsmasq-DNS-Server-01
PowerState : PoweredOff
Notes :
Guest : Dnsmasq-DNS-Server-01:
NumCpu : 2
CoresPerSocket : 1
MemoryMB : 4096
MemoryGB : 4
VMHostId : HostSystem-host-12
VMHost : 192.168.25.200
VApp :
FolderId : Folder-group-v3
Folder : vm
ResourcePoolId : ResourcePool-resgroup-8
ResourcePool : Resources
HARestartPriority : ClusterRestartPriority
HAIsolationResponse : AsSpecifiedByCluster
DrsAutomationLevel : AsSpecifiedByCluster
VMSwapfilePolicy : Inherit
VMResourceConfiguration : CpuShares:Normal/2000 MemShares:Normal/40960
Version : v14
HardwareVersion : vmx-14
PersistentId : 5001c82a-15f7-bee3-729c-339f131139d7
GuestId : rhel7_64Guest
UsedSpaceGB : 0
ProvisionedSpaceGB : 24.179554439149796962738037109
DatastoreIdList : {Datastore-datastore-13}
CreateDate : 2020/5/14 14:25:39
ExtensionData : VMware.Vim.VirtualMachine
CustomFields : {}
Id : VirtualMachine-vm-142
Uid : /VIServer=vsphere.local\administrator@t-vcenter.systec.com.cn:443/VirtualMachine=VirtualMachine-vm-142/
delete VM
- Get-VM
PS C:\Users\Administrator> Get-VM Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- WIN-XP PoweredOff 2 4.000 TTTT-02 PoweredOff 2 4.000 Edge-Host PoweredOff 6 8.000 Product-Host PoweredOff 6 10.000 Internet-Test-Client PoweredOff 1 0.375 nsxmgr-01 PoweredOff 4 16.000 TTTT-01 PoweredOff 2 4.000 New Virtual Machine PoweredOff 1 0.250 WIN10 PoweredOff 2 6.000 LAMP_v7 PoweredOn 1 0.758 workspace PoweredOff 1 0.250 CSR-Router PoweredOff 1 4.000 BIG-IP-LTM PoweredOn 2 4.000 Internet-router PoweredOff 1 1.000 NSX-l2t-client-Xl... PoweredOff 6 8.000 test1 PoweredOff 1 2.000 t-vcenter PoweredOn 2 10.000 FTD1 PoweredOff 4 8.000
Remove-VM with DeletePermanently
PS C:\Users\Administrator> Remove-VM -VM TTTT-01 -DeletePermanently Perform operation? Performing operation 'Removing VM from disk.' on VM 'TTTT-01' [Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“Y”): PS C:\Users\Administrator> Get-VM Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- WIN-XP PoweredOff 2 4.000 TTTT-02 PoweredOff 2 4.000 Edge-Host PoweredOff 6 8.000 Product-Host PoweredOff 6 10.000 Internet-Test-Client PoweredOff 1 0.375 nsxmgr-01 PoweredOff 4 16.000 New Virtual Machine PoweredOff 1 0.250 WIN10 PoweredOff 2 6.000 LAMP_v7 PoweredOn 1 0.758 workspace PoweredOff 1 0.250 CSR-Router PoweredOff 1 4.000 BIG-IP-LTM PoweredOn 2 4.000 Internet-router PoweredOff 1 1.000 NSX-l2t-client-Xl... PoweredOff 6 8.000 test1 PoweredOff 1 2.000 t-vcenter PoweredOn 2 10.000 FTD1 PoweredOff 4 8.000
Get/Change CPUID
Winodws script
http://vknowledge.net/2014/04/17/how-to-fake-a-vms-guest-os-cpuid/
- Enable excution
PS C:\Users\Administrator> Get-ExecutionPolicy Restricted PS C:\Users\Administrator> set-executionpolicy remotesigned 执行策略更改 执行策略可以防止您执行不信任的脚本。更改执行策略可能会使您面临 about_Execution_Policies 帮助主题中所述的安全风险。是否要更改执行策略? [Y] 是(Y) [N] 否(N) [S] 挂起(S) [?] 帮助 (默认值为“Y”):
- Get cpuid
PS C:\Users\Administrator> Get-WmiObject Win32_Processor | select ProcessorId ProcessorId ----------- 0FABFBFF000106A5 0FABFBFF000006A5
- convert HEX to ascii
Rename PortGroup name
Get-VMHost -name 192.168.25.200 | Get-VirtualPortGroup -Name "MGT2" | Set-VirtualPortGroup -Name "MGT" -VLanId 111
start ssh
Get-VMHost | Foreach { Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )}
Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH" } |select VMHost, Label, Running
stop ssh
Get-VMHostService -VMHost * | Where-Object {$_.Key -eq "TSM-SSH" } | Stop-VMHostService