Automation

 View Only
Expand all | Collapse all

storage-path script

  • 1.  storage-path script

    Posted Jul 15, 2008 05:59 AM

    Yes another question.

    I am busy creating a script according to the sniplet examples on the blog, here is the code:

    #VARIABLES

    $policy = new-object VMware.Vim.HostMultipathInfoFixedLogicalUnitPolicy

    $policy.policy = "fixed"

    $ESXHOST = read-host -prompt "Enter Host"

    $USER = read-host -prompt "Enter User"

    $PASSWORD_PROMPT = read-host -assecurestring -prompt "Enter password"

    #CONVERT THE SECURE STRING

    $CONVERT_PASSWORD = http://System.Runtime.InteropServices.Marshal::SecureStringToBSTR($PASSWORD_PROMPT)

    $PASSWORD = http://System.Runtime.InteropServices.Marshal::PtrToStringAuto($CONVERT_PASSWORD)

    #CONNECT TO THE SERVER

    Get-esx -Server $ESXHOST -User $USER -Password $PASSWORD | fl

    #GET THE STORAGE PATHS

    $GET_HOST = Get-VMhost $ESXHOST

    $HOST_VIEW = Get-View $GET_HOST.id

    $STORAGESYSTEM = get-view $HOST_VIEW.ConfigManager.StorageSystem

    #MAKE THE PATH FOR EACH LUN FIXED

    write-output "SET LUN PATHS TO FIXED"

    $STORAGESYSTEM.StorageDeviceInfo.MultipathInfo.lun | where { $_.Path.length -gt 1 } | foreach { $STORAGESYSTEM.SetMultipathLunPolicy($_.ID, $policy) }

    When running the script i get errors:

    New-Object : Cannot find type [VMware.Vim.HostMultipathInfoFixedLogicalUnitPoli

    cy]: make sure the assembly containing this type is loaded.

    At F:\Powershell\set-storagepaths.ps1:6 char:21

    + $policy = new-object <<<< VMware.Vim.HostMultipathInfoFixedLogicalUnitPolicy

    Property 'policy' cannot be found on this object; make sure it exists and is se

    ttable.

    At F:\Powershell\set-storagepaths.ps1:7 char:9

    + $policy.p <<<< olicy = "fixed"

    It looks like the object is not supported.

    What am i doing wrong?



  • 2.  RE: storage-path script

    Posted Jul 15, 2008 06:57 AM

    I have to add path or lunid with it.

    The following code displays the ID and Path

    #VARIABLES

    $policy = new-object VMware.Vim.HostMultipathInfoFixedLogicalUnitPolicy

    $policy.policy = "fixed"

    $ESXHOST = read-host -prompt "Enter Host"

    $USER = read-host -prompt "Enter User"

    $PASSWORD_PROMPT = read-host -assecurestring -prompt "Enter password"

    #CONVERT THE SECURE STRING

    $CONVERT_PASSWORD = http://System.Runtime.InteropServices.Marshal::SecureStringToBSTR($PASSWORD_PROMPT)

    $PASSWORD = http://System.Runtime.InteropServices.Marshal::PtrToStringAuto($CONVERT_PASSWORD)

    #CONNECT TO THE SERVER

    Get-esx -Server $ESXHOST -User $USER -Password $PASSWORD | fl

    #GET THE STORAGE PATHS

    $GET_HOST = Get-VMhost $ESXHOST

    $HOST_VIEW = Get-View $GET_HOST.id

    $STORAGESYSTEM = get-view $HOST_VIEW.ConfigManager.StorageSystem

    #MAKE THE PATH FOR EACH LUN FIXED

    write-output "SET LUN PATHS TO FIXED"

    $STORAGESYSTEM.StorageDeviceInfo.MultipathInfo.lun | where { $_.Path.length -gt 1 } | select ID, Path

    But know i have to get the ID into a variable and check if the LUN number is a even or uneven number. At the moment is don't have a clue on how to do that. Still have to get used to pwoershell.



  • 3.  RE: storage-path script

    Posted Jul 15, 2008 07:32 AM

    To get the LUN Id into a variable you could use something like this

    $LunId = $STORAGESYSTEM.StorageDeviceInfo.MultipathInfo.lun | where { $_.Path.length -gt 1 } | select ID
    

    Better yet, make a new object and store all the properties you want to pass.

    $LUNInfo = "" | select ID, Path
    $STORAGESYSTEM.StorageDeviceInfo.MultipathInfo.lun | where { $_.Path.length -gt 1 } | %{$LUNInfo.ID = $_.ID; $LUNInfo.Path=$_.Path}
    

    To test for even/uneven you can use a binary and with 1 on the LUN Id.

    Something like this

    if($LUNId -band 1) {Write-Host "Uneven"}
    else {Write-Host "Even"}
    



  • 4.  RE: storage-path script

    Posted Jul 15, 2008 07:36 AM

    Hi Luc,

    Thanks for the hints. I already put all the lun's in a array. I think i will get the script ready today or tomorrow.



  • 5.  RE: storage-path script

    Posted Jul 15, 2008 08:52 AM

    The first concept version is working, here is the code, remeber, i only have 2 paths to each lun.

    I am curious what you think about the script, please let me know:

    #CREATED BY: Rob Mokkink Inter Access

    Add-PSSnapin -Name "VMware.VimAutomation.Core"

    #VARIABLES

    $policy = new-object VMware.Vim.HostMultipathInfoFixedLogicalUnitPolicy

    $policy.policy = "fixed"

    $UNEVEN = "vmhba1"

    $EVEN = "vmhba2"

    $ESXHOST = read-host -prompt "Enter Host"

    $USER = read-host -prompt "Enter User"

    $PASSWORD_PROMPT = read-host -assecurestring -prompt "Enter password"

    #CONVERT THE SECURE STRING

    $CONVERT_PASSWORD = ::SecureStringToBSTR($PASSWORD_PROMPT)

    $PASSWORD = ::PtrToStringAuto($CONVERT_PASSWORD)

    #CONNECT TO THE SERVER

    Get-esx -Server $ESXHOST -User $USER -Password $PASSWORD | fl

    #GET THE STORAGE PATHS

    $GET_HOST = Get-VMhost $ESXHOST

    $HOST_VIEW = Get-View $GET_HOST.id

    $STORAGESYSTEM = get-view $HOST_VIEW.ConfigManager.StorageSystem

    #MAKE THE PATH FOR EACH LUN FIXED

    write-output "SET LUN PATHS TO FIXED"

    #LOOP THROUGH THE LUN'S ADD THEM TO THE ARRAY

    $ARRLUN = $STORAGESYSTEM.StorageDeviceInfo.MultipathInfo.lun | where { $_.Path.length -gt 1 }

    #LOOP THROUGH LUN's

    foreach ($LUN in $ARRLUN)

    #CHECK IF THE LUN IS EVEN OR UNEVEN AND SET THE POLICY

    { $CHECK_LUN = $LUN.id.split(':')

    $CHECK_LUNID = $CHECK_LUN[2]

    if($CHECK_LUNID -band 1)

    {Write-Host $LUN.id = "Uneven"

    #GET THE PROPER VMHBA

    $GET_VMHBA = $LUN.Path | where {$_.Name -match $UNEVEN}

    $policy.prefer = $GET_VMHBA.Name

    #SET THE NEW PATH + POLICY

    $storageSystem.SetMultipathLunPolicy($LUN.id, $policy)

    }

    else

    {Write-Host $LUN.id = "Even"

    #GET THE PROPER VMHBA

    $GET_VMHBA = $LUN.Path | where {$_.Name -match $EVEN}

    $policy.prefer = $GET_VMHBA.Name

    #SET THE NEW PATH + POLICY

    $storageSystem.SetMultipathLunPolicy($LUN.id, $policy)

    }

    }



  • 6.  RE: storage-path script

    Posted Jul 15, 2008 09:13 AM

    One thing that bothers me is that when is want to execute the script from the commandline like so:

    powershell.exe F:\Powershell\set-storagepaths-fixed.ps1

    I get the following error:

    New-Object : Cannot find type [VMware.Vim.HostMultipathInfoFixedLogicalUnitPoli

    cy]: make sure the assembly containing this type is loaded.

    At F:\Powershell\set-storagepaths-fixed.ps1:6 char:21

    + $policy = new-object <<<< VMware.Vim.HostMultipathInfoFixedLogicalUnitPolicy

    Property 'policy' cannot be found on this object; make sure it exists and is se

    ttable.

    At F:\Powershell\set-storagepaths-fixed.ps1:7 char:9

    + $policy.p <<<< olicy = "fixed"



  • 7.  RE: storage-path script

    Posted Jul 15, 2008 09:29 AM

    You can force a load of the assembly in the beginning of your script.

    [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("vmware.vim")
    

    I attached a file with this line since the forum SW tends to reformat certain characters.



  • 8.  RE: storage-path script

    Posted Jul 15, 2008 09:46 AM

    Thanks Luc,

    Now i get the following error:

    out-lineoutput : Object of type "Microsoft.PowerShell.Commands.Internal.Format.

    FormatStartData" is not legal or not in the correct sequence. This is likely ca

    used by a user-specified "format-list" command which is conflicting with the de

    fault formatting.

    I don't get this error in the powershell console or vitoolkit console, just on the commandline.



  • 9.  RE: storage-path script
    Best Answer

    Posted Jul 15, 2008 10:06 AM

    You need to pass some parameters to the PowerShell command.

    See and also



  • 10.  RE: storage-path script

    Posted Jul 15, 2008 10:08 AM

    Thanks Luc,

    Everything is working.



  • 11.  RE: storage-path script

    Posted Jul 17, 2008 05:46 PM

    Here's another version of the script.

    It logs in to the VirtualCenter server, enumerates all ESX host and set the proper paths for the luns.

    I also added another script which creates a password file for you. You need to pass the password file en password as arguments, like so

    .\create-passfile-v2.ps1 pass.file password

    Remember that the user who created the password file has rights to read it, i use psexec and a .hta to get that done (i use .hta for allmost everything)

    You can then run the hba loadbalance script with 3 parameters:

    1. password file

    2. virtualcenter server

    3. username

    I have this already have the scripts above running in the lab environment at the customer site i am now. It is scheduled a couple of times a day.

    I anyone has any comments etc. please inform me, so i can make the script better or add functionality.



  • 12.  RE: storage-path script

    Posted Jul 22, 2008 06:18 AM

    Another update. I added some more error handling and the script uses the credentials of the current logged on user, or the user which runs the scheduled task.



  • 13.  RE: storage-path script

    Posted Jul 23, 2008 10:59 PM

    Great Script



  • 14.  RE: storage-path script

    Posted Jul 24, 2008 06:33 AM

    Yeah, the forum is working again :smileywink:

    Cittechs see version 5 which you can use a password file.



  • 15.  RE: storage-path script

    Posted Jul 24, 2008 07:37 AM

    Not from where I'm sitting :smileysad:

    Had to reply via email.

    Since yesterday morning I haven't seen recent content.

    The most recent entries are more than 1 day old.



  • 16.  RE: storage-path script

    Posted Jul 24, 2008 07:46 AM

    I am also getting thread error again. And i have to hit the refresh button a couple of times before the pages get updated. :smileyangry:



  • 17.  RE: storage-path script

    Posted Mar 11, 2009 06:38 PM

    Is this supposed to work on ESXi?

    I get an immediate failure - "ERROR CONNECTING TO HOST: [ERROR}" and "ESXHOST: HAS NO LUNS "

    Tried pointing to localhost and vCenter IP This is also with vCenter v4



  • 18.  RE: storage-path script

    Posted Mar 12, 2009 06:39 AM

    I also tested it on vSphere v4, no problems. Also with esx 3i i didn had any problems.

    Must be an authentication error.



  • 19.  RE: storage-path script

    Posted Mar 12, 2009 01:13 PM

    What authentication? I see nowhere in the script for it (as a static entry), and I never get any kind of query via either command line or pop-up. As stated - the errors are immediate when the script starts.

    Perhaps you can tell me what I should expect to see WRT authentication..... FWIW - I ran numerous other scripts requiring authentication against the vCenter host and had no issues.

    And just to be clear, I am not using vSpehere - it is ESXi 3.5.3+updates - the vCenter Server is 2.5 v4

    I was also wondering - if the host has 4 paths does it balance across all 4? (e.g.paths 1.0, 1.1, 2.0, 2.1)



  • 20.  RE: storage-path script

    Posted Mar 12, 2009 01:26 PM

    Hi Vancod,

    The script uses intergrated authentication. That means, the account running the script has sufficient rights in virtualcenter. In the script there is a line that specifies the virtualcenter host.

    The script is specific to my environment, i only have 2 paths to a lun. But you can customize the script quite easily.



  • 21.  RE: storage-path script

    Posted Mar 12, 2009 04:12 PM

    Ah, OK - I was running it via remote shell.

    Thanks for the pointers - I will look at making it 4-path savvy (but I'm still a noob)



  • 22.  RE: storage-path script

    Posted Mar 12, 2009 05:32 PM

    I actually wrote my own version of this script that requires the VI Toolkit 1.5 a month ago, it will work with any # of paths as long as the number of paths is consistent among all nodes in a cluster:

    http://vmjunkie.wordpress.com/2009/01/29/balancing-lun-paths-on-your-esx-hosts-with-powershell/

    This uses the new Toolkit CMDLets to do the storage path stuff without SDK commands.



  • 23.  RE: storage-path script

    Posted Mar 12, 2009 07:02 PM

    Thanks Justin - will have to check that out next time I have a crack at an actual SAN

    So - in my case with 2 ports / 4 paths I basically have:

    port 1, path 0 and port 1, path 1 (1,0 and 1,1)

    port 2, path 0 and port 2, path 1 (2,0 and 2,1)

    Let's say I have 8 LUNs and 3 hosts - what is the resulting balance going to look like? Will all hosts simply mount the same LUN via paths balanced on all hosts identically, or will it be unique per host?

    What I'm hoping for is a script that would take LUN1 and put it as:

    Path 1,0 on host 1

    Path 1,1 on host 2

    Path 2,0 on host 3

    Path 2,1 on host 4

    .....etc.

    As opposed to simply moving said LUN to, say, path 2,1 on all hosts



  • 24.  RE: storage-path script

    Posted Mar 12, 2009 07:05 PM

    Make sure your storage supports balancing the lun's this way. Talk to your san admins before you do this.

    SAN's like XP series support the way we balance our lun's this way.



  • 25.  RE: storage-path script

    Posted Mar 12, 2009 07:15 PM

    Rob is correct - Be very very careful balancing your LUNs this way because on the vast majority of arrays, this will be disastrous.

    As you can see here in Frank's blog entry:

    http://frankdenneman.wordpress.com/2009/02/09/hp-continuous-access-and-the-use-of-lun-balancing-scripts/

    ...he describes the two types of "Active-Active" arrays. If something is an "Asymmetrical Active-Active" array, then you want to access the same LUN via the same path on all servers.

    Only if you're using some very high-end arrays (like the HP XP that Rob mentioned) can you use all paths to the same LUN simultaneously.

    My script will make it so each server accesses the same LUN via the same path, but different LUNs will be accessed across many paths.

    What storage array are you using?



  • 26.  RE: storage-path script

    Posted Mar 12, 2009 07:26 PM

    It's hosted storage - but based on 3PAR. Not sure of the actual back end model. It didn't start to have issues, but then nothing's been populated. I'll have to look into the support for using all paths, but we were not adivsed explicitly not to do so.



  • 27.  RE: storage-path script

    Posted Mar 12, 2009 07:40 PM

    As far as I know 3PAR is an asymmetric active-active aray.



  • 28.  RE: storage-path script

    Posted Mar 14, 2009 01:53 AM

    Thanks for the info guys. I have been advised that the storage in question is symmetrical active-active, so I'm in good shape. Note that as I understand it many of the newer / upper echelon 3PAR devices are SAA.



  • 29.  RE: storage-path script

    Posted Mar 14, 2009 04:02 AM

    If you have the LUNs presented to every host the same way, then you should be able to use my modified script on my Sky Drive. It is also attached.

    Let me know if it works.

    All I did was add an offset to each server as it iterates through the outer loop, so Server 1 will start with Path 1, Server 2 will start with Path 2, etc...



  • 30.  RE: storage-path script

    Posted Mar 14, 2009 02:10 PM

    Thanks Justin - although I'm not sure when I'll be in front of another cluster to give it a try.



  • 31.  RE: storage-path script

    Posted Mar 14, 2009 02:32 PM

    Vancod,

    You can easily change the script:

    
    
    change this line:
    
    
    $VMHosts = Get-Cluster $clusterName | Get-VMHost
    
    
    to:
    
    
    $VMHosts = get-vmhost | where {$_.state -eq "connected" -or $_state -eq "maintenance"}
    
    
    

    Also it would be wise to put some more error handling in the script.



  • 32.  RE: storage-path script

    Posted Mar 18, 2009 01:02 AM

    Rob,

    Where would you suggest adding some error checking? I'll admit I'm not the most advanced script writer so I'd love some pointers on that.



  • 33.  RE: storage-path script

    Posted Mar 18, 2009 07:03 AM

    Justin,

    When you connect to the cluster end try to enumerate the hosts, check if your array is not empty etc, when you enumerate the luns on the esx host and put them in an array. Check if you can successfully connect to virtualcenter.

    Also when setting the paths

    $lun|Set-ScsiLun -MultipathPolicy Fixed -PreferredPath $paths[$count] #error handling

    if (! $?)
       {

    write-hosts "[ERROR] -Foregroundcolor Red" }else { write-host "[OK] -Foregroundcolor Green" }

    stuff like improve the script. You could also look at adding a log file etc.