If
(!(
Get-PSSnapin
-name
VMware.VimAutomation.Core
-ErrorAction
SilentlyContinue)) {
Add-PSSnapin
VMware.VimAutomation.Core}
If
(!(
Get-PSSnapin
-name
VMware.VimAutomation.Cloud
-ErrorAction
SilentlyContinue)) {
Add-PSSnapin
VMware.VimAutomation.Cloud}
If
(!(
Get-PSSnapin
-name
VMware.VimAutomation.VDS
-ErrorAction
SilentlyContinue)) {
Add-PSSnapin
VMware.VimAutomation.VDS}
Connect-VIServer
vcenternameorip
Disconnect-VIServer
vcenternameorip
-Confirm
:
$False
Connect-CIServer
my.vcd.url
Disconnect-CIServer
my.vcd.url
-Confirm
:
$False
$DefaultVIServers
$DefaultVIServers
.Count
Get-PowerCLIVersion
Get-PowerCLIConfiguration
$VMHosts
=
Get-VMHost
ForEach
(
$VMHost
in
$VMHosts
)
{
$HostName
=
$VMHost
.Name
Connect-VIServer
$HostName
-User
root
-password
P
@ssw0rd
Set-VMHostAccount
-UserAccount
root
-password
N3wPassword
Disconnect-VIServer
-Server
$HostName
-Confirm
:
$False
}
Get-VMHost
| Select Name, @{Name=
"LockdownModeEnabled"
;Expression={(
$_
).Extensiondata.Config.adminDisabled}} | ft
-auto
Get-VMHost
| %{(
$_
|
get-view
).ExitLockdownMode()}
Get-VMHost
| %{(
$_
|
get-view
).EnterLockdownMode()}
Get-VMHost
|
Foreach
{
Start-VMHostService
-HostService
(
$_
|
Get-VMHostService
| Where {
$_
.Key
-eq
"TSM-SSH"
} )}
Get-VMHost
|
Foreach
{
Stop-VMHostService
-HostService
(
$_
|
Get-VMHostService
| Where {
$_
.Key
-eq
"TSM-SSH"
} )
-Confirm
:
$false
}
Get-VMHost
|
Get-VMHostService
| Where {
$_
.Key
-eq
"TSM-SSH"
} | Select VMHost, Label, Policy, Running | ft
-auto
Get-VMHost
|
Get-VMHostFirewallException
| Where {
$_
.Name
-eq
"SSH Server"
} |
Set-VMHostFirewallException
-Enabled
:
$true
Get-VMHost
|
Get-AdvancedSetting
-Name
Config.HostAgent.log.level |
Set-AdvancedSetting
-Value
"info"
-Confirm
:
$false
Get-VMHost
|
Get-AdvancedSetting
-Name
Vpx.Vpxa.config.log.level |
Set-AdvancedSetting
-Value
"info"
-Confirm
:
$false
$esxcli
=
Get-EsxCli
-VMHost
servername
$esxcli
.system.syslog.reload()
Get-VMHost
|
Get-VMHostFirewallException
|?{
$_
.Name
-eq
'syslog'
} |
Set-VMHostFirewallException
-Enabled
:
$true
Get-VMHost
MyESXiHost |
Get-VMHostFirmware
-BackupConfiguration
-DestinationPath
“F:\”
Get-VMHost
MyESXiHost |
Set-VMHost
-State
Maintenance |
Set-VMHostFirmware
-Restore
-SourcePath
“F:\”
Get-VMHost
MyESXiHost |
Set-VMHostFirmware
-ResetToDefaults
Get-VMHost
MyESXiHost |
Get-Log
-Bundle
-DestinationPath
“F:\”
Get-VMHost
MyESXiHost |
Get-Log
hostd | Select
-ExpandProperty
Entries |
Out-File
“F:\hostd.log”
Get-VMHost
MyESXiHost |
Get-Log
vpxa | Select
-ExpandProperty
Entries |
Out-File
“F:\vpxa.log”
Get-VMHost
| Select Name,@{Name=
"Time"
;Expression={(
get-view
$_
.ExtensionData.configManager.DateTimeSystem).QueryDateTime()}}
Get-VMHost
| %{(
Get-View
$_
.ExtensionData.configManager.DateTimeSystem).UpdateDateTime((
Get-Date
-format
u)) }
Foreach
(
$esx
in
Get-VMhost
-Location
ClusterName | sort Name) {
$esx
|
Get-VMHostStorage
-RescanAllHBA
-rescanVMFS
-refresh
}
Get-VMHost
| Select Name, @{N=
"NTPServer"
;E={
$_
|
Get-VMHostNtpServer
}}, @{N=
"ServiceRunning"
;E={(
Get-VmHostService
-VMHost
$_
|
Where-Object
{
$_
.key
-eq
"ntpd"
}).Running}}
$oldntpservers
=
"192.168.0.1"
,
"192.168.0.2"
$newntpservers
=
"192.168.0.20"
,
"192.168.0.21"
Foreach
(
$vmhost
in
Get-VMHost
){
$vmhost
|Get
-VMHostService |?{
$_
.key
-eq
"ntpd"
}
|Stop
-VMHostService
-Confirm
:
$false
$vmhost
|Remove
-VMHostNtpServer
-NtpServer
$oldntpservers
-Confirm
:
$false
$vmhost
|Add
-VmHostNtpServer
-NtpServer
$newntpservers
$vmhost
|Get
-VMHostService |?{
$_
.key
-eq
"ntpd"
}
|Start
-VMHostService
}
Get-VMHost
|
Get-VMHostService
| ?{
$_
.key
-eq
"ntpd"
} |
Set-VMHostService
-Policy
"on"
-confirm
:
$false
Get-VMHost
|
Get-VMHostNetwork
|
Set-VMHostNetwork
-DnsAddress
[DNS1 IP address],[DNS2 IP address]
-Domain
[Domain name]
-SearchDomain
[Search domain name]
Get-VMHost
|
Set-VMHost
-State
Maintenance
Get-VMHost
|
Get-VMHostAuthentication
|
Set-VMHostAuthentication
-Domain
domain
-User
domainuser
-Password
password
-JoinDomain
-Confirm
:
$false
Get-VMHost
|
Get-VMHostAuthentication
|
Set-VMHostAuthentication
-LeaveDomain
-Confirm
:
$false
Get-VMHost
|
Get-VMHostAuthentication
| Select VMHost, DomainMembershipStatus, Domain | ft
-auto
Get-VMHost
|
Set-VMHost
-State Disconnected
-Confirm
:
$false
|
Remove-VMHost
-Confirm
:
$false
Add-VMHost
-Name
$VMHost
-Location
$Location
-Credential
$cred
-Force
-Confirm
:
$false
Move-Inventory
-Item
(
Get-VMHost
-Name
esxHost)
-Destination
(
Get-Cluster
-Name
clusterName)
Get-VM
| Select Name, @{N=”Cluster”;E={
Get-Cluster
-VM
$_
}},@{N=”ESX Host”;E={
Get-VMHost
-VM
$_
}},@{N=”Datastore”;E={
Get-Datastore
-VM
$_
}}
Get-Folder
"projects"
|
Get-VM
|
Where-Object
{
$_
.Guest.OSFullName
-like
"*Windows*"
-and
$_
.PowerState
-eq
"PoweredOn"
} | Sort Name
Get-VM
|
Get-VMResourceConfiguration
| Where {
$_
.CpuReservationMhz
-ne
0} | Select VM,CpuReservationMhz
Get-VM
|
Get-VMResourceConfiguration
| Where {
$_
.MemReservationMB
-ne
0} | Select VM,MemReservationMB
Get-VM
|
Get-VMResourceConfiguration
|
Where-Object
{
$_
.MemLimitMB
-ne
"-1"
} |
Set-VMResourceConfiguration
-MemLimitMB
$null
Get-VM
|
Get-VMResourceConfiguration
|
Where-Object
{
$_
.CpuLimitMhz
-ne
"-1"
} |
Set-VMResourceConfiguration
-CPULimitMhz
$null
Get-VM
|
Get-VMResourceConfiguration
|
Set-VMResourceConfiguration
-MemLimitMB
$null
-CpuLimitMhz
$null
Get-View
-ViewType
“VirtualMachine”
-Property
Guest,name
-filter
@{“Guest.ToolsStatus”=”toolsNotInstalled”;”Guest.GuestState”=”running”} | Select Name
Get-VIEvent
-maxsamples
10000 | Where {
$_
.Gettype().Name
-eq
“VmCreatedEvent”} | Select createdTime, UserName, FullFormattedMessage
Get-VIEvent
-maxsamples
10000 | Where {
$_
.Gettype().Name
-eq
“VmRemovedEvent”} | Select createdTime, UserName, FullFormattedMessage
Get-VIEvent
-maxsamples
10000
|where
{
$_
.Gettype().Name
-eq
"VmCreatedEvent"
-or
$_
.Gettype().Name
-eq
"VmBeingClonedEvent"
-or
$_
.Gettype().Name
-eq
"VmBeingDeployedEvent"
}
|Sort
CreatedTime
-Descending
|Select
CreatedTime, UserName,FullformattedMessage
-First
10
Get-VIEvent
-maxsamples
10000 | where {
$_
.Gettype().Name
-eq
"VmRemovedEvent"
} | Sort CreatedTime
-Descending
| Select CreatedTime, UserName, FullformattedMessage
-First
19
Get-VIEvent
-maxsamples
10000
-Start
(
Get-Date
).AddDays(-14) | where {
$_
.Gettype().Name
-eq
"VmCreatedEvent"
-or
$_
.Gettype().Name
-eq
"VmBeingClonedEvent"
-or
$_
.Gettype().Name
-eq
"VmBeingDeployedEvent"
}
|Sort
CreatedTime
-Descending
|Select
CreatedTime, UserName,FullformattedMessage
Get-VIEvent
-maxsamples
10000
-Start
(
Get-Date
).AddDays(-14)
|where
{
$_
.Gettype().Name
-eq
"VmRemovedEvent"
}
|Sort
CreatedTime
-Descending
|Select
CreatedTime, UserName,FullformattedMessage
Get-VM
| Where {
$_
.NumCPU
-gt
2} | Select Name, NumCPU
Get-View
-ViewType
VirtualMachine | Where {
-not
$_
.Config.Template} | Where{
$_
.Runtime.ConnectionState
-eq
“invalid”
-or
$_
.Runtime.ConnectionState
-eq
“inaccessible”} | Select Name
Move-VM
vm_name
-Destination
(
Get-VMHost
esxi_hostname)
Get-VM
vm_name |
Move-VM
-Datastore
(
Get-Datastore
datastore_name)
$destCluster
=
Get-Cluster
-Name
Cluster01
$destDatastore
=
Get-DatastoreCluster
-Name
Cluster_Datastore
$destFolder
= foldername
$sourceTemplate
=
Get-Template
-Name
W2K12_STD
1..6 |
Foreach
{
New-VM
-Name
test0
$_
-ResourcePool
$destCluster
-Location
$destFolder
-Template
$sourceTemplate
-Datastore
$destDatastore
-RunAsync
}
Get-VM
-Datastore
<SourceDatastore1> |
Move-VM
-Datastore
<TargetDatastore>
-runasync
Get-VM
|
Get-Snapshot
| Where {
$_
.Name.Contains(“Consolidate”) } |
Remove-Snapshot
Get-VM
|
Get-Snapshot
| Select VM,Name,Description,Created
Get-VM
|
Get-HardDisk
-DiskType
"RawPhysical"
,
"RawVirtual"
| Select Parent,Name,DiskType,ScsiCanonicalName,DeviceName
Get-Datastore
|
Where-Object
{
$_
.freespaceMB
-lt
100000}
Get-Datastore
|
Where-Object
{
$_
.freespaceGB
-lt
500
-and
$_
.Name
-notlike
"*localstorage*"
}
Get-Datastore
| Select Name, @{N=
"NumVM"
;E={@(
$_
|
Get-VM
).Count}} | Sort Name
New-PSDrive
-name
"mounteddatastore"
-Root
\
-PSProvider
VimDatastore
-Datastore
(
Get-Datastore
$datastore
)
Copy-Datastoreitem
$patchLocation
+
$patch
-Destination
mounteddatastore:
del mounteddatastore:
$patch
Remove-PSDrive
-name
"mounteddatastore"
-PSProvider
VimDatastore
Get-View
-ViewType
VirtualMachine | ?{
$_
.Runtime.ConnectionState
-eq
"invalid"
-or
$_
.Runtime.ConnectionState
-eq
"inaccessible"
} | %{
$_
.reload()}
Function
Get-VMHostHbaWWN
{
param
(
$VMHost
)
$EsxHostHba
=
get-vmhosthba
-VMHost
$VMHost
foreach
(
$hba
in
$EsxHostHba
){
$WWN
=
"{0:x}"
-f
$hba
.PortWorldWideName
$outObj
=
New-Object
PSObject
$outObj
|
Add-Member
-MemberType
NoteProperty
-Name
Name
-Value
$VMHost
$outObj
|
Add-Member
-MemberType
NoteProperty
-Name
WWNDec
-Value
$hba
.PortWorldWideName
$outObj
|
Add-Member
-MemberType
NoteProperty
-Name
WWNHex
-Value
$WWN
$outObj
|
Add-Member
-MemberType
NoteProperty
-Name
Device
-Value
$hba
.Device
$outObj
|
Add-Member
-MemberType
NoteProperty
-Name
Type
-Value
$hba
.Type
$outObj
|
Add-Member
-MemberType
NoteProperty
-Name
Model
-Value
$hba
.Model
$outObj
|
Add-Member
-MemberType
NoteProperty
-Name
Status
-Value
$hba
.Status
$outObj
}
}
Foreach
(
$esx
in
Get-VMHost
-Location
ClusterName) {
$esx
|
Get-VirtualSwitch
-Name
vSwitch1 |
New-VirtualPortGroup
-Name
"VLAN500"
-VlanId
500 }
export-vdswitch
$switch
-Withoutportgroups
-Description
“Backup of
$switch
without port groups”
-Destination
“c:\vSphere\
$switch
.without_portgroups.
$date
.zip“
export-vdswitch
$switch
-Description
“Backup of
$switch
with port groups”
-Destination
“c:\vSphere\
$switch
.with_portgroups.
$date
.zip“
get-vdswitch
$switch
|
Get-VDPortgroup
|
foreach
{
export-vdportgroup
-vdportgroup
$_
-Description
“Backup of port group $(
$_
.name)”
-destination
“c:\vSphere\$(
$_
.name).portgroup.
$date
.zip“}
get-vm
|
get-networkadapter
|
where-object
{
$_
.networkname
-like
"OldPortGroup"
} |
set-networkadapter
-networkname
"NewPortGroup"
-Confirm
:
$false
Get-VIEvent
-MaxSamples
100000 | ?{(
$_
-is
[VMware.Vim.UserLoginSessionEvent]
)
-or
(
$_
-is
[VMware.Vim.UserLogoutSessionEvent]
)} | %{
if
(
$_
-is
[VMware.Vim.UserLoginSessionEvent]
) {
$strLoginOrLogout
=
"logged in"
;
$strSourceIP
=
$_
.IpAddress
}
else
{
$strLoginOrLogout
=
"logged out"
;
$strSourceIP
=
$null
}
New-Object
-TypeName
PSObject
-Property
@{
UserName =
$_
.UserName
SourceIP =
$strSourceIP
Time =
$_
.CreatedTime
Action =
$strLoginOrLogout
}
} | Select UserName,SourceIP,Time,Action