Friday, September 15, 2017

Adding new NIC to multiple virtual machines using PowerCLI script

Adding new NIC to multiple virtual machines using PowerCLI script

Connect-viserver vcenter_server
$VM = Get-Content D:\servers.txt
New-NetworkAdapter -VM $VM -Type Vmxnet3 -NetworkName network_label -WakeOnLan:$true -StartConnected:$true -Confirm:$false
Disconnect-VIServer -Confirm:$false

Tuesday, September 12, 2017

Restart management agents on an ESXi using PowerCLI script

Restart management agents on an ESXi using PowerCLI script

Get-VMHostService -VMHost esxi_server | where {$_.Key -eq "vpxa"} | Restart-VMHostService -Confirm:$false -ErrorAction SilentlyContinue

Enabling and Disabling SSH on ESXi hosts via PowerCLI scripts

Enabling and Disabling SSH on ESXi hosts via PowerCLI scripts

Enable SSH on Particular ESXi
Get-VMHost -Name esxi_server1,esxi_server2 | Foreach {Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )}

Disable SSH on Particular ESXi
Get-VMHost -Name esxi_server1,esxi_server2 | Foreach {Stop-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )}

SSH status on Particular ESXi
Get-VMHostService -VMHost esxi_server1,esxi_server2 | Where-Object {$_.Key -eq "TSM-SSH"}

Enable SSH on all ESXi
Get-VMHost | Foreach { Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )}

Disable SSH on all ESXi
Get-VMHost | Foreach {Stop-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )}

SSH status on all ESXi
Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH" } |select VMHost, Label, Running

Monday, September 11, 2017

Search vCenter server events using PowerCLI script

Search vCenter server events using PowerCLI script

Retrieves information about the events on a vSphere server

Syntax:

Get-VIEvent <vm_name> -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "<search content>"} |Select CreatedTime, UserName, FullFormattedMessage

Examples:

To search who renamed a virtual machine

Get-VIEvent Win2008 -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "* rename *"} |Select CreatedTime, UserName, FullFormattedMessage

To search for entries created by user "adminuser" in a virtual machine

Get-VIEvent Win2008 -MaxSamples([int]::MaxValue) | Where-Object {$_.UserName -like "adminuser"} |Select CreatedTime, UserName, FullFormattedMessage

To search for entries created in a particular date/time in a virtual machine

Get-VIEvent Win2008 -MaxSamples([int]::MaxValue) | Where-Object {$_.CreatedTime -like "* 9/11/2017 *"} |Select CreatedTime, UserName, FullFormattedMessage

PowerCLI script to rename Virtual machines from a CSV file

PowerCLI script to rename Virtual machines from a CSV file

$csv=Import-CSV rename.csv 

 foreach($vm in $csv)
{
Get-VM $($vm.oldname) |   Set-VM -name $($vm.newname) -confirm:$false

}

Rename.csv

oldname,newname
Win2008,WIN2008
winvm-test1,WINVM-TEST1
winvm-test2,WINVM-TEST2
winvm-test4,WINVM-TEST4

Output:

NamePowerStateNum CPUsMemoryGB
------------------------------
WIN2008PoweredOff12
WINVM-TEST1PoweredOff12
WINVM-TEST2PoweredOff12
WINVM-TEST4PoweredOff12

Virtual Machine VMDK report using PowerCLI

Virtual Machine VMDK report using PowerCLI

Script 1

$VMs = Get-VM *
$Data = @()
foreach ($VM in $VMs){
    $VMDKs = $VM | get-HardDisk
    foreach ($VMDK in $VMDKs) {
        if ($VMDK -ne $null){
            $CapacityGB = $VMDK.CapacityKB/1024/1024
            $CapacityGB = [int]$CapacityGB
            $into = New-Object PSObject
            Add-Member -InputObject $into -MemberType NoteProperty -Name VMname $VM.Name
            Add-Member -InputObject $into -MemberType NoteProperty -Name Datastore $VMDK.FileName.Split(']')[0].TrimStart('[')
            Add-Member -InputObject $into -MemberType NoteProperty -Name VMDK $VMDK.FileName.Split(']')[1].TrimStart('[')
            Add-Member -InputObject $into -MemberType NoteProperty -Name StorageFormat $VMDK.StorageFormat
            Add-Member -InputObject $into -MemberType NoteProperty -Name CapacityGB $CapacityGB
            $Data += $into
        }
    }
 }
$Data | Sort-Object VMname,Datastore,VMDK | Export-Csv -Path D:\scripts\VM-VMDKs.csv -NoTypeInformation

Output:


VMname Datastore VMDK StorageFormat CapacityGB
test3 datastore1  test3/test3.vmdk Thick 100
test3 datastore1  test3/test3_1.vmdk Thin 300
test4 datastore1  test4/test4.vmdk Thick 100
test4 datastore1  test4/test4_1.vmdk Thin 300


Script 2

$report = @()
foreach ($vm in Get-VM){
$view = Get-View $vm
if ($view.config.hardware.Device.Backing.ThinProvisioned -eq $true){
$row = '' | select Name, Provisioned, Total, Used, VMDKs, VMDKsize, DiskUsed, Thin
    $row.Name = $vm.Name
    $row.Provisioned = [math]::round($vm.ProvisionedSpaceGB , 2)
    $row.Total = [math]::round(($view.config.hardware.Device | Measure-Object CapacityInKB -Sum).sum/1048576 , 2)
    $row.Used = [math]::round($vm.UsedSpaceGB , 2)
    $row.VMDKs = $view.config.hardware.Device.Backing.Filename | Out-String
    $row.VMDKsize = $view.config.hardware.Device | where {$_.GetType().name -eq 'VirtualDisk'} | ForEach-Object {($_.capacityinKB)/1048576} | Out-String
    $row.DiskUsed = $vm.Extensiondata.Guest.Disk | ForEach-Object {[math]::round( ($_.Capacity - $_.FreeSpace)/1048576/1024, 2 )} | Out-String
    $row.Thin = $view.config.hardware.Device.Backing.ThinProvisioned | Out-String
$report += $row
}}
$report | Sort Name | Export-Csv -Path "D:\Scripts\test\Thin_Disks.csv"

Output:

Name Provisioned Total Used VMDKs VMDKsize DiskUsed Thin
test3 432.19 400 102.38 [datastore1] test3/test3.vmdk

[datastore1] test3/test3_1.vmdk
100

300
0
False

True
test4 432.19 400 102.38 [datastore1] test4/test4.vmdk

[datastore1] test4/test4_1.vmdk
100

300
0
False

True

Tuesday, August 30, 2016

HBA Storage path redundancy status using PowerCLI

HBA Storage path redundancy status


$views = Get-View -ViewType "HostSystem" -Property Name,Config.StorageDevice


$result = @()

foreach ($view in $views | Sort-Object -Property Name) {
    Write-Host "Checking" $view.Name

    $view.Config.StorageDevice.ScsiTopology.Adapter |?{ $_.Adapter -like "*FibreChannelHba*" } | %{
        $hba = $_.Adapter.Split("-")[2]

        $active = 0
        $standby = 0
        $dead = 0

        $_.Target | %{ 
            $_.Lun | %{
                $id = $_.ScsiLun

                $multipathInfo = $view.Config.StorageDevice.MultipathInfo.Lun | ?{ $_.Lun -eq $id }

                $a = [ARRAY]($multipathInfo.Path | ?{ $_.PathState -like "active" })
                $s = [ARRAY]($multipathInfo.Path | ?{ $_.PathState -like "standby" })
                $d = [ARRAY]($multipathInfo.Path | ?{ $_.PathState -like "dead" })

                $active += $a.Count
                $standby += $s.Count
                $dead += $d.Count
            }
        }

        $result += "{0},{1},{2},{3},{4}" -f $view.Name.Split(".")[0], $hba, $active, $dead, $standby
    }
}

ConvertFrom-Csv -Header "VMHost", "HBA", "Active", "Dead", "Standby" -InputObject $result | ft -AutoSize > "d:\temp\HBA\storagepaths $(get-date -f yyyy-MM-dd-hhmmss).txt"