PowerCLI and View

See also: Part 2 in the series

So I’ve had need to code some checks for VMware View lately – which means among other things I need to dust off my seriously rusty scripting skills and learn what resources are out there for VMware View.

Resource 1: VMware PowerCLI for View chapter of the admin guide

This is where I found how to access the cmdlets : load the VMware.View.Broker and VMware.VimAutomation.Core snapins from a Powershell environment on the Connection server.  Yeah, on the connection server.  Seems you can’t run them other than on the connection server you are referencing.  “Does not play well with others.”

Resource 2: The View PowerCLI cmdlet Parameters chapter of the admin guide

Important one as it lists and details a coupld of the more important settings.  I was part way through making my own table of “PoolType” values before I found this.

Since there isn’t a complete cmdlet reference like there is for the full PowerCLI you’re kind of on your own for the rest of it!

One of my first issues was I was not intending for the main script to run on a Connection server, and I had multiple connection servers anyway.

Enter “Invoke-Command”

$s = Invoke-Command -ComputerName ConnectionServerPrimary `
         -command {c:scriptscript1.ps1}
if ($s | where {$_.PoolType -eq "SviNonPersistent"}) {
   Write-Host "floating (nonpersistent) desktops"} 
   else {Write-Host "Doesn't float, must not be a witch"}

That leaves me with managing multiple script files, but I think I will generate the remote script files on demand by writing them out of the primary file and deleting them at the end.  We’ll see what seems easier.

The next issue was figuring out if the pool I was interested in was enabled for local mode.  Only there is not a PowerCLI cmdlet for that.  Fortunately it’s in LDAP.  Well, it’s fortunate if you know anything about LDAP.  I don’t.  So I opted to dump the LDAP to a local file with

vdmexport -f c:scriptviewldap.ldf -v

Note that this leaves the View LDAP database in text form.  My script deletes the file when its done.  Note also this is sort of a back up so email it to yourself or print it off since your kids probably need scratch paper.

Now comes the entertaining part – parsing the text file to figure out if the pool in question has local mode enabled.  Looking at the LDAP file (ok, I exported the file twice, once with local mode on and once with it off then compared them with http://winmerge.org/ ) I found that a variable was set

pae-mVDIOfflineAllowed: 1

when the pool had it on.  That variable was in a section with a DN referenced, and that DN was later found in a reference with the pool name I was looking for.  Ok, in a perfect world I would get the pool name and then see if it had local mode enabled, but in a perfect world there would already be a cmdlet for that.  And a Menudo reunion.

$found = 0
$s = Invoke-Command -ComputerName ConnectionServer -command {
    c:scriptscript.ps1}
foreach ($block in select-string `
   "\connectionserverc$scriptview.ldf" '
   -pattern "pae-mVDIOfflineAllowed: 1") {

 foreach ($match in select-string `
      "\connectionserverc$scriptview.ldf" '
       -pattern "CN=.*," | where {$_.linenumber'
       -ge $block.linenumber-12 -AND $_.linenumber'
       -le $block.linenumber } ){
[string]$partial = $match.matches 
 foreach ($sublock in select-string'
     "\connectionserverc$scriptview.ldf"'
       -pattern $partial.substring(3,36)) { 
      foreach ($submatch in select-string ' 
     "\connectionserverc$scriptview.ldf" -pattern'
      "PoolWithLocal" | where {$_.linenumber -le '
       $sublock.linenumber+5 -AND '
       $_.linenumber -ge $sublock.linenumber } ){
       $found=1 
} } } } 
If ($found) {Write-Host "Local mode enabled"} 
else {Write-Host "Dude, where's my desktop?"}

This works.  But its not pretty.  I should be able to parse the text file once, and then (using the -context parameter for select-string) just search the 10 lines before it for a DN – but I could not get that to work.  I should also not need to use [string]$partial (I think) but I couldn’t get it to work otherwise.  I’m a little surprised this works in its entirety 😉

This entry was posted in Computing, PowerShell, Scripting, Virtualization, VMware and tagged , , . Bookmark the permalink.

Leave a Reply