вторник, 21 февраля 2023 г.

Extract ESXi iDrac and BIOS versions and their IPs with PowerCLI

 cls

$viserver = "vCenter"

$User = "USER"

$Pass = "PASSWORD"

Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue | Out-Null

$UnsortedVMHosts = (Get-VMHost).name

$VMHostNames = $UnsortedVMHosts | sort #  | select -First 3

$Array = @()

Write-Host


foreach($ESXiName in $VMHostNames)

    {

    Connect-VIServer -Server $viserver -User $User -Password $Pass

    $OneLineArray = New-Object "PSCustomObject"

    

    $ESXiName

    $VMHost = Get-VMHost $ESXiName

    $VMView = $VMHost | Get-View

    $BiosInfo = $VMView.Hardware.BiosInfo

        

    $Vendor = $VMHost.ExtensionData.Hardware.SystemInfo.Vendor

    $Model = $VMHost.ExtensionData.Hardware.SystemInfo.Model

    $BiosVersion = $BiosInfo.BiosVersion

    $ReleaseDate = $BiosInfo.ReleaseDate

    $EsxIP = (Get-VMHostNetworkAdapter -VMHost $VMHost -VMKernel -Name vmk0).IP

    $EsxiVer = $VMHost.ExtensionData.Config.Product.FullName

    $vCenter = $VMHost.ExtensionData.Client.ServiceContent.About.FullName


    Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue

    Connect-VIServer -Server $ESXiName -User root -Password "PASSWORD" # | Out-Null

    Connect-VIServer -Server $ESXiName -User Administrator -Password "PASSWORD" # | Out-Null

    $esxcli = Get-EsxCli -VMHost $ESXiName -V2

    $iDracIP = $esxcli.hardware.ipmi.bmc.get.Invoke().IPv4Address

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "ESXiName" -Value $ESXiName

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "Vendor" -Value $Vendor

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "Model" -Value $Model

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "BiosVersion" -Value $BiosVersion

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "ReleaseDate" -Value $ReleaseDate

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "EsxIP" -Value $EsxIP

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "iDracIP" -Value $iDracIP

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "EsxiVer" -Value $EsxiVer

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "vCenter" -Value $vCenter

    $Array += $OneLineArray

    }

$Array | OGV -Title "ESXi Hosts"

$Array | Export-Csv "C:\Temp\ESXiHosts-report.csv" -NoTypeInformation -UseCulture

ii "C:\Temp\report.csv"

    

Import VMs from vCenter to vCloud vApp with PowerCLI

cls

#  Import VMs from vCenter to vCloud vApp with PowerCLI

$SourceFolder = "SOURCE"

$DestVApp = "DEST_VAPP"

$VIserver = 'VCENTER.DOMAIN.COM"

$CIserver = 'VCLOUD.DOMAIN.COM"

$UVI = "VCENTER_USER"

$PVI = "VCENTER_PASSWORD"

$UCI = "VCLOUD_USER"

$PCI = "VCLOUD_PASSWORD"

Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue | Out-Null

Connect-VIServer -Server $VIserver -User $UVI -Password $PVI

Disconnect-CIServer * -Confirm:$false -ErrorAction SilentlyContinue

Connect-CIServer -Server $CIserver -User $UCI -Password $PCI

$VMs = Get-Folder $SourceFolder | Get-VM | sort 

$CIvappObj = Get-CIVApp $DestVApp

foreach ($VM in $VMs) 

    {

    $VM.name

    $CIvappObj | Import-CIVApp -VM $vm -NoCopy:$False -RunAsync -Confirm $false

    }


среда, 1 февраля 2023 г.

wget-in-Windows

Sometimes we want to use Linux command in Windows.

One of them is wget - an easy way to download sites and pages from the Internet.
There is also cURL.

Do we have them in Windows?
Natively, no, but...

The 1st way - PowerShell has command Invoke-WebRequest.

# Using PowerShell for getting a Google page:
Invoke-WebRequest http://www.google.com/ -OutFile c:\google.html
# or
Invoke-WebRequest http://www.google.com/ | -OutFile c:\google.html

# Using wget as an alias for Invoke-WebRequest (not a real wget)
wget http://www.google.com/ -OutFile c:\google.html
wget http://www.google.com/ | -OutFile c:\google.html

# Showing the results
ii c:\google.html

The 2nd way - we can install wget as a regular program - as an exe file or with Chocolatey.
Chocolatey is an extremely powerful tool with a large range of features and options.
Here is a good site about Chocolatey:
https://howchoo.com/chocolatey/install-pc-programs-using-powershell-and-chocolatey

Let's see the installation with Chocolatey.

# First install Chocolatey with PowerShell (what you see is one long line in PowerShell):
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

This will tell your system to download Chocolatey from the web and install it.
You can upgrade Chocolatey itself by running this command:
choco upgrade chocolatey



Then you can install wget with Chocolatey from PowerShell or even from a regular CMD.
choco install wget

# Use wget to pull a website to your local machine
wget --mirror --convert-links --adjust-extension --page-requisites c:\google.html





Installing Chocolatey

Chocolatey is a program or method for installing packages/programs/application in Windows in the simple way.

For installing Chocolatey we need to use PowerShell command:

 Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

All this is one long line.



четверг, 7 июля 2022 г.

Deleting Entry in vCloud PostgreSQL

If you're familiar with vCloud Director, you know that occasionally there can be stubborn, outdated entries. This happens when a VM, vApp, or template won't respond to the standard GUI methods. When this happens, many people turn to the VMware helpdesk for assistance. However, did you know that you can tackle this issue yourself, even if you're not a DBA? All you need is to take a backup or snapshot of the vCloud VM and make a backup of the Postgre DB with SSH/CLI. So, let's dive in!

1. First go in the GUI to the VM and take IDs of vApp and VM.

VM
e8b68b96-8c8a-4e2c-bf35-17ddc534e451
vApp
a893f027-6bee-4e54-8125-b09c04319c72

2. First enter to the vCloud PhotonOS with SSH (putty, MobaXterm).


4. Then enter to vCloud PostgreSQL with this command:
sudo -i -u postgres psql vcloud
(where postgres is a user, and vcloud is a DB)


5. =Deleting VM=
VM id: e8b68b96-8c8a-4e2c-bf35-17ddc534e451

delete from vapp_vm where id = ' 'e8b68b96-8c8a-4e2c-bf35-17ddc534e451'';
XXX
ERROR:  update or delete on table "vapp_vm" violates foreign key constraint "fk_vap_vm_scl_me2vapp_vm" on table "vapp_vm_sclass_metrics"
DETAIL:  Key (id)=(ac706161-1b79-4a41-a2d8-abfff0754758) is still referenced from table "vapp_vm_sclass_metrics".

We need to delete the VM from the referenced table vapp_vm_sclass_metrics, 
instead of id we will use vapp_vm_id.
select * from vapp_vm_sclass_metrics where vapp_vm_id = 'e8b68b96-8c8a-4e2c-bf35-17ddc534e451';
delete from vapp_vm_sclass_metrics where vapp_vm_id = 'e8b68b96-8c8a-4e2c-bf35-17ddc534e451';

delete from vapp_vm where id = 'e8b68b96-8c8a-4e2c-bf35-17ddc534e451';

Again we have referenced table

select * from guest_personalization_info where vapp_vm_id = 'e8b68b96-8c8a-4e2c-bf35-17ddc534e451';

delete from guest_personalization_info where vapp_vm_id = 'e8b68b96-8c8a-4e2c-bf35-17ddc534e451';


delete from vapp_vm where id = 'e8b68b96-8c8a-4e2c-bf35-17ddc534e451';

In GUI

We see that there is no VM, but again we cannot to delete the vApp.
Success for VM, but still not for vApp.

6. =VAPP=

We are taking vApp id: 'a893f027-6bee-4e54-8125-b09c04319c72'

https://vcloud.algotec.co.il/tenant/support/vdcs/7f306f1b-a035-4015-81d8-d3783b5a3128/vapp/vapp-a893f027-6bee-4e54-8125-b09c04319c72/vcd-vapp-vms

Now we can work with sg_id, which is more relaible than a name.

select * from vm_container where sg_id='a893f027-6bee-4e54-8125-b09c04319c72';
We see the vApp still exists:


Now we will try to delete the vApp.
delete from vm_container where sg_id = 'a893f027-6bee-4e54-8125-b09c04319c72';


We cannot delete the vApp because of another table that contains vApp id - "vapp_logical_resource".
Let's go there.
select * from vapp_logical_resource where vapp_id='a893f027-6bee-4e54-8125-b09c04319c72';


Now deleting.
delete from vapp_logical_resource where vapp_id='a893f027-6bee-4e54-8125-b09c04319c72';


Good, let's check in DB.
select * from vapp_logical_resource where vapp_id='a893f027-6bee-4e54-8125-b09c04319c72';


Now we return back to the original place in trial to delete the vApp.
First, little check:
select * from vm_container where sg_id='a893f027-6bee-4e54-8125-b09c04319c72';
Now the actual deleing:
delete from vm_container where sg_id='a893f027-6bee-4e54-8125-b09c04319c72';


Seems we have deleted the vApp!..., but let's check that it is a real thing:
select * from vm_container where sg_id='a893f027-6bee-4e54-8125-b09c04319c72';

Now we sure it is deleted, but let's check in GUI too:


Bravo!





воскресенье, 19 июня 2022 г.

VMware vCenter HA Master with PowerCLI

If you manage vSphere, sometimes you can wonder - who is this master of crime, master/primary ESXi Host, which is responsible for HA cluster.

воскресенье, 29 мая 2022 г.

Discard VMs suspended state in vCloud with PowerCLI

If you have experienced difficulty in removing a VM from a suspended state within a vApp, then you will understand what I am referring to. However, if this is not something you have encountered, then you may skip this.


Unfortunately, we often encounter problematic states such as suspended states. However, this script can be very useful in completely discarding the suspended state.

==START==
cls
Write-Host "=Suspended VMs=" -BackgroundColor DarkRed -ForegroundColor Yellow
Write-Host "This script shows all suspended VMs on vCenter VCRD" -ForegroundColor Yellow
$host.UI.RawUI.WindowTitle = "=Suspended VMs to Stop="

Write-Host ""
$vCenter = ""
$User = ""
$Pass = ""
$RealPass = ""
$GetVM = ""
$Suspended = ""
$ToStop = ""
$ToStopCount = ""
$StopVM = ""

#=== Policy/Configuration ===#
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false | Out-Null
$ErrorActionPreference= 'SilentlyContinue'
$WarningActionPreference= 'SilentlyContinue'
Add-PsSnapin VMware.VimAutomation.Core

#=== My Login Credentials ===#
$vCenter = 'VCRD.algotec.co.il'
$User = $env:UserName
$SecurePass = Read-Host -AsSecureString "Please enter password for $UserName and wait..."
$Pass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePass))
If(-not$Pass){Write-Host "You must enter a password!" -ForegroundColor Yellow -BackgroundColor Black; Write-Host; Break}

Write-Host "I am connecting you to vCenter..." -ForegroundColor Yellow
Disconnect-VIServer * -Confirm:$false 
If (!($global:defaultviserver.IsConnected)){$VIServerConnected = Connect-VIServer -Server $vCenter -User $User -Password $Pass}
If(!($VIServerConnected)){Write-Host "$vCenter is NOT Connected! Try another password." -BackgroundColor Black -ForegroundColor Yellow; Break}
Else{Write-Host "vCenter is Connected!" -ForegroundColor Green -BackgroundColor DarkRed}
Write-Host 

Write-Host "I am getting all Suspended VMs..." -ForegroundColor Yellow
$GetVM = Get-VM
$Suspended = $GetVM | Where-Object -Property PowerState -EQ 'Suspended'
If(!$Suspended){Write-Host "Okay. There is NO Suspended VMs!" -ForegroundColor Yellow -BackgroundColor DarkRed; Break}
$Suspended | Select Name, Powerstate, VApp, VMHost
$Count = $Suspended.count
Write-Host
Write-Host "The number of Suspended VMs is $Count" -ForegroundColor Green
$ToStop = $Suspended | Out-GridView -Title "Suspended VMs to Stop" -PassThru
Write-Host
$ToStopCount = $ToStop.count
Write-Host "The number of VMs selected to Stop $ToStopCount" -ForegroundColor Green
Write-Host

foreach($StopVM in $ToStop)
    {
    $StopVM | Stop-VM -Confirm:$false | Out-Null
    $StoppedName = $StopVM.name
    Write-Host "$StoppedName is stopped" -ForegroundColor White -BackgroundColor DarkRed
    }

Write-Host "$ToStopCount VMs are stopped" -ForegroundColor Black -BackgroundColor White
==FINISH==

If there are no suspended VMs in your environment, you are lucky.