Pages: [1]
Print
Author Topic: Help with script - List all VMs  (Read 1764 times)
KristianW
Newbie
*
Posts: 32


View Profile
« on: January 27, 2010, 09:45:33 AM »

Hi!!

Need some help with a script I am trying to put together.

I can't seem to get the script to work like I want it to.
This is a small part of the script so far....

Code:

$domain = read-host "What domain? (A1, A2, B1, B2, C1 or C2)"
$role = read-host "What role? (AP, DC, EX or DB)"

get-vm -name $domain $Role *

So, if the domain input is A1, and the role is AP - I want the script to list all server named "A1APxxxxxx"

Just like:

get-vm -Name A1AP*

Then, to put a small twist in there... I would like it to filter out all servers with "W" after the $role parameter.

Like this:
get-vm -Name A1APW*


I guess I could put the "W" in as a variable - which could extend my script for linux/unix-servers too...
$os = read-host "What OS? (W for Windows, L for Linux, U for Unix)

But since I am not a linux admin, I don't care.

Can anyone please give me a pointer?
Logged
esarakaitis
Administrator
Sr. Member
*****
Posts: 256


8223109 sack57@hotmail.com littleking57 rootinfortwayne
View Profile WWW Email
« Reply #1 on: January 27, 2010, 10:22:19 AM »

i would use where...

something like this:

Code:
$vmview = Get-View -Server "$global:defaultviservers" -ViewType VirtualMachine
$domain = read-host "What domain? (32, orch)"
$role = read-host "What role? (XP, DC, EX or DB)"
$vmview | where {$_.name -imatch $domain -and $role}
Logged
ewannema
Administrator
Newbie
*****
Posts: 33


View Profile WWW
« Reply #2 on: January 28, 2010, 11:27:59 AM »

Just in case you are not ready to use the views and base level stuff, here is a simpler approach to do what you want.  In a little bit I will post a slight revision to what esarakaitis posted that should do what you want in one line and would be faster than this example.

Code:
$domain = "A1"
$role = "AP"
$exclude = "W"
$excludeMatch = "{0}{1}W" -f $domain, $role

Get-VM "$domain$role*" | Where-Object {-not ($_.Name -match $excludeMatch)}
Logged
KristianW
Newbie
*
Posts: 32


View Profile
« Reply #3 on: February 01, 2010, 08:12:42 AM »

Thanks for your feedback.

I ended up doing this:

<code>
$domain = read-host "What domain (a1, a2, b1, b1)"
$role = read-host "What role (AP, EX.....)"
$servers = $domain + $role + 'W*'

get-vm -name $servers
</code>

Worked like a charm... The only problem I had with my script, was that it sometimes froze. God knows why.

I tried it on some "AP" servers - worked great.
I then tried it on some other servers - froze up and I had to kill the process.
Started it again on the same servers, and it worked fine....

I guess that the number of servers in the query had some influence.... or the load on the vCenter server.


Ah well - the job is done and I am satisfied.
Logged
Pages: [1]
Print
Jump to: