Backup and Export Distributed Switch configuration

The below script was prepared to Backup and Export Distributed Switch configuration:

Connect-VIServer -Server vCenter_FQDN/IP_Address -Credential (Get-Credential)
$vDSwitchDetails = Get-VDSwitch
$vDSwitchNames = $vDSwitchDetails.Name
$datestamp = Get-Date -Format “MM-dd-yyyy”
Foreach ($vDSwitchName in $vDSwitchNames)
{
$DestiationDir = “C:\Users\Administrator\Desktop\Scripts\vDSExport\”+ $datestamp + “\” + $vDSwitchName + “\”
New-Item -Path $DestiationDir -ItemType “Directory” -Force
$filename= $DestiationDir + $vDSwitchName + “.zip”
Get-VDSwitch -Name $vDSwitchName | Export-VDSwitch -Description “vDS Backup” -Destination $filename
}
Disconnect-VIServer -Confirm:$false

Export LUN IDs with Path Selection Policy

Featured

In this post we are sharing a PowerCLI script that we used to export a list of LUNs attached to ESXi hosts in a cluster along with the details of Path Selection Policy selected for the LUN and CommandsToSwitchPath parameter set for the LUNs.

Script was tested on below versions:

  • PowerCLI version 11.5.0 build 14912921
  • vSphere 6.7U3

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_IP/FQDN -Credential $cred

$getCluster = Get-Cluster -Name "Cluster_Name"
$getESXis = $getCluster | Get-VMHost
$printluns=@()

foreach ($getESXi in $getESXis)
{
    $getLUNsforESXi = Get-VMhost $getESXi | Get-ScsiLun -LunType disk | Select VMHost, CanonicalName, MultipathPolicy, CommandsToSwitchPath
    $printluns = $printluns + $getLUNsforESXi
}

$printluns | Export-Csv C:\users\Administrator\Desktop\PathtoSaveFile.csv -NoTypeInformation 

Disconnect-VIServer -Confirm:$false

Just replace the vCenter_Server_IP_Address/FQDN, Cluster_Name and Path of CSV File and run the script to generate a report of LUNs mapped to all ESXi hosts in a vSphere cluster in your environment. BOOM!!

Happy Scripting!!

Assign Custom Attributes to VMs

Featured

There was a requirement in one of the projects to assign Custom Attributes to multiple Virtual Machines hosted in a vSphere Environment. We wrote a PowerCLI script to Assign the Custom Attributes to Virtual Machines using a CSV file which had all the details of the Custom Attributes.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_Server_IP_Address/FQDN -Credential $cred
$files = Import-CSV "C:\Users\testuser\Desktop\CustomAttributes.csv"
foreach ($file in $files)
{
$vm = Get-VM -Name $file.VMName
$vm | Set-Annotation -CustomAttribute "AppName" -Value $file.AppName
$vm | Set-Annotation -CustomAttribute "Owner" -Value $file.Owner
$vm | Set-Annotation -CustomAttribute "AppDL" -Value $file.AppDL
$vm | Set-Annotation -CustomAttribute "Env" -Value $file.ENv
}

Disconnect-VIServer -Confirm:$false

Input CSV File:

CustomAttributes.csv

Just replace the vCenter_Server_IP_Address/FQDN and Path of CSV File and run the script to assign Custom Attributes to Virtual Machines in vSphere environment. BOOM!!

Happy Scripting!!

Script to Create Multiple Datastores

Featured

There was a requirement in one of the projects to create multiple (approx. 60) datastores using a PowerCLI script. We wrote a PowerCLI script to perform this operation on multiple LUNs presented to all ESXi hosts in the cluster.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer “vCenter_Server_IP_Address/FQDN” -Credential (Get-Credential)

$datanames = Import-Csv ‘C:\Users\Admin\Desktop\File_with_datastore_name_NAA_Ids.csv

foreach ($dataname in $datanames)
{
$dataname.Datastore_Name
$dataname.Naa_Id
New-Datastore -VMHost ESXi-01.mycloud.lab -Name $dataname.Datastore_Name -Path $dataname.Naa_Id -Vmfs -FileSystemVersion 6
Get-Cluster -name “Cloud-Clu-01” | Get-VMhost | Get-VMHostStorage –RescanAllHBA
Start-Sleep -Seconds 15
}

Disconnect-VIServer -Confirm:$false

Input File Sample:

Sample of File_with_datastore_name_NAA_Ids.csv

Just replace the vCenter_Server_IP_Address/FQDN, Path of File Containing Datastore Names, NAA Ids, ESXi Host, Cluster Name and Sleep Interval after each Datastore creation operation with the details of your environment. BOOM!!

Happy Scripting!!

Script to Export VI Permissions

Featured

There was a requirement in one of the projects to export permissions assigned to each Virtual Machine hosted in a vSphere Environment. We wrote a PowerCLI script to fetch the list of users along with the Role assigned to each user and export these details to a CSV file.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_IP/FQDN -Credential $cred
$vms = Get-VM
$output = New-Object System.Collections.ArrayList($null)
$vmpermission = @()
foreach ($vm in $vms)
{
    $permissions = $vm | Get-VIPermission | Get-Unique
    Foreach ($permission in $permissions)
    {
        $formatpermission = $permission.Principal + " (" + $permission.Role + ")"
        $permission | Add-Member -NotePropertyName FormatPermission -NotePropertyValue $formatpermission
    }
    $vmpermission = [pscustomobject]@{VMName=$vm.Name;Role=$permissions.FormatPermission  -join ', '}
    [void]($output.Add($vmpermission))
}

$output | Export-csv C:\Users\VMPermissionsExport.csv -NoTypeInformation

Just replace the vCenter_Server_IP_Address/FQDN and Path of CSV File and run the script to export VI Permissions assigned to each Virtual Machine in vSphere environment. BOOM!!

Happy Scripting!!

Export LUN IDs with Path Selection Policy

Featured

In this post we are sharing a PowerCLI script that we used to export a list of LUNs attached to ESXi hosts in a cluster along with the details of Path Selection Policy selected for the LUN and CommandsToSwitchPath parameter set for the LUNs.

Script was tested on below versions:

  • PowerCLI version 11.5.0 build 14912921
  • vSphere 6.7U3

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_IP/FQDN -Credential $cred

$getCluster = Get-Cluster -Name "Cluster_Name"
$getESXis = $getCluster | Get-VMHost
$printluns=@()

foreach ($getESXi in $getESXis)
{
    $getLUNsforESXi = Get-VMhost $getESXi | Get-ScsiLun -LunType disk | Select VMHost, CanonicalName, MultipathPolicy, CommandsToSwitchPath
    $printluns = $printluns + $getLUNsforESXi
}

$printluns | Export-Csv C:\users\Administrator\Desktop\PathtoSaveFile.csv -NoTypeInformation 

Disconnect-VIServer -Confirm:$false

Just replace the vCenter_Server_IP_Address/FQDN, Cluster_Name and Path of CSV File and run the script to generate a report of LUNs mapped to all ESXi hosts in a vSphere cluster in your environment. BOOM!!

Happy Scripting!!

Assign Custom Attributes to VMs

Featured

There was a requirement in one of the projects to assign Custom Attributes to multiple Virtual Machines hosted in a vSphere Environment. We wrote a PowerCLI script to Assign the Custom Attributes to Virtual Machines using a CSV file which had all the details of the Custom Attributes.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_Server_IP_Address/FQDN -Credential $cred
$files = Import-CSV "C:\Users\testuser\Desktop\CustomAttributes.csv"
foreach ($file in $files)
{
$vm = Get-VM -Name $file.VMName
$vm | Set-Annotation -CustomAttribute "AppName" -Value $file.AppName
$vm | Set-Annotation -CustomAttribute "Owner" -Value $file.Owner
$vm | Set-Annotation -CustomAttribute "AppDL" -Value $file.AppDL
$vm | Set-Annotation -CustomAttribute "Env" -Value $file.ENv
}

Disconnect-VIServer -Confirm:$false

Input CSV File:

CustomAttributes.csv

Just replace the vCenter_Server_IP_Address/FQDN and Path of CSV File and run the script to assign Custom Attributes to Virtual Machines in vSphere environment. BOOM!!

Happy Scripting!!

Script to Export VI Permissions

Featured

There was a requirement in one of the projects to export permissions assigned to each Virtual Machine hosted in a vSphere Environment. We wrote a PowerCLI script to fetch the list of users along with the Role assigned to each user and export these details to a CSV file.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_IP/FQDN -Credential $cred
$vms = Get-VM
$output = New-Object System.Collections.ArrayList($null)
$vmpermission = @()
foreach ($vm in $vms)
{
    $permissions = $vm | Get-VIPermission | Get-Unique
    Foreach ($permission in $permissions)
    {
        $formatpermission = $permission.Principal + " (" + $permission.Role + ")"
        $permission | Add-Member -NotePropertyName FormatPermission -NotePropertyValue $formatpermission
    }
    $vmpermission = [pscustomobject]@{VMName=$vm.Name;Role=$permissions.FormatPermission  -join ', '}
    [void]($output.Add($vmpermission))
}

$output | Export-csv C:\Users\VMPermissionsExport.csv -NoTypeInformation

Just replace the vCenter_Server_IP_Address/FQDN and Path of CSV File and run the script to export VI Permissions assigned to each Virtual Machine in vSphere environment. BOOM!!

Happy Scripting!!

Script to Create Multiple Datastores

Featured

There was a requirement in one of the projects to create multiple (approx. 60) datastores using a PowerCLI script. We wrote a PowerCLI script to perform this operation on multiple LUNs presented to all ESXi hosts in the cluster.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer “vCenter_Server_IP_Address/FQDN” -Credential (Get-Credential)

$datanames = Import-Csv ‘C:\Users\Admin\Desktop\File_with_datastore_name_NAA_Ids.csv

foreach ($dataname in $datanames)
{
$dataname.Datastore_Name
$dataname.Naa_Id
New-Datastore -VMHost ESXi-01.mycloud.lab -Name $dataname.Datastore_Name -Path $dataname.Naa_Id -Vmfs -FileSystemVersion 6
Get-Cluster -name “Cloud-Clu-01” | Get-VMhost | Get-VMHostStorage –RescanAllHBA
Start-Sleep -Seconds 15
}

Disconnect-VIServer -Confirm:$false

Input File Sample:

Sample of File_with_datastore_name_NAA_Ids.csv

Just replace the vCenter_Server_IP_Address/FQDN, Path of File Containing Datastore Names, NAA Ids, ESXi Host, Cluster Name and Sleep Interval after each Datastore creation operation with the details of your environment. BOOM!!

Happy Scripting!!

Bulk Shutdown of Virtual Machines

Featured

There was a requirement in one of the projects to perform an unattended shutdown of multiple Virtual Machines during a planned Maintenance Window. We wrote a PowerCLI script to perform shutdown operation on multiple VMs with a delay of 20 seconds between each shutdown.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later
  • VMware Tools on the Virtual Machines

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer “vCenter_Server_IP_Address/FQDN” -Credential (Get-Credential)
$VMs = Get-Content C:\Scripts\VMShutdown\VMList.txt
foreach ($VM in $VMs)
{
Write-Host “Initiating Guest Shutdown for VM ” + $VM -ForegroundColor DarkRed
Shutdown-VMGuest -VM $VM -Confirm:$false
Start-Sleep -Seconds “20
}

foreach ($VM in $VMs)
{
Get-VM $VM | Select Name, PowerState | Ft -AutoSize -Wrap
}
Disconnect-VIServer -Confirm:$false

Note: Virtual Machines are shutdown gracefully and requires VMware Tools to installed on machine.

Just replace the vCenter_Server_IP_Address/FQDN, Path of File Containing VMs Name and Sleep Interval after each shutdown operation with the details of your environment. BOOM!!

Happy Scripting!!