1 2 3 Previous Next 32 Replies Latest reply on Mar 18, 2009 12:02 AM by RobMokkink

    storage-path script

    RobMokkink Expert

       

      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?

       

       

        • 1. Re: storage-path script
          RobMokkink Expert

           

          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.

           

           

          • 2. Re: storage-path script
            LucD Guru
            vExpertCommunity WarriorsUser Moderators

            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"}
            

             

            1 person found this helpful
            • 3. Re: storage-path script
              RobMokkink Expert

               

              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.

               

               

              • 4. Re: storage-path script
                RobMokkink Expert

                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 = 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"

                 

                 

                #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)

                 

                 

                }

                 

                }

                • 5. Re: storage-path script
                  RobMokkink Expert

                   

                  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"

                   

                   

                   

                   

                   

                  • 6. Re: storage-path script
                    LucD Guru
                    vExpertUser ModeratorsCommunity Warriors

                    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.

                    1 person found this helpful
                    • 7. Re: storage-path script
                      RobMokkink Expert

                       

                      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.

                       

                       

                      • 8. Re: storage-path script
                        LucD Guru
                        User ModeratorsCommunity WarriorsvExpert

                        You need to pass some parameters to the PowerShell command.

                        See  Connecting to the vitoolkit from a commandline and also  VI Toolkit (for Windows) Admin Guide

                        • 9. Re: storage-path script
                          RobMokkink Expert

                           

                          Thanks Luc,

                           

                           

                          Everything is working.

                           

                           

                          • 10. Re: storage-path script
                            RobMokkink Expert

                            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.

                            • 11. Re: storage-path script
                              RobMokkink Expert

                              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.

                               

                              • 12. Re: storage-path script
                                CITITECHS Enthusiast

                                Great Script

                                • 13. Re: storage-path script
                                  RobMokkink Expert

                                   

                                  Yeah, the forum is working again

                                   

                                   

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

                                   

                                   

                                  • 14. Re: storage-path script
                                    LucD Guru
                                    User ModeratorsCommunity WarriorsvExpert

                                    Not from where I'm sitting

                                    Had to reply via email.

                                    Since yesterday morning I haven't seen recent content.

                                    The most recent entries are more than 1 day old.

                                    1 2 3 Previous Next