Options

Trouble with Powershell / Variables and Objects

jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
In Powershell (well, PowerCLI), I am getting a list of virtual machines and just "get" the first of a sorted list
get-vm | where-object { $_.name -like "$vm-BkpClone-*"} | Sort-Object name | Select-Object -First 1 

Now I need to call this at a later stage in an if-statement so I am trying to put this into a variable
$oldClone = get-vm | where-object { $_.name -like "$vm-BkpClone-*"} | Sort-Object name | Select-Object -First 1 

It kinda works, but not perfectly. I noticed whilst it seems to be working, it breaks other parts of the script, writing to a logfile for example.

Here the whole extract from the part in question
		If(Get-VM $cloneName) 
		{
	 		"$(Get-Date –f o) - $cloneName Created Sucessfully" | Out-File $VM_Clonelog -Append -Encoding "ASCII"
		

		
		$oldClone = get-vm | where-object { $_.name -like "$vm-BkpClone-*"} | Sort-Object name | Select-Object -First 1 
				
		if ($oldClone -match $cloneName) 
			{
			Write-Host "`nCreated clone is the only copy - nothing to delete" | Out-File $VM_Clonelog -Append -Encoding "ASCII"
			}
			
			else
			
			{
			Write-Host "`nDeleting oldest clone" | Out-File $VM_Clonelog -Append -Encoding "ASCII"
			Get-VM $oldClone | Remove-VM -DeleteFromDisk -Confirm:$False  | Out-File $VM_Clonelog -Append -Encoding "ASCII"
			}
			
		
	    $VMsCloned = $VMsCloned +1
		
 		} 

The second line "$cloneName Created Sucessfully" is being passed correctly into the logfile, and from that point on it stops.

I also notice that if I am moving
$oldClone = get-vm | where-object { $_.name -like "$vm-BkpClone-*"} | Sort-Object name | Select-Object -First 1 

Outside the "main" if-statement, the second if-statement stops working.

Hope it make sense ?
My own knowledge base made public: http://open902.com :p

Comments

  • Options
    QordQord Member Posts: 632 ■■■■□□□□□□
    Meh....these always give me trouble. Have you tried $oldClone.name?
    if ($oldClone.name -match $cloneName)
    

    What about complicating it by adding another variable in there....
    $oldClone = get-vm | where-object { $_.name -like "$vm-BkpClone-*"} | Sort-Object name | Select-Object -First 1 
    $oldClone2 = $oldClone.name
    if ($oldClone2 -match $cloneName)
    

    I usually wind up doing a lot of trial end error before something like this works for me, complex variables give me a lot of trouble.
  • Options
    meadITmeadIT Member Posts: 581 ■■■■□□□□□□
    Is the Select-Object part of the command required to pull the information you need? If I recall correctly, I had a similar issue using Select-Object. I think what happens is that the Select-Object creates a new object that doesn't have all of the member properties of the original object (in this case the object created by the get-vm command).
    CERTS: VCDX #110 / VCAP-DCA #500 (v5 & 4) / VCAP-DCD #10(v5 & 4) / VCP 5 & 4 / EMCISA / MCSE 2003 / MCTS: Vista / CCNA / CCENT / Security+ / Network+ / Project+ / CIW Database Design Specialist, Professional, Associate
  • Options
    meadITmeadIT Member Posts: 581 ■■■■□□□□□□
    Another option to compare them may be to compare the PersistentId property (UUID) between the two objects. This should be unique for each virtual machine.
    if ($oldClone.PersistentID -eq $cloneName.PersistentID)
    
    CERTS: VCDX #110 / VCAP-DCA #500 (v5 & 4) / VCAP-DCD #10(v5 & 4) / VCP 5 & 4 / EMCISA / MCSE 2003 / MCTS: Vista / CCNA / CCENT / Security+ / Network+ / Project+ / CIW Database Design Specialist, Professional, Associate
  • Options
    jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
    Thanks for the input guys, will try a few suggestions laters.

    meadIT - Let me explain.

    Our "DR-Strategy" - if you can call it that - is cloning a set of VMs to our DR site. I am using a csv file to pull the names of the VMs and the date it requires to be backed up.
    MasterVM,DestDS,DestDatacenter,DestResourcePool,DestCluster,DestFolder,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
    VM-1,iSCSI,Datacenter,Clones,Cluster,DR,1,1,1,1,1,1,1
    VM-2,iSCSI,Datacenter,Clones,Cluster,DR,1,1,1,1,1,1,1
    VM-3,iSCSI,Datacenter,Clones,Cluster,DR,1,1,1,1,1,1,1
    

    Then first, I sort the names, making sure the "oldest" is on the top of the list, for example
    [B][I]PowerCLI C:\Scripts> get-vm | where-object { $_.name -like "*BkpClone-*"}  | Sort-Object name |fl -Property name[/I][/B]
    Name : VM-1-BkpClone-20130704
    Name : VM-1-BkpClone-20130705
    Name : VM-1-BkpClone-20130706
    Name : VM-1-BkpClone-20130707
    Name : VM-1-BkpClone-20130708
    Name : VM-1-BkpClone-20130709
    

    I then use select-object to find the oldest - the first one in that list based on the reverse date
    [B][I]PowerCLI C:\Scripts> get-vm | where-object { $_.name -like "*BkpClone-*"}  | Sort-Object name | Select-Object -First 1 | fl -Property name[/I][/B]
    Name : VM-1-BkpClone-20130704
    

    Which I then try to delete. The IF statement is needed to make sure it isn't always deleting the first one (i.e. if the first one in the list is equals the current clone, then skip it), otherwise it would delete the clone as soon as it is created - if there is no previous one that is.
    My own knowledge base made public: http://open902.com :p
Sign In or Register to comment.