Showing posts with label VMware. Show all posts
Showing posts with label VMware. Show all posts

Friday, July 27, 2018

vmware - Powershell get hardware inventory

Need a script to get serial numbers from my esxi hosts and a few other items
PowerState        : PoweredOn
Version           : 6.0.0
NumCpu            : 12
Cluster           : Clustername
MaxEVCMode        : intel-haswell
vCenter           : vcentername
Name              : esxi host name
Build             : buildnumber
CpuCoreCountTotal : 12
Model             : Hardware model number
MemoryTotalGB     : Total memory of esxi host
CpuModel          : cpu model
MemoryUsageGB     : memory usage at time of report
SerialNumber      : esxi host serial number
ConnectionState   : Connected
Prior to running this make sure you are up to date on your vmware powercli via
Install-Module -Name VMware.PowerCLI
Import-Module VMware.VimAutomation.Core
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false
Update-Module -Name VMware.PowerCLI
$vcenter = "vcenterhostname"

#Tony Unger
#Requires -Version 5

<#If script fails try to run the following commands 

Install-Module -Name VMware.PowerCLI
Import-Module VMware.VimAutomation.Core
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false
Update-Module -Name VMware.PowerCLI

#>

& 'C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'
$report =@()

Connect-VIServer -Server $vcenter


$vmHosts = get-vmhost | select Name,`
ConnectionState,`
PowerState,`
NumCpu,`
MemoryUsageGB,`
MemoryTotalGB,`
Version,`
Build,`
MaxEVCMode,`
@{N="Cluster";E={ $_.Parent}},`
@{N="vCenter";E={ ($_.uid).split("@")[1].split(":")[0]}} 

foreach ($vmHost in $vmHosts) {

$VMHardwareInfo = get-vmhosthardware -vmhost $vmHost.name |select Manufacturer,`
Model,`
SerialNumber,`
CpuModel,`
CpuCoreCountTotal

  $ESXiInfo = New-Object -TypeName PSObject -Property @{ 
                        Name = $vmHost.name 
                        ConnectionState = $vmHost.ConnectionState
                        PowerState = $vmHost.PowerState 
                        NumCpu = $vmHost.NumCpu 
                        MemoryUsageGB = $vmHost.MemoryUsageGB             
                        MemoryTotalGB = $vmHost.MemoryTotalGB 
                        Version = $vmHost.Version 
                        Build = $vmHost.Build
                        MaxEVCMode = $vmHost.MaxEVCMode 
                        Cluster = $vmHost.Cluster
                        vCenter = $vmHost.vCenter 
                        Model = $VMHardwareInfo.Model 
                        SerialNumber = $VMHardwareInfo.SerialNumber             
                        CpuModel = $VMHardwareInfo.CpuModel 
                        CpuCoreCountTotal = $VMHardwareInfo.CpuCoreCountTotal 
                       
                    }      
        $report += $ESXiInfo



}


  $report | export-csv -path c:\temp\ESXiHardwareInfo.csv -notype

Monday, May 9, 2016

Vmware: Powershell List all Datastores and virtual machines within


Purpose: Need to know if i had any empty datastores attached to my ESXi hosts. Came up with this one liner which will list out all datastores and the vms that are within empty or not.

get-datastore | select name, @{name="VM";e={get-datastore $_.name | get-vm } } | fl

Thursday, December 24, 2015

Powershell: VMware one liner to list total vm that are powered on each host

This one liner will query each host and id its cluster total vms and how many VMs are powered on
Get-VMHost | Select @{N="Cluster";E={Get-Cluster -VMHost $_}}, Name, @{N="Total_VMs";E={($_ | Get-VM).Count}},@{N="Powered_On";E={($_ | get-vm | ? {$_.powerstate -like "*ON*"}).count}} | Sort Cluster, Name | fl

Tuesday, July 8, 2014

Vmware vSphere Host Client 6.0

VMware Vsphere 6.0 Host Client 6.0 beta If you get this error running the Host Client:

uncaught node.js Error Error: EPERM, open 'c:\Program Files\VMware\HostClient\server\logs\VMware_Hostclient_logs.txt'


Run the client as an administrator

Monday, October 14, 2013

Powershell: Add NFS Exports to ESXi Hosts

Purpose:  Batch add all NFS exports to all ESXi hosts listed in $HostsToAddNFS variable.

#Add all ESXi Hosts names
$HostsToAddNFS = "hostname1","hostname2"

foreach ($Current_Host in $HostsToAddNFS )

{
connect-viserver $Current_Host
#add all NFS exports here
New-Datastore -Nfs -Name Volume1 -Path "/vol/volume1" -NfsHost x.x.x.x
New-Datastore -Nfs -Name Volume2 -Path "/vol/volume2" -NfsHost x.x.x.x


}

Monday, October 7, 2013

VMware: Change Load Balance Policy to IP Hash on ESXi Host Console

This command changes the vSwitch Load Balance Policy to iphash

esxcli network vswitch standard policy failover set -l iphash -v vSwitch0

This command changes the Port Group Management Network Load Balance Policy iphash

esxcli network vswitch standard portgroup policy failover set -p "Management Network" -l iphash

Thursday, February 21, 2013

Powershell - Find VM raw disk path to netapp lun


Purpose:
I needed a way get raw disk mappings from a vmware vm to the lun it is using on a netapp filer. One thing that needs to be edited is the SearchFilers function. Just add your runtime names and what filers they correspond to.Run this from powercli. and this requires the dataontap module which you can get from your now.netapp.com site Note: this script could be improved by gather all lun information from each filer instead of what i did the in the search filer function

              
param([string]$VC)
Import-Module DataONTAP
#########################
####Tony Unger
#########################


#################Functions##############################################
function GetNetappPath([string]$VMHost,[string]$VMHDLunID,[string]$RuntimeName) {
$stor = get-view (Get-VMHostStorage -VMHost $VMHost)
$IscsiWWN = $stor.StorageDeviceInfo.HostBusAdapter | where {$_.GetType().Name -eq "HostInternetScsiHba"} | Select -First 1 -expandproperty IScsiName
Write-Host "Found ISCSI NAME: $IscsiWWN on Host $VMHost"
$c = SearchFilers $IscsiWWN $RuntimeName
Write-Host "Netapp path: $c"
return $c
}

function SearchFilers([string]$IscsiWWN,[string]$RuntimeName){

switch -wildcard ($RuntimeName) 
    { 
       #Add all you runtime names here with what filer
        "vmhba40:C0:T0*" {$NetappFiler = "x.x.x.x"} 
        
        default {Write-host "Error! determining filer - $RuntimeName not found";read-host}
    }
 
 Write-host "Connecting to $NetappFiler"
connect-nacontroller $NetappFiler | out-null

$Igroup = get-nalun | Get-Nalunmap | Select Name,Initiators | Where {$_.Initiators -like $IscsiWWN} | Select -First 1 -expandproperty Name
$a = get-nalunbyigroup $Igroup $VMHDLunID | Select -ExpandProperty Path
Write-Host "SearchFilers function: $a"
$a = "$NetappFiler$a"
 return $a
 }
 
#################Functions##############################################
########################
$PathtoCSV = "C:\temp\VMlunID.csv"
########################
If ($VC){$strVC = $VC}
ELSE{$strVC = Read-Host "What is the Vcenter hostname?"}
If (!$strVC){Write-Host "Error: Vcenter not entered";exit}
Connect-VIServer -Server $strVC  #Enter your vCenter Server
"Hostname,DiskID,CapacityG,HD Path,ScsiCanonicalName,LunID,ESX Host,Netapp Path" > $PathtoCSV
Write-Host "Getting VM Information from Vcenter. This can take awhile based on how many VM you have"
$Disks = Get-VM | Get-HardDisk | Where {"RawPhysical","RawVirtual" -contains $_.DiskType}
Write-Host "Completed getting VM Information from Vcenter"
Write-Host ""
Write-Host "Querying SCSI Information for Vcenter"
#Adding all hosts Canonicalnames and runtimes names to a hash table 
#thanks http://communities.vmware.com/people/LucD
$lunTab = @{}
Get-VMHost  | Get-ScsiLun | %{
  $lunTab.Add($_.VMHost.Name + $_.CanonicalName, $_.RuntimeName)
}
Write-Host "Completed SCSI Information for Vcenter"

Foreach ($Disk in $Disks) {
$VMName = $Disk.Parent
write-host "VM Name: $VMName"
$VMHDname = $Disk.Name
write-host "Hard drive ID: $VMHDname"
$VMCapacityGB = $Disk.capacityGB
write-host "Lun Capacity(GB): $VMCapacityGB"
$VMHDPath = $Disk.filename
write-host "RAW VMDK Path: $VMHDPath"
$VMScsiCanonicalName = $Disk.ScsiCanonicalName
write-host "$ScsiCanonicalName: $VMScsiCanonicalName"
$VMHost = Get-VM $VMName | Select -ExpandProperty VMHost
write-host "ESX Host: $VMHost"
$RuntimeName = $lunTab[$VMHost.Name + $Disk.SCSICanonicalName]
write-host "RuntimeName: $RuntimeName"
$VMHDLunID = $RuntimeName.Substring($RuntimeName.LastIndexof(“L”)+1)
Write-Host "LunID: $VMHDLunID"
$NetappPath = GetNetappPath $VMHost $VMHDLunID $RuntimeName
Write-host "PathtoNetapp: $NetappPath"
$Combine = $VMName,$VMHDname,$VMCapacityGB,$VMHDPath,$VMScsiCanonicalName,$VMHDLunID,$VMHost,$NetappPath
Write-Host "Writing to $PathtoCSV"
$Combine -join "," >> $PathtoCSV
}

Thursday, January 17, 2013

Powershell VMware NTP


Purpose:
This is my first powershell script. It connects to a esxi host and removes all NTP servers dynamically then sets the new ones.

param([string]$VC, [string]$NTP1, [string]$NTP2)

If ($VC){$strVC = $VC}
ELSE{$strVC = Read-Host "What is the Vcenter hostname?"}
If (!$strVC){Write-Host "Error: Vcenter not entered";exit}

If ($NTP1){$strNTP1 = $NTP1}
ELSE{$strNTP1 = Read-Host "What is the first NTP server?"}
If (!$strNTP1){Write-Host "Error: NTP1 not entered need at least one NTP server";exit}

If ($NTP2){$strNTP2 = $NTP2}
ELSE{$strNTP2 = Read-Host "What is the second NTP server?"}


Connect-VIServer -Server $strVC  #Enter your vCenter Server

$NtpServers = @($strNTP1,$strNTP2)
#http://day2dayadmin.blogspot.com/2009/10/ntp-powershell-its-about-time.html
$ESXHosts = Get-VMHost | Select-Object Name,@{Name="NTP Server";Expression={$_ | Get-VMHostNtpServer}}, @{Name="NTP Running";Expression={($_ | Get-VMHostService | Where-Object {$_.key -eq "ntpd"}).Running}} | Sort-Object -Property "NTP Running", "NTP Server"



ForEach ($ESXHost in $ESXHosts) {
   
   
#Write-Host $ESXHost

   #remove current NTP servers
   Write-Host "Removing NTP servers" -BackgroundColor "Green" -ForegroundColor "Black"
   Remove-VMHostNtpServer -NtpServer $ESXHost."NTP Server" -VMHost $ESXHost.Name
   #Add new NTP servers
   Write-Host "Adding New NTP servers" -BackgroundColor "Green" -ForegroundColor "Black"
   Add-VmHostNtpServer -NtpServer $NtpServers -VMHost $ESXHost.Name
   #Stop NTP service   
   Write-Host "Stopping NTP Service" -BackgroundColor "Green" -ForegroundColor "Black"
   Get-VmHostService -VMHost $ESXHost.Name | Where-Object {$_.key -eq "ntpd"} | Restart-VMHostService
 
}
Write-Host "Done!"

Powershell: Microsoft Graph to add new roles to application registration

PowerShell Script Bulk add new roles to application registration in azure. Update $roles with ,Us...