Microsoft Virtualization Discussions
Microsoft Virtualization Discussions
I am working on automating our test environment refresh, which spins up a flexclone from a SMVI backup, then mounts the NFS volume into VMware vCenter. Once mounted I will use PowerCLI to register the .vmx files and modify the VM's. The NFS volumes are spread over two controllers, thus requiring the -Controller property to be called in New-NaVolClone and Add-NaNfsExport, which is forcing me to wrap the entire script in my Foreach controller loop.
Here's my the first part of my script that I'm having problems with:
$Snapshot = @()
$FlexClone = @()
#Create Object that connects to both FAS controllers
[object[]]$CDCFAS=$null
Foreach ($FAS in @("CDCFAS01", "CDCFAS02")) {
$NewFAS=Connect-nacontroller $FAS
$CDCFAS=$CDCFAS+$NewFAS
}
#Gets weekley snapshot names of vmds_nfs volumes and creates flexclones from those volumes
Foreach ($ProdFAS in $CDCFAS) {
$NFSVol = Get-NaVol -Controller $ProdFAS | where { $_.Name -like 'vmds_nfs_*'}
$Snapshot += Foreach ($Vol in $NFSVol) {
Get-NaSnapshot -TargetName $Vol.Name -Controller $ProdFAS | where { $_.Name -notlike 'ATLFAS*' } | Sort-Object -Descending -Property Created | select -First 1
}
Foreach ($Snap in $Snapshot) {
$NewClone = $ProdFAS.Name+'_'+($Snap.Created.ToShortDateString()).Replace("/","_")+'_'+$Snap.TargetName
New-NaVolClone -ParentVolume $Snap.TargetName -ParentSnapshot $Snap.Name -CloneVolume $NewClone -Controller $ProdFAS
Add-NaNfsExport -Persistent -Path /$NewClone -ActualPath /vol/$NewClone -ReadOnly $esxtststorage -Controller $ProdFAS
$NFSObject = New-Object System.Object
$NFSObject | Add-Member -MemberType NoteProperty -Name DatastoreName -Value $NewClone
$NFSObject | Add-Member -MemberType NoteProperty -Name Volume -Value $NewClone
$NFSObject | Add-Member -MemberType NoteProperty -Name NFSPath -Value /$NewClone
$NFSObject | Add-Member -MemberType NoteProperty -Name NFSHost -Value ($ProdFAS.ToString()+"d")
$NFSObject | Add-Member -MemberType NoteProperty -Name Controller -Value $ProdFAS
$FlexClone += $NFSObject
}
}
The problem is that while my $Snapshot variable is populating with the 4 correct current snapshots, my Foreach-Object ($Snap ...) loop is populating my $Flexclone array with 6 objects. I've listed the results of $Snapshot and $Flexclone below and highlighted the additional 2 objects that should not be there (Dark blue). *Note: If the volume name is like nfs_01, then the controller should be CDCFAS01. Nfs02 = CDCFAS02.
PS C:\> $Snapshot | select Name,Created
Name Created
---- -------
2012-07-11_0200-0400_weekly_VMware_nfs_01_1_CDCFAS01_vmds_nfs_01_1_ 7/11/2012 6:08:23 AM
2012-07-12_0400-0400_weekly_VMware_nfs_01_3_CDCFAS01_vmds_nfs_01_3_ 7/12/2012 8:05:52 AM
2012-07-12_0200-0400_weekly_VMware_nfs_02_1_CDCFAS02_vmds_nfs_02_1_ 7/12/2012 6:05:49 AM
2012-07-11_0400-0400_weekly_VMware_nfs_02_2_CDCFAS02_vmds_nfs_02_2_ 7/11/2012 8:04:02 AM
PS C:\> $FlexClone
DatastoreName : CDCFAS01_7_11_2012_vmds_nfs_01_1
Volume : CDCFAS01_7_11_2012_vmds_nfs_01_1
NFSPath : /CDCFAS01_7_11_2012_vmds_nfs_01_1
NFSHost : CDCFAS01d
Controller : CDCFAS01
DatastoreName : CDCFAS01_7_12_2012_vmds_nfs_01_3
Volume : CDCFAS01_7_12_2012_vmds_nfs_01_3
NFSPath : /CDCFAS01_7_12_2012_vmds_nfs_01_3
NFSHost : CDCFAS01d
Controller : CDCFAS01
DatastoreName : CDCFAS02_7_11_2012_vmds_nfs_01_1
Volume : CDCFAS02_7_11_2012_vmds_nfs_01_1
NFSPath : /CDCFAS02_7_11_2012_vmds_nfs_01_1
NFSHost : CDCFAS02d
Controller : CDCFAS02
DatastoreName : CDCFAS02_7_12_2012_vmds_nfs_01_3
Volume : CDCFAS02_7_12_2012_vmds_nfs_01_3
NFSPath : /CDCFAS02_7_12_2012_vmds_nfs_01_3
NFSHost : CDCFAS02d
Controller : CDCFAS02
DatastoreName : CDCFAS02_7_12_2012_vmds_nfs_02_1
Volume : CDCFAS02_7_12_2012_vmds_nfs_02_1
NFSPath : /CDCFAS02_7_12_2012_vmds_nfs_02_1
NFSHost : CDCFAS02d
Controller : CDCFAS02
DatastoreName : CDCFAS02_7_11_2012_vmds_nfs_02_2
Volume : CDCFAS02_7_11_2012_vmds_nfs_02_2
NFSPath : /CDCFAS02_7_11_2012_vmds_nfs_02_2
NFSHost : CDCFAS02d
Controller : CDCFAS02
Thanks for the help!
Solved! See The Solution
My problem was a code problem. I wasn't clearing my $Snapshot array variable in each loop, I was adding each result to the array.
Incorrect Code --> $Snapshot += Foreach
Foreach ($ProdFAS in $CDCFAS) {
$NFSVol = Get-NaVol -Controller $ProdFAS | where { $_.Name -like 'vmds_nfs_*'}
$Snapshot += Foreach ($Vol in $NFSVol) {
Get-NaSnapshot -TargetName $Vol.Name -Controller $ProdFAS | where { $_.Name -notlike 'ATLFAS*' } | Sort-Object -Descending -Property Created | select -First 1
}
Correct Code --> $Snapshot = Foreach
Foreach ($ProdFAS in $CDCFAS) {
$NFSVol = Get-NaVol -Controller $ProdFAS | where { $_.Name -like 'vmds_nfs_*'}
$Snapshot = Foreach ($Vol in $NFSVol) {
Get-NaSnapshot -TargetName $Vol.Name -Controller $ProdFAS | where { $_.Name -notlike 'ATLFAS*' } | Sort-Object -Descending -Property Created | select -First 1
}
Thanks,
Robbie
Do you have any VMs that span filers and/or datastores?
Check your weekly job in SMVI and check the "Spanning" tab.
My problem was a code problem. I wasn't clearing my $Snapshot array variable in each loop, I was adding each result to the array.
Incorrect Code --> $Snapshot += Foreach
Foreach ($ProdFAS in $CDCFAS) {
$NFSVol = Get-NaVol -Controller $ProdFAS | where { $_.Name -like 'vmds_nfs_*'}
$Snapshot += Foreach ($Vol in $NFSVol) {
Get-NaSnapshot -TargetName $Vol.Name -Controller $ProdFAS | where { $_.Name -notlike 'ATLFAS*' } | Sort-Object -Descending -Property Created | select -First 1
}
Correct Code --> $Snapshot = Foreach
Foreach ($ProdFAS in $CDCFAS) {
$NFSVol = Get-NaVol -Controller $ProdFAS | where { $_.Name -like 'vmds_nfs_*'}
$Snapshot = Foreach ($Vol in $NFSVol) {
Get-NaSnapshot -TargetName $Vol.Name -Controller $ProdFAS | where { $_.Name -notlike 'ATLFAS*' } | Sort-Object -Descending -Property Created | select -First 1
}
Thanks,
Robbie
Nice script. Did you also write a script to reverse everything after the test is complete?