One|Liner

How to easy enhance your Administration

Le OneLiner est un script compressé en une seule ligne !
Ceci lui permettant de se lancer via un simple copier/coller dans une fenêtre VMware PowerCLI ou Microsoft PowerShell.

Il est devenu au fil du temps une évidence pour les Administrateurs :
– Simple à utiliser
– Facilement compréhensible
– Challenge de toujours faire plus court

Citrix

[toggles class= »yourcustomclass »]

[toggle title= »#004 Exporter les groupes/users de tous les Delivery Group » class= »out »]

asnp Citrix*;$DGroups=(Get-BrokerDesktopGroup|Select PublishedName).PublishedName;ForEach($DGroup in $DGroups){Get-BrokerAccessPolicyRule -DesktopGroupName $DGroup|Select -expandproperty IncludedUsers|Select-object Name|Sort-Object -Unique}

[/toggle]

[toggle title= »#003 Exporter les groupes/users d’un Delivery Group » class= »out »]

asnp Citrix*;Get-BrokerAccessPolicyRule -DesktopGroupName 'Delivery_Group_Name' |Select -expandproperty IncludedUsers|Select-Object Name|Sort-Object -Unique

[/toggle]

[toggle title= »#002 Listing des utilisateurs associés aux VDI » class= »out »]

asnp Citrix*;Get-BrokerDesktop|Select {$_.MachineName}, {$_.AssociatedUserFullNames}, {$_.AssociatedUserNames}|ft -AutoSize

[/toggle]

[toggle title= »#001 Task Schedule de redémarrage des XenApp d’un Delivery Group via Tags » class= »out »]

asnp Citrix*;New-BrokerRebootScheduleV2 -Name "Wednesday Reboot for SSL" -DesktopGroupName "Nom_du_Delivery_Group" -Day Wednesday -Frequency Weekly -StartTime "2:00" -RebootDuration 120 -RestrictToTag Nom_du_Tag -WarningDuration 30 -WarningRepeatInterval 10 -WarningMessage "This machine will restart in %m% minutes. Please save your work." -WarningTitle "Warning: Reboot Pending" -Enabled $true

[/toggle] [/toggles]

VMware

[toggles class= »yourcustomclass »] [toggle title= »#029 Exporter la liste des VMs en RDM » class= »out »]

Get-VM|Get-HardDisk|Where-Object{$_.DiskType -like “Raw*”}|Select @{N=”ESXi”;E={$_.Parent.VMhost.Name}},@{N=”VM”;E={$_.Parent}},Name,DiskType,@{N=”SCSI ID”;E={$_.ScsiCanonicalName}},Filename,CapacityGB|Export-Csv C:\VMware\RDM-list.csv -NoTypeInformation

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#028 Connaitre l’auteur d’un Snapshot » class= »out »]

foreach($snap in Get-VM|Get-Snapshot){$snapevent=Get-VIEvent -Entity $snap.VM -Types Info -Finish $snap.Created -MaxSamples 1|Where-Object{$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'};if ($snapevent -ne $null){Write-Host(""+ $snap.VM + " - Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + " by " + $snapevent.UserName +".")}else {Write-Host(""+ $snap.VM + " - Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + ". This event is not in vCenter events database")}}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#027 Afficher l’espace disque total des VMs et le Datastore associé » class= »out »]

Get-VM|Select-Object Name,@{n="VMDKsGB";e={[math]::round((Get-HardDisk -VM $_|Measure-Object -Sum CapacityGB).Sum)}},@{n="DSName";e={(Get-Datastore -VM $_)}}|sort VMDKsGB|ft -AutoSize

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#026 Afficher l’adresse IPv4/v6 des VMs » class= »out »]

Get-VM|ForEach{write-host $_ -ForegroundColor Yellow;$_.Guest.Nics.IPAddress;write-host}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#025 Afficher la MAC Address des VMs » class= »out »]

Get-VM|ForEach{write-host $_ -ForegroundColor Yellow;Get-VM $_|Get-NetworkAdapter|select -ExpandProperty NetworkName;Get-VM $_|Get-NetworkAdapter|select -ExpandProperty MacAddress;write-host}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#024 Afficher le créateur des Snapshots (Ne fonctionne pas pour tous) » class= »out »]

Get-Snapshot *|Where-Object {$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'};Write-Host "" $snap.VM ": Snapshot" $snap "created on" $snap.Created.DateTime "by" $snapevent.UserName ""

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#023 Lister les VMs avec Snapshot » class= »out »]

Get-Snapshot *|Select-Object -Property VM,Name,Created,@{n="SizeGB";e={[math]::round($_.SizeGB)}},Description|Sort-Object -Property SizeGB -Descending|ft -AutoSize

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#022 Enregistrer les VMs d’un DS sur un ESXi standalone (connect_viserver ESXi_IP) » class= »out »]

Get-View -ViewType Datastore -Property Name,Browser -Filter @{"Name" = "^DS_Name$"}|%{(Get-View $_.Browser).SearchDatastoreSubFolders($("[" + $_.Name + "]"),(New-Object VMware.Vim.HostDatastoreBrowserSearchSpec -Property @{matchPattern=("*.vmx")}))}|%{(Get-View -ViewType Folder -Property Name -Filter @{"Name" = "^vm"}).GetType().GetMethod("RegisterVM_Task").Invoke($(Get-View -ViewType Folder -Property Name -Filter @{"Name" = "^vm"}),@($($_.FolderPath + "/" + $_.File.Path),$null,$false,(Get-View -ViewType ResourcePool -Property Name -Filter @{"Name" = "Resources"}).MoRef,$null))}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#021 Trouver une VM via son adresse IP (VMware Tools) » class= »out »]

Get-VM|Where-Object -FilterScript{$_.Guest.Nics.IPAddress -contains "1.2.3.4"}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#020 Supprimer la limite de mémoire RAM pour toutes les VMs » class= »out »]

Get-VM|Get-VMResourceConfiguration|Where-Object{$_.MemLimitMB -ne ‘-1’}|Set-VMResourceConfiguration -MemLimitMB $Null

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#019 Supprimer la réservation de mémoire RAM pour toutes les VMs » class= »out »]

$guestConfig=New-Object VMware.Vim.VirtualMachineConfigSpec;$guestConfig.memoryReservationLockedToMax=$False;(Get-VM).ExtensionData.ReconfigVM_task($guestConfig);Get-VM|Get-VMResourceConfiguration|Set-VMResourceConfiguration -MemReservationMB 0

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#018 Réserver la mémoire RAM pour toutes les VMs » class= »out »]

$guestConfig=New-Object VMware.Vim.VirtualMachineConfigSpec;$guestConfig.memoryReservationLockedToMax=$True;(Get-VM).ExtensionData.ReconfigVM_task($guestConfig)

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#017 Storage vMotion (Thick EagerZeroed) de toutes les VMs d’un Datastore + Check » class= »out »]

Get-Datastore "Source_DS"|Get-VM|Move-VM -DiskStorageFormat "EagerZeroedThick" -Datastore "Destination_DS" –RunAsync
Get-Datastore "Source_DS"|Get-VM|Select Name,@{N="Datastore";E={$_|Get-Datastore}}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#016 Affichage hRAM, vRAM + Ratio Overcommit RAM ESXi & Cluster » class= »out »]

$getcluster=Get-Cluster;ForEach($clustername in $getcluster){ write-host;write-host $clustername -ForegroundColor Yellow;$vmhosts=Get-Cluster $clustername|Get-VMHost;$Output=@();$Output1=@();$totalhostram=0;$totalvram=0;$totalratio=0;ForEach ($vmhost in $vmhosts){$ratio=$null;$hostvram=0;$vms = Get-Cluster $clustername | Get-VMHost $vmhost | Get-VM | where {$_.PowerState -eq "PoweredOn"};ForEach ($vm in $vms){$vram=0;$vram = $vm.MemoryGB;$hostvram += $vram};$totalvram += $hostvram;$hostram="{0:N0}" -f ($vmhost.memorytotalGB);$ratio = "{0:N1}" -f ($hostvram/$hostram);$hvalue= New-Object psobject;$hvalue| Add-Member -MemberType Noteproperty "Hostname" -value $vmhost.name;$hvalue| Add-Member -MemberType Noteproperty "hRAM" -Value $hostram;$hvalue| Add-Member -MemberType Noteproperty "vRAM" -Value $hostvram;$hvalue| Add-Member -MemberType Noteproperty "Ratio" -Value $ratio;$Output+=$hvalue;$totalhostram += $hostram};$totalratio = "{0:N1}" -f ($totalvram/$totalhostram);$cvalue= New-Object psobject;$cvalue| Add-Member -MemberType Noteproperty "Hostname" -value "Cluster";$cvalue| Add-Member -MemberType Noteproperty "hRAM" -Value $totalhostram;$cvalue| Add-Member -MemberType Noteproperty "vRam" -Value $totalvram;$cvalue| Add-Member -MemberType Noteproperty "Ratio" -Value $totalratio;$Output1+=$cvalue;$Output|ft -autosize;$Output1|ft –autosize}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#015 Afficher le type de vNIC des VM du Cluster » class= »out »]

$Cluster_Name="Nom_du_Cluster";$vms=Get-Cluster $Cluster_Name|Get-VM;$Output=@();foreach($vm in $vms){$value=Get-NetworkAdapter -VM $vm;$hvalue=New-Object psobject;$hvalue|Add-Member -MemberType Noteproperty "VM Name" -value $vm.Name;$hvalue| Add-Member -MemberType Noteproperty "vNIC" -Value $value.Type;$Output+=$hvalue};$Output|Format-Table -AutoSize

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#014 Affichage vCPU, pCPu, % Usage + Ratio Overcommit CPU ESXi & Cluster (Simple / GridView) » class= »out »]

ForEach ($clustername in Get-Cluster) { Write-Host; Write-Host $clustername -ForegroundColor Yellow; $vmhosts = Get-Cluster $clustername | Get-VMHost; $vms = Get-Cluster $clustername | Get-VM | Where-Object { $_.PowerState -eq "PoweredOn" }; $Output = @(); $Output1 = @(); $totalvcpus = 0; $totalhostthreads = 0; $totalratio = 0; ForEach ($vmhost in $vmhosts) { $vcpus = 0; $ratio = $null; $ht = $vmhost.HyperThreadingActive; if ($ht -eq "True") { $Multiple = "0.75" } else { $Multiple = "1" }; $hostthreads = $vmhost.extensiondata.hardware.cpuinfo.numcputhreads * $Multiple; $vms | Where-Object { $_.vmhost -like $vmhost } | ForEach { $vcpus += $_.numcpu }; if ($vcpus -ne "0") { $ratio = "{0:N1}" -f ($vcpus / $hostthreads) }; $cpuUsagePercent = [Math]::Round((Get-Stat -Entity $vmhost -Stat "cpu.usage.average" -Start (Get-Date).AddDays(-7) -IntervalMins 60 | Measure-Object -Property Value -Average).Average); $hvalue = New-Object psobject; $hvalue | Add-Member -MemberType NoteProperty "Hostname" -value $vmhost.name; $hvalue | Add-Member -MemberType NoteProperty "pCPU" -Value $hostthreads; $hvalue | Add-Member -MemberType NoteProperty "vCPU" -Value $vcpus; $hvalue | Add-Member -MemberType NoteProperty "Ratio" -Value $ratio; $hvalue | Add-Member -MemberType NoteProperty "CPU %" -Value $cpuUsagePercent; $Output += $hvalue; $totalhostthreads += $hostthreads; $totalvcpus += $vcpus; $totalratio = "{0:N1}" -f ($totalvcpus / $totalhostthreads) }; $cvalue = New-Object psobject; $cvalue | Add-Member -MemberType NoteProperty "Hostname" -value "Cluster"; $cvalue | Add-Member -MemberType NoteProperty "pCPU" -Value $totalhostthreads; $cvalue | Add-Member -MemberType NoteProperty "vCPUs" -Value $totalvcpus; $cvalue | Add-Member -MemberType NoteProperty "Ratio" -Value $totalratio; $Output1 += $cvalue; $Output | ft -AutoSize; $Output1 | ft -AutoSize }
$Result = @(); ForEach ($clustername in Get-Cluster) { $Output = @(); $Output1 = @(); $totalvcpus = 0; $totalhostthreads = 0; $totalratio = 0; ForEach ($vmhost in (Get-Cluster $clustername | Get-VMHost)) { $vcpus = 0; $ratio = $null; $ht = $vmhost.HyperThreadingActive; if ($ht -eq "True") { $Multiple = 0.75 } else { $Multiple = 1 }; $hostthreads = $vmhost.extensiondata.hardware.cpuinfo.numcputhreads * $Multiple; (Get-Cluster $clustername | Get-VM | Where-Object { $_.vmhost -like $vmhost }) | ForEach { $vcpus += $_.numcpu }; if ($vcpus -ne 0) { $ratio = "{0:N1}" -f ($vcpus / $hostthreads) }; $cpuUsagePercent = [Math]::Round((Get-Stat -Entity $vmhost -Stat "cpu.usage.average" -Start (Get-Date).AddDays(-7) -IntervalMins 60 | Measure-Object -Property Value -Average).Average); $hvalue = New-Object psobject; $hvalue | Add-Member -MemberType NoteProperty "Hostname" -value $vmhost.name; $hvalue | Add-Member -MemberType NoteProperty "pCPU" -Value $hostthreads; $hvalue | Add-Member -MemberType NoteProperty "vCPU" -Value $vcpus; $hvalue | Add-Member -MemberType NoteProperty "Ratio" -Value $ratio; $hvalue | Add-Member -MemberType NoteProperty "CPU %" -Value $cpuUsagePercent; $Output += $hvalue; $totalhostthreads += $hostthreads; $totalvcpus += $vcpus; $totalratio = "{0:N1}" -f ($totalvcpus / $totalhostthreads) }; $cvalue = New-Object psobject; $cvalue | Add-Member -MemberType NoteProperty "Hostname" -value "Cluster"; $cvalue | Add-Member -MemberType NoteProperty "pCPU" -Value $totalhostthreads; $cvalue | Add-Member -MemberType NoteProperty "vCPUs" -Value $totalvcpus; $cvalue | Add-Member -MemberType NoteProperty "Ratio" -Value $totalratio; $Output1 += $cvalue; $Result += $Output; $Result += $Output1 }; $Result | Out-GridView

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#013 Exporter les infomations Hardware des ESXi (Hyperthreading non pris en compte !) » class= »out »]

Get-VMHost|Sort Name|Get-View|Select Name,@{N=“HW Type“;E={$_.Hardware.SystemInfo.Vendor+ “ “ + $_.Hardware.SystemInfo.Model}},@{N=“Socket(s)“;E={$_.Hardware.CpuInfo.NumCpuPackages}},@{N=“Core(s)“;E={[math]::round($_.Hardware.CpuInfo.NumCpuCores / $_.Hardware.CpuInfo.NumCpuPackages)}},@{N=“Logical CPUs“;E={[math]::round($_.Hardware.CpuInfo.NumCpuCores)}},@{N=“CPU“;E={"" + [math]::round($_.Hardware.CpuInfo.Hz / 1000000000, 2) + " Ghz"}},@{N=“RAM“;E={“” + [math]::round($_.Hardware.MemorySize / 1GB, 0) + “ GB“}} | Export-Csv C:\ESXi_HW.csv

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#012 Exporter le status des VMware Tools » class= »out »]

Get-VM|Get-View|Select-Object @{N=”VM Name”;E={$_.Name}},@{Name=”Status Tools”;E={$_.Guest.ToolsStatus}}|Export-CSV C:\VMwareToolsStatus.csv

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#011 Exporter la liste des PortGroups des Hosts du vCenter » class= »out »]

Get-VMHost|Foreach-Object{$ClusterName=$_.Parent.Name; $HostName = $_;Get-VirtualPortGroup $_|Select-Object @{N="Cluster";E={$ClusterName}},@{N="Host";E={$HostName}},Name,VLanId}|Export-CSV C:\PortGroup_List.csv

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#010 Afficher la liste des VMs avec l’Hardware Version (ex. 11) » class= »out »]

Get-VM|ForEach-Object{Get-View $_.ID}|ForEach-Object{if($_.Config.Version -like "vmx-11"){Write-Host $_.Name -ForegroundColor Yellow}}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#009 Afficher les VMs en Thin Provisionning (true) / Thick Provisionning (false) » class= »out »]

Get-VM|ForEach{$View=Get-View $_;if($View.config.hardware.Device.Backing.ThinProvisioned -eq $true){Get-Cluster -VM $_|Select Name;write-host $_ -ForegroundColor Yellow;write-host""}}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#008 Afficher les informations (Espace utilisé, libre…) des Datastores » class= »out »]

Get-Datastore|Select Name,@{N="Total_GB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}},@{N="Used_GB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},@{N=”Free_GB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity - ($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace))/1GB,0)}},@{N='%_Free';E={[Math]::Round($_.FreespaceGB/$_.CapacityGB*100,0)}}|Sort-Object -Property %_Free|Format-Table -AutoSize

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#007 Afficher la Version et l’Update des ESXi du Cluster » class= »out »]

Get-Cluster Nom_du_Cluster|Get-VMHost|%{(Write-Host $_ " " -ForegroundColor Green),(Get-EsxCli -VMHost $_).system.version.get()|select Update,Version}|fl -autosize

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#006 Activer SSH sur les ESXi du Cluster » class= »out »]

Get-Cluster Nom_du_Cluster|Get-VMHost|Foreach{Start-VMHostService -HostService ($_|Get-VMHostService|Where {$_.Key -eq "TSM-SSH"}) -Confirm:$false}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#005 Désactiver SSH sur les ESXi du Cluster » class= »out »]

Get-Cluster Nom_du_Cluster|Get-VMHost|Foreach{Stop-VMHostService -HostService ($_|Get-VMHostService|Where {$_.Key -eq "TSM-SSH"}) -Confirm:$false}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#004 Exporter en CSV la liste FQDN des VMs du Cluster » class= »out »]

Get-Cluster Nom_du_Cluster|Get-VMHost|Get-VM|select @{N="DnsName"; E={$_.ExtensionData.Guest.Hostname}}|Export-CSV C:\FQDN_VMs.csv

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#003 Afficher le Nom et l’IP de tout les ESXi du vCenter » class= »out »]

Get-VMHost|Foreach{(Write-Host -NoNewLine $_ -ForegroundColor Green),($_|Get-AdvancedSetting -Name Vpx.Vpxa.config.vpxa.hostIp|Format-Wide -Property Value)}

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#002 Ajouter un PortGroup avec VLAN à tout les ESXi du Cluster » class= »out »]

Get-Cluster Nom_du_Cluster|Get-VMHost|Get-VirtualSwitch -Name vSwitch1|New-VirtualPortGroup -Name Nom_du_PG -VLanId Num_du_VLAN

[/toggle] [/toggles]

[toggles class= »yourcustomclass »] [toggle title= »#001 Supprimer un PortGroup de tout les ESXi du Cluster » class= »out »]

Get-Cluster Nom_du_Cluster|Get-VMHost|Get-VirtualSwitch -Name vSwitch1|Get-VirtualPortGroup -Name Nom_du_PG|Remove-VirtualPortGroup -Confirm:$false

[/toggle] [/toggles]