Difference between revisions of "PowerCLI Powerhsell"

From Michael's Information Zone
Jump to navigation Jump to search
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Installation==
 
==Installation==
 +
===2021===
 +
Clean install of Serve 2016
 +
*Enable TLS support<ref>https://www.alitajran.com/unable-to-install-nuget-provider-for-powershell/</ref>
 +
<pre>
 +
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
 +
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
 +
</pre>
 +
*Restart powershell and install<ref>https://www.powershellgallery.com/packages/VMware.PowerCLI/12.3.0.17860403</ref>
 +
<pre>
 +
Install-Module -Name VMware.PowerCLI</pre>
 +
*Install Horizon view helper moduals<ref>https://blogs.vmware.com/euc/2020/01/vmware-horizon-7-powercli.html</ref>
 +
 +
===old===
 
Allow powershell to run powercli cmdlets
 
Allow powershell to run powercli cmdlets
 
<pre>
 
<pre>
 
Set-ExecutionPolicy RemoteSigned
 
Set-ExecutionPolicy RemoteSigned
Install-Module -Name VMware.PowerCLI
+
Install-Module -Name VMware.PowerCLI -allowclobber
 +
</pre>
 +
If you have not installed a valid certificate.<ref>https://communities.vmware.com/thread/584530</ref>
 +
<pre>Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false</pre>
 +
Download modules and place them in the modules directory.
 +
*Download from github
 +
*Unzip, move modules to "C:\ProgramFiles\WindowsPowerShell\Modules"
 +
 
 +
==Horizon==
 +
<ref>https://blogs.vmware.com/euc/2017/01/vmware-horizon-7-powercli-6-5.html</ref>It looks like vmware relies on the API to do most of the work with horizon. If you are not familiar with working with the API, like myself, this requires you to install additional scripts from github<ref>https://github.com/vmware/PowerCLI-Example-Scripts</ref>. In my case I had to change the execution policy to unrestricted as the vmware.hv.helper module was not signed. Fun stuff.
 +
===Installation===
 +
After installing PowerShell 7.1.3 <ref>https://github.com/PowerShell/PowerShell/releases/tag/v7.1.3</ref><ref>https://www.powershellgallery.com/packages/VMware.PowerCLI/12.3.0.17860403</ref>
 +
<pre>
 +
Install-Module -Name VMware.PowerCLI -AllowClobber
 +
</pre>
 +
 
 +
===List of Commands===
 +
<pre>
 +
PS C:\Windows\system32> Get-Command | where {$_.name -match "get-hv"}
 +
 
 +
CommandType    Name                                              Version    Source
 +
-----------    ----                                              -------    ------
 +
Function        Get-HvCommand                                      7.6.0.1... VMware.VimAutomation.HorizonView
 +
Function        Get-HVEntitlement                                  1.1        VMware.Hv.Helper
 +
Function        Get-HVEvent                                        1.1        VMware.Hv.Helper
 +
Function        Get-HVEventDatabase                                1.1        VMware.Hv.Helper
 +
Function        Get-HVFarm                                        1.1        VMware.Hv.Helper
 +
Function        Get-HVFarmSummary                                  1.1        VMware.Hv.Helper
 +
Function        Get-HVGlobalEntitlement                            1.1        VMware.Hv.Helper
 +
Function        Get-HVGlobalSession                                1.1        VMware.Hv.Helper
 +
Function        Get-HVGlobalSettings                              1.1        VMware.Hv.Helper
 +
Function        get-hvhealth                                      1.1        VMware.Hv.Helper
 +
Function        Get-HVHomeSite                                    1.1        VMware.Hv.Helper
 +
Function        Get-HVInternalName                                1.1        VMware.Hv.Helper
 +
Function        Get-HVlicense                                      1.1        VMware.Hv.Helper
 +
Function        get-HVlocalsession                                1.1        VMware.Hv.Helper
 +
Function        Get-HVMachine                                      1.1        VMware.Hv.Helper
 +
Function        Get-HVMachineSummary                              1.1        VMware.Hv.Helper
 +
Function        get-hvpodfederation                                1.1        VMware.Hv.Helper
 +
Function        Get-HVPool                                        1.1        VMware.Hv.Helper
 +
Function        Get-HVPoolSpec                                    1.1        VMware.Hv.Helper
 +
Function        Get-HVPoolSummary                                  1.1        VMware.Hv.Helper
 +
Function        Get-HVQueryFilter                                  1.1        VMware.Hv.Helper
 +
Function        Get-HVQueryResult                                  1.1        VMware.Hv.Helper
 +
Function        Get-HVResourceStructure                            1.1        VMware.Hv.Helper
 +
Function        get-hvsite                                        1.1        VMware.Hv.Helper
 +
</pre>
 +
===Creating a list of machines and users===
 +
====The problem====
 +
This is not as intuitive as other cmdlets. For example; you can run get-hvmachinesummary and get a list of machines.
 +
<pre>
 +
PS C:\Windows\system32> Get-HVMachineSummary
 +
 
 +
Machine        DesktopPool  DNS Name    User    Host            Agent Datastore  Status
 +
-------        -----------  --------    ----    ----            ----- ---------  ------
 +
lxn-vdi-02      Test        lxn-vdi-0... csp.t... 172.16.5.50    7.7.0 {vdiswa... DISCONNECTED
 +
lxn-vdi-03      Test        lxn-vdi-0... ad.my... 172.16.5.42    7.7.0 {vdiswa... AVAILABLE
 +
lxn-vdi-04      Test        lxn-vdi-0... csp.t... 172.16.5.42    7.7.0 {vdiswa... DISCONNECTED
 +
csp-vdi-70      Test        csp-vdi-7... ad.my... 172.16.5.42    7.4.0 {vdiswa... CONNECTED
 +
</pre>
 +
But then you can't select the Machine and User fields.
 +
<pre>
 +
PS C:\Windows\system32> Get-HVMachineSummary | select Machine,User
 +
 
 +
Machine User
 +
------- ----
 +
 
 +
</pre>
 +
====The fix====
 +
Ends up things are a little different<ref>https://www.rayheffer.com/quick-guide-horizon-7-api-vmware-powercli/</ref>. Using get-member was useful to finding that there is a "base" property, then under this there was all the information I needed.
 +
<pre>
 +
PS C:\Windows\system32> $machines=Get-HVMachineSummary
 +
PS C:\Windows\system32> $machines | get-member
 +
 
 +
 
 +
  TypeName: VMware.Hv.MachineNamesView
 +
 
 +
Name                    MemberType Definition
 +
----                    ---------- ----------
 +
Equals                  Method    bool Equals(System.Object obj)
 +
GetHashCode            Method    int GetHashCode()
 +
GetType                Method    type GetType()
 +
ToString                Method    string ToString()
 +
Base                    Property  VMware.Hv.MachineBase Base {get;set;}
 +
Id                      Property  VMware.Hv.MachineId Id {get;set;}
 +
ManagedMachineNamesData Property  VMware.Hv.MachineManagedMachineNamesData ManagedMachineNamesData {get;set;}
 +
MessageSecurityData    Property  VMware.Hv.MachineMessageSecurityData MessageSecurityData {get;set;}
 +
NamesData              Property  VMware.Hv.MachineNamesData NamesData {get;set;}
 +
 
 +
PS C:\Windows\system32> $machines.base
 +
 
 +
 
 +
Name                            : lxn-vdi-02
 +
DnsName                          : lxn-vdi-02.domain
 +
User                            : VMware.Hv.UserOrGroupId
 +
AccessGroup                      : VMware.Hv.AccessGroupId
 +
Desktop                          : VMware.Hv.DesktopId
 +
DesktopName                      : Test
 +
Session                          : VMware.Hv.SessionId
 +
BasicState                      : DISCONNECTED
 +
Type                            : MANAGED_VIRTUAL_MACHINE
 +
OperatingSystem                  : Windows 10
 +
OperatingSystemArchitecture      : 64_bit
 +
AgentVersion                    : 7.7.0
 +
AgentBuildNumber                : 11054235
 +
RemoteExperienceAgentVersion    :
 +
RemoteExperienceAgentBuildNumber : 11054235
 +
 
 +
PS C:\Windows\system32> $machines.namesdata
 +
 
 +
DesktopName UserName
 +
----------- --------
 +
Test        domain.tld\user1
 +
 
 +
</pre>
 +
Then to pull it all together by creating an array<ref>http://www.get-blog.com/?p=82</ref> to hold the values.
 +
<pre>
 +
##This will create a CSV of users and their assigned desktops.
 +
##Then save the file to C:\list.csv.
 +
 
 +
##Grab credentials
 +
$creds=Get-Credential -ErrorAction Stop
 +
 
 +
##Set execution policy, because vmware didn't sign their modules.
 +
Set-ExecutionPolicy Unrestricted
 +
 
 +
##Connect to horizon connection server
 +
Connect-HVServer -server localhost -Credential $creds -ErrorAction Stop
 +
##Import needed modules
 +
Import-Module VMware.Hv.Helper -ErrorAction stop
 +
Import-Module VMware.VimAutomation.HorizonView -ErrorAction stop
 +
Import-Module VMware.VimAutomation.Core -ErrorAction Stop
 +
##Get list of machines
 +
$machines=Get-HVMachineSummary
 +
##Create empt array
 +
$list=@()
 +
##Generate list of users and their machines.
 +
$machines | foreach {
 +
    $test=$null
 +
    $test=new-object system.object
 +
    $test | Add-Member -Type NoteProperty -Name User -Value $_.namesdata.username
 +
    Add-Member -InputObject $test -Type NoteProperty -Name Desktop -Value $_.base.name
 +
    $list+= $test
 +
    }
 +
$list | Export-Csv -NoClobber -NoTypeInformation -Path C:\list.csv
 +
</pre>
 +
===Horizon API===
 +
These are just notes on what I was able to figure out.<bR>
 +
====Example: Maintenance MOde====
 +
1.Connect to the server and create the object to hold all the commands and junk.
 +
<pre>
 +
$hvserver=connect-hvserver -server localhost -user user -password password -domain domain.tld
 +
$services=$hvserver.extensiondata
 +
</pre>
 +
2. Figure out the overload objects for the tasks you want to complete
 +
<pre>
 +
PS C:\Windows\system32> $services.machine.machine_entermaintenancemode
 +
 
 +
OverloadDefinitions
 +
-------------------
 +
void Machine_EnterMaintenanceMode(VMware.Hv.MachineID id)
 +
</pre>
 +
3. Create the object with the value you want.
 +
<pre>
 +
[Vmware.Hv.machineid]$machine=(get-machine -machinename testmachine).id
 +
</pre>
 +
4. Execute the task
 +
<pre>
 +
$services.machine.machine_entermaintenancemode($machine)
 +
</pre>
 +
====Example: Refresh all VMs for Composer based images====
 +
<pre>
 +
$hvserver=connect-hvserver -server localhost -user user -password password -domain domain.tld
 +
$services=$hvserver.extensiondata
 +
$spechelper=New-Object VMware.Hv.DesktopService+DesktopRefreshSpecHelper
 +
$specbase=New-Object VMware.Hv.DesktopRefreshSpec
 +
(get-hvmachine).base.desktop | foreach {
 +
[Vmware.Hv.DesktopID]$deskid=$_
 +
$spec=$specbase
 +
$spec.logoffsetting=$spechelper.LOGOFF_SETTING_FORCE_LOGOFF
 +
$spec.machines=$deskid
 +
$services.desktop.desktop_refresh($deskid, $spec)
 +
}
 
</pre>
 
</pre>
 +
 +
==vcenter==
 
Log into the server and start using commands
 
Log into the server and start using commands
 
<pre>
 
<pre>
Line 11: Line 208:
 
get-vm
 
get-vm
 
</pre>
 
</pre>
 +
===Add/Remove Hardware===
 +
====CPUs====
 +
<ref>https://www.vmguru.com/2015/12/powershell-friday-adding-cpus-powercli/</ref>
  
==Add/Remove Network Interfaces==
+
====Network Interfaces====
 
<ref>https://communities.vmware.com/message/1837346?tstart=0</ref>The fun with this is that you can not remove a NIC from a running VM using powercli, or at least not in the way you would think. You actually need to rebuild the config with the changes you want then apply it back. Instead of doing this I decided to just disconnect the unwanted NIC and install the desired one. Thankfully this is in a DHCP environment so no modifications needed in the guest OS.
 
<ref>https://communities.vmware.com/message/1837346?tstart=0</ref>The fun with this is that you can not remove a NIC from a running VM using powercli, or at least not in the way you would think. You actually need to rebuild the config with the changes you want then apply it back. Instead of doing this I decided to just disconnect the unwanted NIC and install the desired one. Thankfully this is in a DHCP environment so no modifications needed in the guest OS.
 
<br>
 
<br>
Line 28: Line 228:
 
</pre>
 
</pre>
  
==Clone VM==
+
====RAM====
 +
Lets say you created a default config with 6GB of RAM because you were an ignorant moron, and now you need to increase this to a proper 8GB.<ref>https://www.vmguru.com/2015/12/powershell-friday-adding-memory/</ref> The fun part is that the -MemoryGB option does not autofill, so you would be hard pressed to find this option without the Google. Gotta love Linux man pages!
 +
<pre>
 +
Get-Folder -Name <your folder>| get-vm | where {$_.MemoryGB -eq "6.00"} | Set-VM -MemoryGB 8 -confirm:$false
 +
</pre>
 +
 
 +
===Clone VM===
 
<pre>
 
<pre>
 
new-vm -name <new_machine_name> -vm <source_machine_name> -vmhost <ip or host name of the physical host, not the controller>
 
new-vm -name <new_machine_name> -vm <source_machine_name> -vmhost <ip or host name of the physical host, not the controller>
Line 36: Line 242:
 
ie clone_vm.ps1 3
 
ie clone_vm.ps1 3
 
<pre>
 
<pre>
param([string]$num1)
+
#######################################################################
 +
###08-14-2017
 +
###Specified the datastore to use because vmware is stupid, and was
 +
###putting the VMs in the swap volume. Idiots.
 +
#######################################################################
 +
$num1 = Read-Host -Prompt "How many VMs? "
 
#Athenticate
 
#Athenticate
 
$cred=get-credential
 
$cred=get-credential
Line 62: Line 273:
 
Do{
 
Do{
 
$usehost=(Get-VMHost | Get-Random)
 
$usehost=(Get-VMHost | Get-Random)
new-vm -name vm-name-$base -vm clonefromthis -VMHost $usehost -RunAsync -WhatIf
+
new-vm -name vm-name-$base -vm clonefromthis -VMHost $usehost -Datastore <datastore to be used> -RunAsync -WhatIf
 
$num453=[int]$num453 + [int]1
 
$num453=[int]$num453 + [int]1
 
}until ([int]$num453 -eq [int]$num1)
 
}until ([int]$num453 -eq [int]$num1)
 
</pre>
 
</pre>
==Delete VM==
+
 
 +
===Delete VM===
 +
<pre>
 +
remote-vm -vm <namme_of_vm> -deletepermanently  -confirm:$false
 +
</pre>
 +
===Shutdown / Start VM===
 +
To shutdown and start VMs<ref>https://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/Shutdown-VMGuest.html</ref>
 +
<pre>
 +
Get-Folder -Name pool02 | get-vm | where {$_.MemoryGB = "6.00"} | Shutdown-VM -Confirm:$false
 +
Get-Folder -Name <your folder> | get-vm | Start-VM -Confirm:$false
 +
</pre>
 +
 
 +
===Snapshot Management===
 +
This will create snapshots and remove any from a month ago.<br>
 +
TODO:Update to remove any older than a month. Also need email notification of failed attempts<br>
 
<pre>
 
<pre>
remote-vm -vm <namme_of_vm> -deletepermanently  -confirm $false
+
#################################################
 +
##For creating a snapshot every week, and
 +
##removing snapshots older than a month.
 +
#################################################
 +
$newdate=date -Format MMddyyy
 +
$olddate=date (date).addmonths(-1) -Format MMddyyy
 +
$vsipass= echo 'password' | ConvertTo-SecureString -AsPlainText -force
 +
$cred=New-Object System.Management.Automation.PSCredential -ArgumentList username,$vsipass
 +
Connect-VIServer -server localhost -Credential $cred
 +
##Create new snapshots
 +
Get-VM | New-Snapshot -Name "vsi-$newdate" -RunAsync -whatif
 +
##Delete old snapshots
 +
$vsisnapshots=Get-VM | Get-Snapshot
 +
if ($vsisnapshots.Name -like "vsi-$olddate"){$_.Name | remove-Snapshot -RunAsync -whatif}
 +
Remove-Variable newdate
 +
Remove-Variable olddate
 +
exit 0
 +
##Check logs
 +
sleep -Seconds 3600
 +
get-vmhost | Get-Log -Key vmkernel | select $_.Entries -ExpandProperty Entries | where {$_ -like '*Failed*snapshot*'}
 
</pre>
 
</pre>
 +
==Various Tasks==
 +
===Modify VM hardware for Disconnected Desktops===

Latest revision as of 11:30, 28 June 2021

Installation

2021

Clean install of Serve 2016

  • Enable TLS support[1]
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
  • Restart powershell and install[2]
Install-Module -Name VMware.PowerCLI
  • Install Horizon view helper moduals[3]

old

Allow powershell to run powercli cmdlets

Set-ExecutionPolicy RemoteSigned
Install-Module -Name VMware.PowerCLI -allowclobber

If you have not installed a valid certificate.[4]

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

Download modules and place them in the modules directory.

  • Download from github
  • Unzip, move modules to "C:\ProgramFiles\WindowsPowerShell\Modules"

Horizon

[5]It looks like vmware relies on the API to do most of the work with horizon. If you are not familiar with working with the API, like myself, this requires you to install additional scripts from github[6]. In my case I had to change the execution policy to unrestricted as the vmware.hv.helper module was not signed. Fun stuff.

Installation

After installing PowerShell 7.1.3 [7][8]

Install-Module -Name VMware.PowerCLI -AllowClobber

List of Commands

PS C:\Windows\system32> Get-Command | where {$_.name -match "get-hv"}

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-HvCommand                                      7.6.0.1... VMware.VimAutomation.HorizonView
Function        Get-HVEntitlement                                  1.1        VMware.Hv.Helper
Function        Get-HVEvent                                        1.1        VMware.Hv.Helper
Function        Get-HVEventDatabase                                1.1        VMware.Hv.Helper
Function        Get-HVFarm                                         1.1        VMware.Hv.Helper
Function        Get-HVFarmSummary                                  1.1        VMware.Hv.Helper
Function        Get-HVGlobalEntitlement                            1.1        VMware.Hv.Helper
Function        Get-HVGlobalSession                                1.1        VMware.Hv.Helper
Function        Get-HVGlobalSettings                               1.1        VMware.Hv.Helper
Function        get-hvhealth                                       1.1        VMware.Hv.Helper
Function        Get-HVHomeSite                                     1.1        VMware.Hv.Helper
Function        Get-HVInternalName                                 1.1        VMware.Hv.Helper
Function        Get-HVlicense                                      1.1        VMware.Hv.Helper
Function        get-HVlocalsession                                 1.1        VMware.Hv.Helper
Function        Get-HVMachine                                      1.1        VMware.Hv.Helper
Function        Get-HVMachineSummary                               1.1        VMware.Hv.Helper
Function        get-hvpodfederation                                1.1        VMware.Hv.Helper
Function        Get-HVPool                                         1.1        VMware.Hv.Helper
Function        Get-HVPoolSpec                                     1.1        VMware.Hv.Helper
Function        Get-HVPoolSummary                                  1.1        VMware.Hv.Helper
Function        Get-HVQueryFilter                                  1.1        VMware.Hv.Helper
Function        Get-HVQueryResult                                  1.1        VMware.Hv.Helper
Function        Get-HVResourceStructure                            1.1        VMware.Hv.Helper
Function        get-hvsite                                         1.1        VMware.Hv.Helper

Creating a list of machines and users

The problem

This is not as intuitive as other cmdlets. For example; you can run get-hvmachinesummary and get a list of machines.

PS C:\Windows\system32> Get-HVMachineSummary

Machine         DesktopPool  DNS Name     User     Host            Agent Datastore  Status
-------         -----------  --------     ----     ----            ----- ---------  ------
lxn-vdi-02      Test         lxn-vdi-0... csp.t... 172.16.5.50     7.7.0 {vdiswa... DISCONNECTED
lxn-vdi-03      Test         lxn-vdi-0... ad.my... 172.16.5.42     7.7.0 {vdiswa... AVAILABLE
lxn-vdi-04      Test         lxn-vdi-0... csp.t... 172.16.5.42     7.7.0 {vdiswa... DISCONNECTED
csp-vdi-70      Test         csp-vdi-7... ad.my... 172.16.5.42     7.4.0 {vdiswa... CONNECTED

But then you can't select the Machine and User fields.

PS C:\Windows\system32> Get-HVMachineSummary | select Machine,User

Machine User
------- ----

The fix

Ends up things are a little different[9]. Using get-member was useful to finding that there is a "base" property, then under this there was all the information I needed.

PS C:\Windows\system32> $machines=Get-HVMachineSummary
PS C:\Windows\system32> $machines | get-member


   TypeName: VMware.Hv.MachineNamesView

Name                    MemberType Definition
----                    ---------- ----------
Equals                  Method     bool Equals(System.Object obj)
GetHashCode             Method     int GetHashCode()
GetType                 Method     type GetType()
ToString                Method     string ToString()
Base                    Property   VMware.Hv.MachineBase Base {get;set;}
Id                      Property   VMware.Hv.MachineId Id {get;set;}
ManagedMachineNamesData Property   VMware.Hv.MachineManagedMachineNamesData ManagedMachineNamesData {get;set;}
MessageSecurityData     Property   VMware.Hv.MachineMessageSecurityData MessageSecurityData {get;set;}
NamesData               Property   VMware.Hv.MachineNamesData NamesData {get;set;}

PS C:\Windows\system32> $machines.base


Name                             : lxn-vdi-02
DnsName                          : lxn-vdi-02.domain
User                             : VMware.Hv.UserOrGroupId
AccessGroup                      : VMware.Hv.AccessGroupId
Desktop                          : VMware.Hv.DesktopId
DesktopName                      : Test
Session                          : VMware.Hv.SessionId
BasicState                       : DISCONNECTED
Type                             : MANAGED_VIRTUAL_MACHINE
OperatingSystem                  : Windows 10
OperatingSystemArchitecture      : 64_bit
AgentVersion                     : 7.7.0
AgentBuildNumber                 : 11054235
RemoteExperienceAgentVersion     :
RemoteExperienceAgentBuildNumber : 11054235

PS C:\Windows\system32> $machines.namesdata

DesktopName UserName
----------- --------
Test        domain.tld\user1

Then to pull it all together by creating an array[10] to hold the values.

##This will create a CSV of users and their assigned desktops.
##Then save the file to C:\list.csv.

##Grab credentials
$creds=Get-Credential -ErrorAction Stop

##Set execution policy, because vmware didn't sign their modules.
Set-ExecutionPolicy Unrestricted

##Connect to horizon connection server
Connect-HVServer -server localhost -Credential $creds -ErrorAction Stop
##Import needed modules
Import-Module VMware.Hv.Helper -ErrorAction stop
Import-Module VMware.VimAutomation.HorizonView -ErrorAction stop
Import-Module VMware.VimAutomation.Core -ErrorAction Stop
##Get list of machines
$machines=Get-HVMachineSummary
##Create empt array
$list=@()
##Generate list of users and their machines.
$machines | foreach {
    $test=$null
    $test=new-object system.object
    $test | Add-Member -Type NoteProperty -Name User -Value $_.namesdata.username
    Add-Member -InputObject $test -Type NoteProperty -Name Desktop -Value $_.base.name
    $list+= $test
    }
$list | Export-Csv -NoClobber -NoTypeInformation -Path C:\list.csv

Horizon API

These are just notes on what I was able to figure out.

Example: Maintenance MOde

1.Connect to the server and create the object to hold all the commands and junk.

$hvserver=connect-hvserver -server localhost -user user -password password -domain domain.tld
$services=$hvserver.extensiondata

2. Figure out the overload objects for the tasks you want to complete

PS C:\Windows\system32> $services.machine.machine_entermaintenancemode

OverloadDefinitions
-------------------
void Machine_EnterMaintenanceMode(VMware.Hv.MachineID id)

3. Create the object with the value you want.

[Vmware.Hv.machineid]$machine=(get-machine -machinename testmachine).id

4. Execute the task

$services.machine.machine_entermaintenancemode($machine)

Example: Refresh all VMs for Composer based images

$hvserver=connect-hvserver -server localhost -user user -password password -domain domain.tld
$services=$hvserver.extensiondata
$spechelper=New-Object VMware.Hv.DesktopService+DesktopRefreshSpecHelper
$specbase=New-Object VMware.Hv.DesktopRefreshSpec
(get-hvmachine).base.desktop | foreach {
 [Vmware.Hv.DesktopID]$deskid=$_
 $spec=$specbase
 $spec.logoffsetting=$spechelper.LOGOFF_SETTING_FORCE_LOGOFF
 $spec.machines=$deskid
 $services.desktop.desktop_refresh($deskid, $spec)
}

vcenter

Log into the server and start using commands

$cred=get-credential
connect-viserver -server localhost -credential $cred
get-vm

Add/Remove Hardware

CPUs

[11]

Network Interfaces

[12]The fun with this is that you can not remove a NIC from a running VM using powercli, or at least not in the way you would think. You actually need to rebuild the config with the changes you want then apply it back. Instead of doing this I decided to just disconnect the unwanted NIC and install the desired one. Thankfully this is in a DHCP environment so no modifications needed in the guest OS.

The reason for this change was due to Windows VMs losing connectivity to domain traffic using the emulated Intel NIC. According to our MS rep, we needed to change the NICs from e1000 to vmxnet3.

connect-viserver -server localhost
$list=get-vm
foreach ($i in $list){
$type=$i | Get-NetworkAdapter | select -ExpandProperty Type
if ($type -like "e1000"){
$i | get-networkadapter -name "Network adapter 1" | set-networkadapter -connected $false -startconnected $false -confirm $false
$i | new-networkadapter -networkname VDI -StartConnected -type vmxnet3}
}

RAM

Lets say you created a default config with 6GB of RAM because you were an ignorant moron, and now you need to increase this to a proper 8GB.[13] The fun part is that the -MemoryGB option does not autofill, so you would be hard pressed to find this option without the Google. Gotta love Linux man pages!

Get-Folder -Name <your folder>| get-vm | where {$_.MemoryGB -eq "6.00"} | Set-VM -MemoryGB 8 -confirm:$false

Clone VM

new-vm -name <new_machine_name> -vm <source_machine_name> -vmhost <ip or host name of the physical host, not the controller>

Sample Script : format is <scriptname>.ps1 <number of VMs to make>
ie clone_vm.ps1 3

#######################################################################
###08-14-2017
###Specified the datastore to use because vmware is stupid, and was
###putting the VMs in the swap volume. Idiots.
#######################################################################
$num1 = Read-Host -Prompt "How many VMs? "
#Athenticate
$cred=get-credential
connect-viserver -server localhost -credential $cred

#Check for enough space
$neededspace=([int]$num1 * '<space needed for each VM>')
$space12=(datastore -Name <datastore name> | Select-Object FreeSpaceGB | ForEach-Object { $_.FreeSpaceGB })
if ( $space12 -lt $neededspace ){
echo "Not enought space, $space12 is available and you need $neededspace"
exit
}
#The following assumes you are creating VMs with an incrementing name based on a [a-z]+-[a-z]+-[0-9]+ format.
#Get last VM created for incremental creation
get-folder -name <folder name> | get-vm | select name > C:\powercli\outtest
$vms=(Get-Content C:\powercli\outtest | %{"$($_.Split('-')[2])"})
$base=0
foreach($line in $vms){
if ([int]$line -gt [int]$base){
$base=$line
}
}
$base = [int]$base + 1
$num453=0
Do{
$usehost=(Get-VMHost | Get-Random)
new-vm -name vm-name-$base -vm clonefromthis -VMHost $usehost -Datastore <datastore to be used> -RunAsync -WhatIf
$num453=[int]$num453 + [int]1
}until ([int]$num453 -eq [int]$num1)

Delete VM

remote-vm -vm <namme_of_vm> -deletepermanently  -confirm:$false

Shutdown / Start VM

To shutdown and start VMs[14]

Get-Folder -Name pool02 | get-vm | where {$_.MemoryGB = "6.00"} | Shutdown-VM -Confirm:$false
Get-Folder -Name <your folder> | get-vm | Start-VM -Confirm:$false

Snapshot Management

This will create snapshots and remove any from a month ago.
TODO:Update to remove any older than a month. Also need email notification of failed attempts

#################################################
##For creating a snapshot every week, and
##removing snapshots older than a month.
#################################################
$newdate=date -Format MMddyyy
$olddate=date (date).addmonths(-1) -Format MMddyyy
$vsipass= echo 'password' | ConvertTo-SecureString -AsPlainText -force
$cred=New-Object System.Management.Automation.PSCredential -ArgumentList username,$vsipass
Connect-VIServer -server localhost -Credential $cred
##Create new snapshots
Get-VM | New-Snapshot -Name "vsi-$newdate" -RunAsync -whatif
##Delete old snapshots
$vsisnapshots=Get-VM | Get-Snapshot
if ($vsisnapshots.Name -like "vsi-$olddate"){$_.Name | remove-Snapshot -RunAsync -whatif}
Remove-Variable newdate
Remove-Variable olddate
exit 0
##Check logs
sleep -Seconds 3600
get-vmhost | Get-Log -Key vmkernel | select $_.Entries -ExpandProperty Entries | where {$_ -like '*Failed*snapshot*'}

Various Tasks

Modify VM hardware for Disconnected Desktops