VI Toolkit 1.5 Getting Started and Undocumented Functions

0. Installing
Installing or upgrading the VI Toolkit 1.5 is as easy as Next, Next, Finish. And using the new VI Toolkit shortcut on your desktop allows quick and easy access to the Toolkit. But what about running these cmdlets from your scripts or integrating it with other snapins? Read on!

1. Setting up your profile
The new desktop shortcut does two things for you: it starts powershell with the VI Toolkit snapin loaded and it runs a script which modified the look of the Powershell window ánd adds some cool extra functions. If you want to have the same functionality in your normal Powershell window and your scripts, you have to copy some stuff to your Powershell profile. First, set up your profile:

a. Start a normal Powershell window.
b. Run the following command: Test-Path $profile
c. Did it return True? Then you already have a profile file. Did it return False, do step d.
d. Create a profile file by running: New-Item $profile -ItemType File

2. Adding the snap-in
a. Open your profile by running: Invoke-Item $profile
b. Add the following line to the profile file to load the snap-in: Add-PSSnapIn VMware.VimAutomation.Core -ErrorAction SilentlyContinue

3. Adding undocumented functions
a. Open the file C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\Initialize-VIToolkitEnvironment.ps1
b. Copy the following Function Blocks to your profile file: Get-VICommand, New-DatastoreDrive, New-VIInventoryDrive, Get-VIToolkitDocumentation, Get-VIToolkitCommunity

4. Undocumented Functions?
Ok, you can read about them in the Administrator’s Guide (http://www.vmware.com/support/developer/windowstoolkit/wintk10/doc/viwin_admin.pdf), but you might overlook these functions because they are not cmdlets, so Get-Command won’t show them (nor will Auto-Complete work for them).
Here’s a short description of each of them:

  • Get-VICommand: Displays all VI Toolkit cmdlets (equal to: Get-Command -PSSnapIn VMware.VimAutomation.Core);
  • New-DatastoreDrive: Allows you to browse a datastore like a drive (Example Usage: New-DatastoreDrive -Name <DriveName> -Datastore (Get-Datastore <DatastoreName>); Then jump to the drive: cd <DriveName>: );
  • New-VIInventoryDrive: Allows you to browse your Virtual Infrastructure like a drive (Example Usage: New-VIInventoryDrive -Name <DriveName> -Location (Get-Inventory <Folder-/Cluster-/ResourcePool-/HostName>); Then jump to drive: cd <DriveName>: );
  • Get-VIToolkitDocumentation: Opens the VI Toolkit Command Reference Help File;
  • Get-VIToolkitCommunity: Opens the VI Toolkit Community website.

More examples and scripts here soon!
Hugo

»crosslinked«

Build your own PowerShell universe

The PowerShell profile. In essence it’s nothing more than a personal script that runs each time you start PowerShell. But it’s possibilities are endless. But I’ll get to the possibilities later. Let’s first examine whether we have a profile.

 

Determining the location of your profile is easy. Just look at the value of the $profile variable:

PS D:\> $profile

C:\Documents and Settings\PeetersHJ\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

 

Now lets see whether this file exists:

PS D:\> Test-Path $profile

True

 

If it returns False, you can easily create it:

PS D:\> New-Item $profile -itemType file

 

 Directory: Microsoft.PowerShell.Core\FileSystem::C:\Documents and Settings\PeetersHJ\My Documents\WindowsPowerShell

 

Mode                LastWriteTime     Length Name

—-                ————-     —— —-

-a—         29-4-2008     10:10          0 Microsoft.PowerShell_profile.ps1

 

Now we can open the file and start editing it. We could browse to the location of the file and double-click it. But hey, we’ve got PowerShell. It makes life easy:

PS D:\> Invoke-Item $profile

 

What type of commands would you like to run each and every time you start PowerShell?

 

Maybe you would like to start at the root of your script directory:

Set-Location D:\scripts

 

How about storing some variables for use in your daily routines:

$weddingday = Get-Date -year 2006 -month 10 -day 13

 

But the real fun starts when using functions and filters.

A function is a command that optionally takes input and will generate output, while saving you from typing those same lines of code over and over again. A big advantage is that you can use this function in your scripts and only have to change one line of code instead of every occurrence in all your scripts when making modifications.

A filter has the same advantages. It allows you to replace complex scriptblocks and centralize them.

 

For example:

function Cred

{

$global:cred = Get-Credential $User

}

A simple function I use a lot. It asks for credentials and stores them in the $cred variable. It allows me to use -Credential $cred in all subsequent commands that need alternate credentials. The variable $user is defined earlier in my profile as “DOMAIN\adminuser”.

 

filter PRD

{

$_.Description -like “*@PRD*”

}

Filters like this one allow me to filter search results (in this case of AD computers) using the following syntax: |Where {PRD}.

 

PS: Did you notice the word global in the Cred function? $global:var turns the variable $var into a global variable, which allows it to be used throughout your PowerShell session. Without the global, the variable would only exist during the execution of the function. This can be handy when you need a temporary variable inside your function, instead of a variable that is the result of your function.