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
#004 Exporter les groupes/users de tous les Delivery Group
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}
#003 Exporter les groupes/users d’un Delivery Group
asnp Citrix*;Get-BrokerAccessPolicyRule -DesktopGroupName 'Delivery_Group_Name' |Select -expandproperty IncludedUsers|Select-Object Name|Sort-Object -Unique
#002 Listing des utilisateurs associés aux VDI
asnp Citrix*;Get-BrokerDesktop|Select {$_.MachineName}, {$_.AssociatedUserFullNames}, {$_.AssociatedUserNames}|ft -AutoSize
#001 Task Schedule de redémarrage des XenApp d’un Delivery Group via Tags
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
VMware
#029 Exporter la liste des VMs en RDM
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
#028 Connaitre l’auteur d’un Snapshot
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")}}
#027 Afficher l’espace disque total des VMs et le Datastore associé
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
#026 Afficher l’adresse IPv4/v6 des VMs
Get-VM|ForEach{write-host $_ -ForegroundColor Yellow;$_.Guest.Nics.IPAddress;write-host}
#025 Afficher la MAC Address des VMs
Get-VM|ForEach{write-host $_ -ForegroundColor Yellow;Get-VM $_|Get-NetworkAdapter|select -ExpandProperty NetworkName;Get-VM $_|Get-NetworkAdapter|select -ExpandProperty MacAddress;write-host}
#024 Afficher le créateur des Snapshots (Ne fonctionne pas pour tous)
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 ""
#023 Lister les VMs avec Snapshot
Get-Snapshot *|Select-Object -Property VM,Name,Created,@{n="SizeGB";e={[math]::round($_.SizeGB)}},Description|Sort-Object -Property SizeGB -Descending|ft -AutoSize
#022 Enregistrer les VMs d’un DS sur un ESXi standalone (connect_viserver ESXi_IP)
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))}
#021 Trouver une VM via son adresse IP (VMware Tools)
Get-VM|Where-Object -FilterScript{$_.Guest.Nics.IPAddress -contains "1.2.3.4"}
#020 Supprimer la limite de mémoire RAM pour toutes les VMs
Get-VM|Get-VMResourceConfiguration|Where-Object{$_.MemLimitMB -ne ‘-1’}|Set-VMResourceConfiguration -MemLimitMB $Null
#019 Supprimer la réservation de mémoire RAM pour toutes les VMs
$guestConfig=New-Object VMware.Vim.VirtualMachineConfigSpec;$guestConfig.memoryReservationLockedToMax=$False;(Get-VM).ExtensionData.ReconfigVM_task($guestConfig);Get-VM|Get-VMResourceConfiguration|Set-VMResourceConfiguration -MemReservationMB 0
#018 Réserver la mémoire RAM pour toutes les VMs
$guestConfig=New-Object VMware.Vim.VirtualMachineConfigSpec;$guestConfig.memoryReservationLockedToMax=$True;(Get-VM).ExtensionData.ReconfigVM_task($guestConfig)
#017 Storage vMotion (Thick EagerZeroed) de toutes les VMs d’un Datastore + Check
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}}
#016 Affichage hRAM, vRAM + Ratio Overcommit RAM ESXi & Cluster
$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}
#015 Afficher le type de vNIC des VM du Cluster
$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
#014 Affichage vCPU, pCPu, % Usage + Ratio Overcommit CPU ESXi & Cluster (Simple / GridView)
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; $cpuVendor = $vmhost.ExtensionData.Hardware.CpuPkg[0].Vendor; $Multiple = if ($cpuVendor -like "*Intel*") { 0.75 } elseif ($cpuVendor -like "*AMD*") { 0.65 } else { 1 }; $hostthreads = [Math]::Round($vmhost.ExtensionData.Hardware.CpuInfo.NumCpuThreads * $Multiple); $vms | Where-Object { $_.VMHost -eq $vmhost } | ForEach { $vcpus += $_.NumCpu }; if ($vcpus -ne 0 -and $hostthreads -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 }; if ($totalhostthreads -ne 0) { $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 | Format-Table -AutoSize; $Output1 | Format-Table -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; $cpuVendor = $vmhost.ExtensionData.Hardware.CpuPkg[0].Vendor; $Multiple = if ($cpuVendor -like "*Intel*") { 0.75 } elseif ($cpuVendor -like "*AMD*") { 0.65 } else { 1 }; $hostthreads = [Math]::Round($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 -Title "VMware Cluster and Host Details"
#013 Exporter les infomations Hardware des ESXi (Hyperthreading non pris en compte !)
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
#012 Exporter le status des VMware Tools
Get-VM|Get-View|Select-Object @{N=”VM Name”;E={$_.Name}},@{Name=”Status Tools”;E={$_.Guest.ToolsStatus}}|Export-CSV C:\VMwareToolsStatus.csv
#011 Exporter la liste des PortGroups des Hosts du vCenter
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
#010 Afficher la liste des VMs avec l’Hardware Version (ex. 11)
Get-VM|ForEach-Object{Get-View $_.ID}|ForEach-Object{if($_.Config.Version -like "vmx-11"){Write-Host $_.Name -ForegroundColor Yellow}}
#009 Afficher les VMs en Thin Provisionning (true) / Thick Provisionning (false)
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""}}
#008 Afficher les informations (Espace utilisé, libre…) des Datastores
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
#007 Afficher la Version et l’Update des ESXi du Cluster
Get-Cluster Nom_du_Cluster|Get-VMHost|%{(Write-Host $_ " " -ForegroundColor Green),(Get-EsxCli -VMHost $_).system.version.get()|select Update,Version}|fl -autosize
#006 Activer SSH sur les ESXi du Cluster
Get-Cluster Nom_du_Cluster|Get-VMHost|Foreach{Start-VMHostService -HostService ($_|Get-VMHostService|Where {$_.Key -eq "TSM-SSH"}) -Confirm:$false}
#005 Désactiver SSH sur les ESXi du Cluster
Get-Cluster Nom_du_Cluster|Get-VMHost|Foreach{Stop-VMHostService -HostService ($_|Get-VMHostService|Where {$_.Key -eq "TSM-SSH"}) -Confirm:$false}
#004 Exporter en CSV la liste FQDN des VMs du Cluster
Get-Cluster Nom_du_Cluster|Get-VMHost|Get-VM|select @{N="DnsName"; E={$_.ExtensionData.Guest.Hostname}}|Export-CSV C:\FQDN_VMs.csv
#003 Afficher le Nom et l’IP de tout les ESXi du vCenter
Get-VMHost|Foreach{(Write-Host -NoNewLine $_ -ForegroundColor Green),($_|Get-AdvancedSetting -Name Vpx.Vpxa.config.vpxa.hostIp|Format-Wide -Property Value)}
#002 Ajouter un PortGroup avec VLAN à tout les ESXi du Cluster
Get-Cluster Nom_du_Cluster|Get-VMHost|Get-VirtualSwitch -Name vSwitch1|New-VirtualPortGroup -Name Nom_du_PG -VLanId Num_du_VLAN
#001 Supprimer un PortGroup de tout les ESXi du Cluster
Get-Cluster Nom_du_Cluster|Get-VMHost|Get-VirtualSwitch -Name vSwitch1|Get-VirtualPortGroup -Name Nom_du_PG|Remove-VirtualPortGroup -Confirm:$false