Automation

 View Only
  • 1.  Script to match a VM that shows 2 datastores

    Posted May 07, 2010 04:08 PM

    been working on this for a few... err... long time. searched all over an i just can't find what i need, not complete anyway.

    I have several hundred VMs that we did a svmotion from one datastore to another. about 20% of them did what is called linked-cloning and left the old datastore listed. Its not critical we fix this but we need to for future reasons and we know the easiest fix is to just create a snapshot which for whatever reason kicks the VM into realizing the old datastore isn't active.

    I'm trying to write a script that would check a cluster for VMs with 2 datastores listed matching XXX1 and XXX2

    here is what i have:

    $vms = Get-Cluster "drscluster01" |Get-VM |Get-Datastore |where {$_.Name -match "XXX1" }|where {$_.Name -match "XXX2"}

    Foreach ($Vm in $Vms) {

    Write-Host "test"

    }

    obviously write-host "test" just tells me its found, i plan on putting in create-snapshot for it once i know it works. More importantly is I can't get them to match 1 name let alone 2.

    Any help would be appreciated :smileyhappy:



  • 2.  RE: Script to match a VM that shows 2 datastores

    Posted May 07, 2010 04:27 PM

    Take a look at my solution in this thread .

    Regards, Robert



  • 3.  RE: Script to match a VM that shows 2 datastores

    Posted May 07, 2010 07:48 PM

    Thanks, i did look at something like that earlier but it didn't do what i wanted. I wrote this.

    This script is specifically designed to go through a Cluster, check for 2 datastores with matching "name1" and "name2", creates a snapshot and then deletes it. Its purposed was for svmotion linked-clone issue where a svmotion attempt would be successful but still leave trace of the old datastore. This messed up our reports and was the reason for this script.

    Probably a one time deal, but its nice event though it could use polish.

    Connect-VIServer -Server $server
    $dsnum = 2
    $vservers = get-cluster "cluster1" |Get-VM ; foreach ($vserver in $vservers){ 
    $var2 = (get-vm $vserver | get-datastore | Measure-Object).Count
    $var = get-vm $vserver | get-datastore
    foreach ($_ in $var)
    {
       if (($_.Name -match "xxx1") -and ($_.Name -match "xxx2") -and ($var2 -ge $dsnum ))
       {
       		Write-host "Listed DataStores $var" -BackgroundColor DarkMagenta -ForegroundColor White
    		Write-Host "Match $vserver" -fore Blue 
    		Get-VM $vserver | New-Snapshot -Name "Snap01" 
    		Get-VM $vserver | get-snapshot | Remove-Snapshot -confirm:$false
    		$var3 = get-vm $vserver | get-datastore 
    		write-host "Updated $vserver Listed DataStores $var3" -ForegroundColor Cyan
    	   }
    	elseif ($var2 -lt $dsnum ){
    		write-host "Server Not Matched $vserver" -BackgroundColor Black -ForegroundColor White
    	}
    }
    }
    Disconnect-VIServer -confirm:$false
    



  • 4.  RE: Script to match a VM that shows 2 datastores

    Posted May 07, 2010 08:41 PM

    I did some polishing of your script and came to this:

    Connect-VIServer -Server $server
    $dsnum = 2
    Get-Cluster "cluster1" | Get-VM | ForEach-Object {
    	$vm = $_
    	$Datastores = $vm | Get-Datastore
    	$Count = ($Datastores | Measure-Object).Count
    	if ($Count -ge $dsnum )
    	{
    		Write-host "Listed DataStores $Datastores" -BackgroundColor DarkMagenta -ForegroundColor White
    		Write-Host "Match $($vm.Name)" -fore Blue 
    		$vm | New-Snapshot -Name "Snap01" 
    		$vm | Get-Snapshot | Remove-Snapshot -confirm:$false
    		$NewDatastores = $vm | Get-Datastore 
    		write-host "Updated $($vm.Name) Listed DataStores $NewDatastores" -ForegroundColor Cyan
    	}
    	else {
    		write-host "Server Not Matched $($vm.Name)" -BackgroundColor Black -ForegroundColor White
    	}
    }
    Disconnect-VIServer -confirm:$false
    

    I didn't test the script. But I would like to hear from you if it works oke.

    Regards, Robert