VMware Cloud Community
Roman_Romano
Enthusiast
Enthusiast

Object No Longer Exists....

I'm wanting to find out which of our VMs have out-of-date tools. I wrote the following:

-


Get-VIServer vmserver

$vms = get-vm | where {$_.powerstate -eq "PoweredOn"} | %{Get-View $_.ID}

foreach ($i in $VMS)

{$ed = $i.Config.Tools.ToolsVersion

write-host $i.name "Tools Version " $ed}

-


This works fine when i connect to a standalone ESX. However, when i try to enumerate the VMs via connection to our VirtualCenter server or specify an ESX node in a cluster, i get the following:

"Script Error. The request refers to an object that no longer exists or has never existed. At line 2, position 293 $vms = get-vm | where.............."

I'm NOT a scripting guy so it could be very simple Smiley Happy

Any advice??

Regards

Reply
0 Kudos
5 Replies
halr9000
Commander
Commander

First thing that comes to mind is to try this in a new powershell session. If that works, then see if it has to do with not initialiazing the $vms variable. You can try setting it to $null.

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Roman_Romano
Enthusiast
Enthusiast

Thanks for that pointer.

I'd been doing all the scripting and testing using PowerGUI Script Editor where i was getting the error. On your advice i ran the script from a powershell prompt and it completed successfully. Must be something to do with the Script Editor.

Thanks for your help

Reply
0 Kudos
halr9000
Commander
Commander

Rather than a bug, I think its more of a consequence of how it is creating the objects. I don't use powerGUI too often, but I know you can customize the scripts. It may be as easy as initializing the variables at the top of each script, or adding a scope modifier. e.g.

$vms = $null

or

$script:vms = get-viserver ...

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
Poshoholic
Contributor
Contributor

If I understand this problem correctly, I just blogged about this here:

Just to make sure I'm on the same page, I'll restate the issue. The issue you're describing is that you have a script that does not run in PowerGUI without errors, yet it runs in the VMware VI Toolkit (for Windows) PowerShell console just fine. Is that correct? Because if that's the issue, it's not surprising at all.

Almost every PowerShell snapin that extends PowerShell (I have 13 of them, and counting) comes with their own PowerShell console. These consoles are customized for that particular snapin to make it easy for you to get your work done. I don't like the way these customizations are done at the moment because they result in users learning about variables and functions and aliases that only work in one particular console, but the consoles don't tell the user that those items only work in that console. The result? Scripts that are only portable between like consoles.

Anyhow, please read my blog post I reference above and let me know if this is indeed the issue. If it is, you'll have to look at the properties for the shortcut to the console, see what scripts it is running, and then look at those scripts to find the customization pieces you will need for your script that you want to run in PowerGUI.

--

Kirk Munro

Poshoholic

Kirk Munro [MVP] Poshoholic http://poshoholic.com
Reply
0 Kudos
Roman_Romano
Enthusiast
Enthusiast

Thanks for that, it was an interesting read. Although i don't think it was exactly the problem i was having (halr9000 suggestion of the way the object is created seems to be the issue) i do agree with you that as a complete scripting novice, have varous different powershell consoles is confussing. Like you said maybe a nice disclosure somewhere might stop unwanted pulling out of hair!

regards

Reply
0 Kudos