Given a sample script, modify the script to perform a given action

LogIcon

Basic Script 1

This script will connect to the vCenter Server and get all VMs in a folder followed by starting those VMs in a folder

$vms = get-vm -Location Test-Folder
# Start each vm in the folder
ForEach($vm in $vms)
{
start-vm -RunAsync -VM $vm -Confirm:$false
}

Basic Script 2

This script will go and find any VMs which have their CD Drive connected

Get-VM | Where-Object {$_ | Get-CDDrive |  Where-Object { $_.ConnectionState.Connected -eq “true”  } } |  Select-Object Name

Advanced Reporting Script

This script should only show RDMs but for both compatibility modes

http://communities.vmware.com/message/1063909

$RDMs = @()
foreach($vm in (Get-View -ViewType “VirtualMachine”)) {
    foreach($dev in ($vm.Config.Hardware.Device | where {($_.gettype()).Name -eq “VirtualDisk”})) {
        if(($dev.Backing.CompatibilityMode -eq “physicalMode”) -or ($dev.Backing.CompatibilityMode -eq “virtualMode”)) {
            $objRdm = “” | select VMName, VMDK, UUID, DiskLabel, SCSIBus, SCSIDevice, Mode
            $objRdm.VMName = $vm.Name
            $objRdm.VMDK = $dev.Backing.FileName
            $objRdm.UUID = $dev.Backing.LunUuid
            $objRdm.DiskLabel = $dev.DeviceInfo.Label
            $objRdm.SCSIBus = ( $vm.Config.Hardware.Device | ? { $_.Key -eq $dev.ControllerKey }).BusNumber
            $objRdm.SCSIDevice = $dev.UnitNumber
            $objRdm.Mode = $dev.Backing.CompatibilityMode
            $RDMs += $objRdm
        }
    }
}

$report = @()

foreach ($cluster in (Get-View -ViewType “ClusterComputeResource”)) {
    $vmhostsview = $cluster.host | % { Get-View $_ }
    $vmhostview = $vmhostsview | Select -first 1
    $ScsiLuns = $vmhostsview | % { $_.Config.StorageDevice.ScsiLun } | Select -unique *
    $UUIDs = $ScsiLuns | Select -unique UUID
    $Datastores = $vmhostsview | % { $_.Config.FileSystemVolume.MountInfo } | % { $_.Volume } | Select -Unique *
    $HostLUN = $vmhostsview | % { $_.Config.StorageDevice.ScsiTopology.Adapter } | % { $_.Target | % { $_.LUN } } | Select -unique *
    foreach ($UUID in $UUIDs) {
        $Lun = $ScsiLuns | ? { $_.UUID -eq $UUID.UUID } | Select -first 1
        $objVolume = “” | Select Cluster, VolumeName, CanonicalName, DisplayName, VolumeType, CapacityGB, BlockSizeMb, VMFSVersion, LunType, Vendor, Model, HostLUN, VM, VMDiskLabel, VMSCSIBus, VMSCSIDevice, Revision, ScsiLevel, UUID
        $objVolume.Cluster = $cluster.Name
        $objVolume.CanonicalName = $Lun.CanonicalName
        $objVolume.HostLUN = ($HostLUN | ? { $_.ScsiLun -eq $Lun.Key } | select -unique LUN).LUN
        $objVolume.UUID = $Lun.Uuid
        $objVolume.CapacityGB = $Lun.Capacity.Block * $Lun.Capacity.BlockSize / 1GB
        $objVolume.DisplayName = $Lun.DisplayName
        $objVolume.LunType = $Lun.LunType
        $objVolume.Vendor = $Lun.Vendor
        $objVolume.Model = $Lun.Model
        $objVolume.Revision = $Lun.Revision
        $objVolume.ScsiLevel = $Lun.ScsiLevel
        foreach ($vol in $Datastores) {
            if ($vol.extent | % { $_.diskname -eq $Lun.CanonicalName}) {
                $objVolume.VolumeName = $vol.Name
                $objVolume.BlockSizeMb = $vol.BlockSizeMb
                $objVolume.VMFSVersion = $vol.Version
                $objVolume.VolumeType = “vmfs”
            }
        }
        foreach ($rdm in $RDMs) {
            if ($Lun.Uuid -eq $rdm.UUID) {
                $objVolume.VolumeName = $rdm.VMDK
                $objVolume.VM = $rdm.VMName
                $objVolume.VMDiskLabel = $rdm.DiskLabel
                $objVolume.VMSCSIBus = $rdm.SCSIBus
                $objVolume.VMSCSIDevice = $rdm.SCSIDevice
                if ($rdm.Mode -eq “virtualMode” ) { $objVolume.VolumeType = “rdm” }
                if ($rdm.Mode -eq “physicalMode”) { $objVolume.VolumeType = “rdmp” }
            }
        }
        $report += $objVolume
    }
}
$report | Export-Csv “C:\report.csv” -NoTypeInformation -UseCulture

Running PowerShell Scripts

  • Launch PowerShell
  • Make sure Set-ExecutionPolicy RemoteSigned is the policy
  • Run the script by entering the full path to the script (c:\scripts\myscript.ps1), or if it’s in the current directory, prefix it with a period followed by a backslash (.\myscript.ps1).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.