Archive

Posts Tagged ‘powershell’

Using the Data ONTAP PowerShell Toolkit to add vSphere Storage

June 9th, 2010 Jase 3 comments

Over the past year or so, I have started using PowerShell more and more.

I was really exited to see the release of the NetApp Data ONTAP PowerShell Toolkit 1.0. I say this, because I have been using the “unsupported” PoshOnTap PowerShell extensions for some NetApp management. I have to give credit where credit is due, and Glenn Sizemore has done a great job with PoshOnTap.

PowerShell is really powerful, and because more and more vendors are providing more and more modules for the management of different aspects of the enterprise. With one script, I can create a NetApp volume, export it as an NFS export, and then connect it to all of my VMware vSphere hosts.

Note: This script is meant to be run from the vSphere PowerCLI.

##########################################################
# Create-And-Add-Storage-example.ps1
# Jase McCarty 6/7/2010
# Posh Script to add a volume on a NetApp Filer
# and present it to all vSphere hosts
##########################################################

# Add the vSphere PowerCLI SnapIn and the DATA ONTAP Module
Import-module DataONTAP

# Set my variables
$vCenter = vcenter.jasemccarty.com
$Filer = netapp1.jasemccarty.com
$aggr = aggr1
$newvol = volx

# Connect to vCenter and our NetApp Filer
Connect-VIServer $vCenter
Connect-NaController $Filer

# Create a new volume
New-NaVol $newvol $aggr 500g

# Set some options for the new volume
Set-NaVolOption $newvol no_atime_update yes
Set-NaVolOption $newvol fractional_reserve 0

# Set the SnapShot Reserve to 0
Set-NaSnapshotreserve  $newvol 0
Set-NaSnapshotschedule $newvol -Weeks 0 -Days 0 -Hours 0

# Add an NFS export
Add-NaNfsExport /vol/$newvol -Persistent -ReadWrite all-hosts -NoSuid -SecurityFlavors sys,krb5

# Get all the vSphere Hosts and add the NFS export
$Hosts = Get-VMHost
ForEach ($H in $Hosts)
{
 New-Datastore -Nfs -VMHost $_.Name -NAME $newvol -Path /vol/$newvol -NfsHost $Filer;
}

**Note, this is a pretty incomplete script.  Security for the export will have to be addressed, and the Filer management IP (netapp1.jasemccarty.com) is probably not the same as the IP used for the NFS export.  This is just an example of how this can be used.

Pretty cool.  I can only imagine the Poshibilities.

PowerCLI cmdlets: Get/Set-VMGuestNetworkInterface

June 8th, 2010 Jase No comments

After my last post on Get/Set-OSCustomizationNicMapping, I started looking further at what options there were to configure network settings for guests that were “already” created.

It is one thing to be able to set the network adapter properties during the cloning process and something entirely different to do it after the fact.  Maybe a mistake was made during the cloning process.  More likely a network migration could be planned, and a completely different way of doing things would be needed.

Using Invoke-VMScript
In January, I wrote a post on how to Update VMware Windows Guest DNS and WINS through PowerCLI that uses the Invoke-VMScript cmdlet to run a batch file in Windows to set the DNS and WINS information.  For those versed with netsh, it is powerful, but there are requirements

One pretty big requirement is that the  name of the network adapter must be known.  On older VM’s, or possibly P2V’ed VM’s the network adapter name could be something like “Local Network Connection 2″, “Local Network Connection 3″, etc.  Using the above mentioned script is going to fail if the connection name is not known.  The only way around that, would be to put some logic in to determine the name to use when calling the script.  Additionally using netsh with DNS and WINS requires the addition of new values, and deletion of old values.

Another issue with the Invoke-VMScript method, is that it requires different scripts for Windows and Linux guests.

A Different Way
So I started to peruse the online docs for the current vSphere 4.0 Update 1 and found the Get/Set-VMGuestNetworkInterface cmdlets.  Just like my last post, I couldn’t really find any example scripts of anyone using it.

Here is the syntax for these cmdlets:

Get-VMGuestNetworkInterface
Syntax
Get-VMGuestNetworkInterface [[-VM] <VirtualMachine[]>] [-VMGuest <VMGuest[]>] [-Server <VIServer[]>] [-ToolsWaitSecs <Int32>] [-GuestPassword <SecureString>] [-GuestUser <String>] [-GuestCredential <PSCredential>] [-HostPassword <SecureString>] [-HostUser <String>] [-HostCredential <PSCredential>] [<CommonParameters>]

Set-VMGuestNetworkInterface
Syntax
Set-VMGuestNetworkInterface -VmGuestNetworkInterface <VMGuestNetworkInterface[]> [-WinsPolicy <DhcpPolicy>] [-Wins <String[]>] [-DnsPolicy <DhcpPolicy>] [-Dns <String[]>] [-IPPolicy <DhcpPolicy>] [[-Gateway] <Object>] [[-Netmask] <String>] [[-Ip] <IPAddress>] [-ToolsWaitSecs <Int32>] [-GuestPassword <SecureString>] [-GuestUser <String>] [-GuestCredential <PSCredential>] [-HostPassword <SecureString>] [-HostUser <String>] [-HostCredential <PSCredential>] [-WhatIf] [-Confirm] [<CommonParameters>]

One thing that struck me as odd, was that both of these commands have the following statements as part of their documentation:

… Consider that this functionality is experimental. …

and

Notes
Supported on ESX 3.5 and newer. …

So which is it? Maybe @cshanklin can address this at some point.

How Does It Really Work?
Are the Get/Set-VMGuestNetworkInterface cmdlets really that different from Invoke-VMScript? Not really, except for the fact that VMware has taken some of the guesswork out of the process.  These cmdlets actually call scripts in the Scripts folder located under the installation path of the vSphere PowerCLI.  There are separate scripts for Windows OSes and Linux OSes (currently only supported on RHEL 5).

The netsh scripting limitation I mentioned earlier (network connection name) is actually taken care of by these scripts.  Additionally these scripts can be modified/added to if desired.

There are a couple things to be aware of.

  • Set-VMGuestNetworkInterface does not work in my environment if the vNic is disconnected.
  • I could not use the vSphere PowerCLI in x64 mode to run the script.  The PowerCLI stated that I needed to use the 32bit PowerCLI instead.
  • WINS settings are not available in Linux guests.

How To Update
Now how do I go and update IP addresses on several machines (possibly those that I created in my last post)?

Here are the contents of my text file (C:\vms.csv) that contained my VM names and other settings.

basevm,datastore,vmhost,custspec,vmname,ipaddress,subnet,gateway,pdnswins,sdnswins,vlan
BASEVM,DS1,ESXi1,W2K3,VM01,192.168.0.80,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Primary
BASEVM,DS1,ESXi1,W2K3,VM02,192.168.0.81,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Primary
BASEVM,DS1,ESXi1,W2K3,VM03,192.168.0.82,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Primary
BASEVM,DS1,ESXi1,W2K3,VM04,192.168.255.83,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Secondary
BASEVM,DS1,ESXi1,W2K3,VM05,192.168.255.84,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Secondary

So I’ll modify it because I didn’t mean to use 192.168.0.x, I meant to use 192.168.1.x. And I’ll pull out the things I don’t need, like basevm, datastore, vmhost, & custspec.

vmname,ipaddress,subnet,gateway,pdnswins,sdnswins,vlan
VM01,192.168.1.80,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Primary
VM02,192.168.1.81,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Primary
VM03,192.168.1.82,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Primary
VM04,192.168.255.83,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Secondary
VM05,192.168.255.84,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Secondary

Now I’ll save it as C:\vms-newip.csv.

Also, unlike the Get/Set-OSCustomizationNicMapping cmdlet, I’ll have to connect specifically to the host and the guest with appropriate credentials.  I have to do this, because credentials are required in the guest to make the change.

The Script
I’ll use the following script to update the VM’s.

##########################################################
# updateip.ps1
# Jase McCarty 6/6/2010
# Posh Script to update IP
# addresses in Virtual Machines
##########################################################

Connect-VIServer vcenter.jasemccarty.com

$HostCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter ESX host credentials", "", "")
$GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials", "", "")

$vmlist = Import-CSV C:\vms.csv

foreach ($item in $vmlist) {

  # I like to map out my variables
  $vmname = $item.vmname
  $ipaddr = $item.ipaddress
  $subnet = $item.subnet
  $gateway = $item.gateway
  $pdnswins = $item.pdnswins
  $sdnswins = $item.sdnswins

  #Get the current interface info
  $GuestInterface = Get-VMGuestNetworkInterface -VM $vmname -HostCredential $HostCred -GuestCredential $GuestCred

  #If the IP in the VM matches, then I don't need to update
  If ($ipaddr -ne $($GuestInterface.ip)) {
      Set-VMGuestNetworkInterface  -VMGuestNetworkInterface $GuestInterface -HostCredential $HostCred -GuestCredential $GuestCred -IP $ipaddr -Netmask $subnet -Gateway $gateway -DNS $pdnswins,$sdnswins -WINS $pdnswins,$sdnswins
  }
}

As can be seen, it doesn’t take a complex script to update IP information in guests.

Keep in mind that not all of the attributes that I chose to use are required.  If desired, only the IP, DNS, WINS, etc can be updated.  My script just happens to update several of these items.  Additionally, this can be used to change a guest from a static IP address to a DHCP address as well.

Hopefully this script will help you when presented with a similar task of updating IP addresses/DNS/WINS settings.

For More Info
For more info on each of these commands, look here for the VMware documentation:

PowerCLI cmdlets: Get/Set-OSCustomizationNicMapping

June 5th, 2010 Jase No comments

At some point in the near future, I have to create several hundred new VM’s.  These VM’s will be using the same Customization Specification, but some will need to have Static IP addresses, and others will need to have DHCP addresses that are reserved based on Mac Address.

The Task
Alright, how am I going to create 400-500 VM’s with specific names and specific IP’s (with some being DHCP)?  I know there are some tools like the RCU from NetApp that will rapidly provision VM’s, but I’m not looking for 100% clones, as not all of these are going to be used for the same purpose (not using VDI here).

Customization Specifications typically can only be used once per VM when attempting to use a Static IP address.  Using DHCP, a Customization Specification can be easily reused, as it doesn’t change.  The problem is, what if I want to give each VM a Static IP.  That requirement makes it a little more difficult.

The Tool: PowerCLI
There are a couple new cmdlets in the 4.0 Update 1 release of the PowerCLI that will help accomplish this task.

  • Get-OSCustomizationNicMapping
  • Set-OSCustomizationNicMapping

Using these together in a PowerCLI script, settings like IP address (if static), DHCP, gateway, DNS & WINS settings, and Mac Address can easily be set.

The syntax for these are as follows:

Get-OSCustomizationNicMapping
SYNTAX
Get-OSCustomizationNicMapping [-Spec] <OSCustomizationSpec[]> [-Server <VIServer[]>] [<CommonParameters>]

Set-OSCustomizationNicMapping
SYNTAX
Set-OSCustomizationNicMapping -OSCustomizationNicMapping <OSCustomizationNicMapping[]> [-Position <Int32>] [-Server <VIServer[]>] [-IpMode <OSCustomizationIPMode>] [-VCApplicationArgument <String>] [[-IpAddress] <String>] [[-SubnetMask] <String>] [[-DefaultGateway] <String>] [-AlternateGateway <String>] [[-Dns] <String[]>] [-Wins <String[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

Set-OSCustomizationNicMapping -OSCustomizationNicMapping <OSCustomizationNicMapping[]> [-NetworkAdapterMac <String>] [-Server <VIServer[]>] [-IpMode <OSCustomizationIPMode>] [-VCApplicationArgument <String>] [[-IpAddress] <String>] [[-SubnetMask] <String>] [[-DefaultGateway] <String>] [-AlternateGateway <String>] [[-Dns] <String[]>] [-Wins <String[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

Get-Help Get/Set-OSCustomizationNicMapping -full can give you more information on each of these cmdlets.

My Script
So I put together a little script to read content from a file, set a nic mapping for the selected Customization Specification, clone the VM, and set the appropriate VLAN for the VM.

The text file proceeds with the assumption that the DNS and WINS servers are the same.  The pdnswins and sdnswins variables are used twice each, one for the primary DNS/WINS and once for the secondary DNS/WINS.  Here are the contents of my text file that contains my VM names and other settings.

basevm,datastore,vmhost,custspec,vmname,ipaddress,subnet,gateway,pdnswins,sdnswins,vlan
BASEVM,DS1,ESXi1,W2K3,VM01,192.168.0.80,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Primary
BASEVM,DS1,ESXi1,W2K3,VM02,192.168.0.81,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Primary
BASEVM,DS1,ESXi1,W2K3,VM03,192.168.0.82,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Primary
BASEVM,DS1,ESXi1,W2K3,VM04,192.168.255.83,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Secondary
BASEVM,DS1,ESXi1,W2K3,VM05,192.168.255.84,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Secondary

I saved the file as C:\vms.csvEven though I named the file with a .csv extenstion, I decided to use colons rather than commas.  I did this because I wanted to send multiple DNS/WINS entries to my script, but I’ll get into that a little later. I have updated the script to use the standard comma delimiter.

The script to clone the VM’s and set their appropriate values

##########################################################
# cloneandsetip.ps1
# Jase McCarty 6/5/2010
# Posh Script to clone VM's and set appropriate
# IP addresses in Windows Virtual Machines
##########################################################
Connect-VIServer vcenter.jasemccarty.com

$vmlist = Import-CSV C:\vms.csv

foreach ($item in $vmlist) {

	# I like to map out my variables
	$basevm = $item.basevm
	$datastore = $item.datastore
	$vmhost = $item.vmhost
	$custspec = $item.custspec
	$vmname = $item.vmname
	$ipaddr = $item.ipaddress
	$subnet = $item.subnet
	$gateway = $item.gateway
	$pdns = $item.pdnswins
	$pwins = $item.pdnswins
	$sdns = $item.sdnswins
	$swins = $item.sdnswins
	$vlan = $item.vlan

	#Get the Specification and set the Nic Mapping (Apply 2 DNS/WINS if 2 are present)
	If ($Varable) {
		Get-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns -Wins $pwins,$swins
	} else {
		Get-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns -Wins $pwins
	}

	#Clone the BaseVM with the adjusted Customization Specification
	New-VM -Name $vmname -VM $basevm -Datastore $datastore -VMHost $vmhost | Set-VM -OSCustomizationSpec $custspec -Confirm:$false

	#Set the Network Name (I often match PortGroup names with the VLAN name)
	Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $vlan -Confirm:$false

	#Remove the NicMapping (Don't like to leave things unkept)
	Get-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Remove-OSCustomizationNicMapping -Confirm:$false
}

Set-OSCustomizationNicMapping is very powerful.  Further examination of the documentation will show that things like setting the Mac Address and Alternate Gateway are also possible.

The Gotchas
There are a couple gotchas that I found, and you might want to be aware of.

  • The setting -PrimaryWins in the documentation does not work, use -Wins. Documentation and error messages will continue to state -PrimaryWins, but that attribute doesn’t work.
  • Multiple settings for DNS/WINS must read something like this 192.168.0.199,192.168.0.198.

    I had some difficulty passing 2 DNS/WINS entries into a single variable, so I am basically performing a check to see if a second entry is present.

In Closing
Thanks VMware for the very useful cmdlets to help make our jobs easier.

For more info on each of these commands, look here for the VMware documentation:

Set Guest OS I/O Timeout Settings via PowerShell

April 29th, 2010 Jase 5 comments

Back in October 2009, I wrote a post on how to remotely set the TimeoutValue for the Disk service in Windows per NetApp’s best practices.  It is kind of a pain to grab a list of VM’s, make a bunch of batch files, call them, and so on.

I was playing around with the PSRemoteRegistry PowerShell module, and figured out a better way of doing it… Using PowerShell.

To make this work
A few things are needed:

  • PowerShell 2.0
  • vSphere PowerCLI
  • PSRemoteRegistry
  • vCenter Server and appropriate access by the account running the script
  • Connectivity to the Guest as the changes are made via a remote registry call
  • Credentials in the VM to make changes using the same account running the script
  • Run this script from the vSphere PowerCLI

The Workflow
The script will basically need to enumerate all VM’s, perform a registry query to see if the TimeoutValue exists, and depending on what the result is, either update the setting or create it if it does not exist.  Because this only relates to Windows OSes, I chose to only look at VM’s running Windows.  Also, no point in attempting the update on a VM that isn’t powered on, given that I’m making a remote registry call.

The PowerShell functions that help with the process are Test-RegValue and Set-DWord, both of which are part of the Remote Registry PowerShell Module.  I’m not going to go into the syntax of these functions, but more information can be found on them using Get-Help Test-RegValue and Get-Help Set-DWord once the PSRemoteRegistry module is loaded.

Two variables I’m using are $tovdec and $tovhex which are TimeOut Value in Decimal and TimeOut Value in Hexadecimal.  These can easily be changed to a value that is appropriate with whatever storage is being used, based on the recommendations of the storage vendor (NetApp, EMC, etc).

The Script
Here is the script I came up with:

##########################################################
# correctvmtimeout.ps1
# Jase McCarty 4/29/2010
# Posh Script to set/correct TimeoutValue
# in Windows Virtual Machines
##########################################################

#Import the PSRemoteRegistry
Import-Module PSRemoteRegistry

#Connect to vCenter Server
Connect-VIServer MYvCenterServer

# Specify the Hive, RegKey, and DWORD Value
$Hive = "LocalMachine"
$DiskKey = "SYSTEM\CurrentControlSet\Services\Disk"
$tovdec = "190"
$tovhex = "0xBE"

# Get only Windows VM's that are Powered On and are Windows Guests
Get-VM | Sort-Object Name | Where { $_.PowerState -eq "PoweredOn" } | %{get-view $_.ID} | Foreach {If ($_.guest.GuestFamily -eq "windowsGuest") {

     # Check to see if the value is present, and if it is, what is the value
     $Key = Test-RegValue -CN $_.Name -Hive $Hive -Key $DiskKey -Value TimeoutValue -PassThru

     If ($Key -eq $false) {
          Write-Host $_.Name,incorrect,missing,correcting
          Set-RegDWord -CN $strComputer -Hive $Hive -Key $DiskKey -Value TimeoutValue -Data $tovhex -Force
          }
else {
          If ($Key.Data -eq $tovdec) {
              Write-Host $Key.ComputerName, correct,$Key.Data
          }
          else    {
             Write-Host $Key.ComputerName,incorrect,$Key.Data,correcting
             Set-RegDWord -CN $Key.ComputerName -Hive $Hive -Key $DiskKey -Value TimeoutValue -Data $tovhex -Force
          }
       }
    }
}

I hope this helps anyone looking to perform a mass update of the TimeoutValue registry setting.  Additionally, this could be integrated into a config/check script that is run from time to time to ensure that all VM’s are held in compliance to the appropriate values a storage vendor recommends.

I have a couple different types of storage, so maybe I will add some additional logic to determine the storage provider, and adjust accordingly… Now to find the time to do it.

And many thanks to Shay Levy for the work on the PSRemoteRegistry module!

Add a Published App to XenApp using Powershell

April 14th, 2010 Jase No comments

I’m using Powershell more and more daily, and started looking at the XenApp Commands Technology Preview (v2), which are Citrix developed Powershell cmdlets to manage a Citrix environment.

To gain access to this tech preview, you will need a Citrix account.

After resetting my Citrix account password, to regain access, I logged in, and downloaded the add on for Powershell.

To be able to use this tech preview, at a minimum, you must have XenApp 4.5 with HRP3 or higher.  XenApp versions previous to 4.5 HRP3 do not have the .NET classes that the cmdlets were built against.

I am using the Citrix trial virtual appliance (XenApp 5) to test against.  I’ll be honest, I didn’t install Hyper-V, or use my Windows Virtual PC, that is included with Windows 7.  I did open the appliance with Windows Virtual PC, only long enough to create a config file for it.  I then used VMware Converter to convert it for use by VMware Workstation 7.

I powered on the newly converted VM, and performed various tasks like adding VMware Tools, removing the Microsoft equivalent, and adding the VM to my test domain, and renaming the VM to XenApp1.  I’m not going through that process here.

After installing the XenApp Commands, I figured I would try to create a new published app.  This is a 3 step process, including:

  1. Create the published application
  2. Add users/groups to the application
  3. Assign servers to the published app

To create the application I used New-XAApplication cmdlet. I’m not going to get into all the specifics of the New-XAApplication parameters, but “Get-Help New-XAApplication” will do for you if you want to know all the parameters.

So I created an application using the following code:

New-XAApplication -BrowserName “Notepad” -ApplicationType “ServerInstalled” -DisplayName “Notepad” -FolderPath “Applications/Test” -Enabled $true -CommandLineExecutable “c:\windows\system32\notepad.exe” -WorkingDirectory “C:\Windows\system32″ -AnonymousConnectionsAllowed $false -AddToClientStartMenu $true -InstanceLimit “-1″ -WindowType “1024×768″ -ColorDepth “Colors256″

That got me started, but it doesn’t provide access to the app to specific users/groups, or publish the application to a server.

To add users to the application, I used the Add-XAApplicationAccount cmdlet:

Add-XAApplicationAccount -BrowserName “Notepad” -Accounts “JASEMCCARTY\jase”

To publish the application, I used the Add-XAApplicationServer cmdlet:

Add-XAApplicationServer -BrowserName “Notepad” -ServerNames “XENAPP1″

So a quick Powershell script to create, assign, and publish an application would look like this:

New-XAApplication -BrowserName “Notepad” -ApplicationType “ServerInstalled” -DisplayName “Notepad” -FolderPath “Applications/Test” -Enabled $true -CommandLineExecutable “c:\windows\system32\notepad.exe” -WorkingDirectory “C:\Windows\system32″ -AnonymousConnectionsAllowed $false -AddToClientStartMenu $true -InstanceLimit “-1″ -WindowType “1024×768″ -ColorDepth “Colors256″
Add-XAApplicationAccount -BrowserName “Notepad” -Accounts “JASEMCCARTY\jase”
Add-XAApplicationServer -BrowserName “Notepad” -ServerNames “XENAPP1″

Pretty easy if you ask me.

Couple this with a Get-Content command and many applications could be published very quickly.

I look forward to the GA release of the XenApp Commands for Powershell.

Categories: Virtualization Tags: , ,