Archive

Posts Tagged ‘powershell’

Using the Data ONTAP PowerShell Toolkit to add vSphere Storage

June 9th, 2010 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 17 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.

Read more…

PowerCLI cmdlets: Get/Set-OSCustomizationNicMapping

June 5th, 2010 16 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.

Read more…

Set Guest OS I/O Timeout Settings via PowerShell

April 29th, 2010 7 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 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:windowssystem32notepad.exe” -WorkingDirectory “C:Windowssystem32″ -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 “JASEMCCARTYjase”

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:windowssystem32notepad.exe” -WorkingDirectory “C:Windowssystem32″ -AnonymousConnectionsAllowed $false -AddToClientStartMenu $true -InstanceLimit “-1″ -WindowType “1024×768″ -ColorDepth “Colors256″
Add-XAApplicationAccount -BrowserName “Notepad” -Accounts “JASEMCCARTYjase”
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: , ,