<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Script to match a VM that shows 2 datastores in VMware PowerCLI Discussions</title>
    <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Script-to-match-a-VM-that-shows-2-datastores/m-p/1660252#M51440</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Take a look at my solution in this thread &lt;SPAN __jive_macro_name="thread" id="267191"&gt;&lt;/SPAN&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards, Robert&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 07 May 2010 16:26:36 GMT</pubDate>
    <dc:creator>RvdNieuwendijk</dc:creator>
    <dc:date>2010-05-07T16:26:36Z</dc:date>
    <item>
      <title>Script to match a VM that shows 2 datastores</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Script-to-match-a-VM-that-shows-2-datastores/m-p/1660251#M51439</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;EM&gt;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.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;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.&lt;/EM&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm trying to write a script that would check a cluster for VMs with 2 datastores listed matching XXX1 and XXX2 &lt;/P&gt;&lt;P&gt;here is what i have: &lt;/P&gt;&lt;P&gt;$vms = Get-Cluster "drscluster01" |Get-VM |Get-Datastore |where {$_.Name -match "XXX1" }|where {$_.Name -match "XXX2"}&lt;/P&gt;&lt;P&gt;Foreach ($Vm in $Vms) {&lt;/P&gt;&lt;P&gt;Write-Host "test"&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help would be appreciated &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.vmware.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 May 2010 16:08:15 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Script-to-match-a-VM-that-shows-2-datastores/m-p/1660251#M51439</guid>
      <dc:creator>jmajor1111</dc:creator>
      <dc:date>2010-05-07T16:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: Script to match a VM that shows 2 datastores</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Script-to-match-a-VM-that-shows-2-datastores/m-p/1660252#M51440</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Take a look at my solution in this thread &lt;SPAN __jive_macro_name="thread" id="267191"&gt;&lt;/SPAN&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards, Robert&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 May 2010 16:26:36 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Script-to-match-a-VM-that-shows-2-datastores/m-p/1660252#M51440</guid>
      <dc:creator>RvdNieuwendijk</dc:creator>
      <dc:date>2010-05-07T16:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: Script to match a VM that shows 2 datastores</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Script-to-match-a-VM-that-shows-2-datastores/m-p/1660253#M51441</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks, i did look at something like that earlier but it didn't do what i wanted. I wrote this. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Probably a one time deal, but its nice event though it could use polish.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __jive_macro_name="code"&gt;
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
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 May 2010 19:47:38 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Script-to-match-a-VM-that-shows-2-datastores/m-p/1660253#M51441</guid>
      <dc:creator>jmajor1111</dc:creator>
      <dc:date>2010-05-07T19:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: Script to match a VM that shows 2 datastores</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Script-to-match-a-VM-that-shows-2-datastores/m-p/1660254#M51442</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I did some polishing of your script and came to this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __jive_macro_name="code"&gt;
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
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I didn't test the script. But I would like to hear from you if it works oke.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards, Robert&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 May 2010 20:41:17 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Script-to-match-a-VM-that-shows-2-datastores/m-p/1660254#M51442</guid>
      <dc:creator>RvdNieuwendijk</dc:creator>
      <dc:date>2010-05-07T20:41:17Z</dc:date>
    </item>
  </channel>
</rss>

