VMware Cloud Community
DZ1
Hot Shot
Hot Shot
Jump to solution

Trying to save multiple variables

I'm working on a deployment script, my goal right now is to save the name of the VM and a custom vCenter attribute called "assignee" into a variable, I would like the variable name to be the actual name of the VM to make it easier.  The "assignee" attribute is just what it sounds like, the name of the person that the VM is assigned to.  When I deploy the new VMs they will have the same VM name, and the assignee will be the same as well, so bascially, it's a pull down of the current info, and then a put back of that same info

So I would like something like this:

$VMname = $VMname, "John D"

$VMname1 = $VMname1, "Jane D"

and so on.

I have a small amount of the code that I'm trying to use to get this working, nothing in-depth really:

get-folder test | Get-VM | foreach {
if (($_ | Get-Annotation -customAttribute "assignee").value -gt 0)
{ $temp, $temp1 = @($_.name, ($_ | Get-Annotation -CustomAttribute "Assignee").value) }
}

Any help is appreciated...thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There was indeed a missing curly brace (copy/paste is not my friend Smiley Wink).

I corrected it.

It won't take you years, just regular practice.

And if you're stuck, raise a thread.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this

Get-Folder test | Get-VM | foreach {
  $assignee = Get-Annotation -Entity $_ -CustomAttribute "assignee" -ErrorAction SilentlyContinue
  if ($assignee.value -gt 0){     New-Variable -Name $_.Name -Value (New-Object PSObject -Property @{       VM = $_.Name
      Assignee = $assignee.Value
    }
  }
}

The New-Variable cmdlet allows you create a new variable.

And in that variable we store an object with 2 properties, the VM name and the assignee


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can use the PowerShell New-Variable cmdlet.

get-folder test | Get-VM | foreach {
  if (($_ | Get-Annotation -customAttribute "assignee").value -gt 0) 
  {
    New-variable -Name $_.Name -Value @($_.name, ($_ | Get-Annotation -CustomAttribute "Assignee").value)
  }
}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
DZ1
Hot Shot
Hot Shot
Jump to solution

Thank you to both of you, It's very nice that I see two different ways of doing it.  I get so use to doing the same things that when I need to break out of my box, it's hard to do.

Luc, I did get an error when I ran your script, but it's the close parentheses for the section starting the "New-Object" that was missing.  After I put that there, it ran fine.  I really apprciate the help, and the response was fast.  Hopefully (in many years) I can help out some people with their powershell scripts. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was indeed a missing curly brace (copy/paste is not my friend Smiley Wink).

I corrected it.

It won't take you years, just regular practice.

And if you're stuck, raise a thread.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos