RobMokkink
Expert
Expert

storage-path script

Jump to solution

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?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You need to pass some parameters to the PowerShell command.

See and also


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
32 Replies
RobMokkink
Expert
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.

0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RobMokkink
Expert
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.

0 Kudos
RobMokkink
Expert
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 = ::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)

}

}

0 Kudos
RobMokkink
Expert
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"

0 Kudos
LucD
Leadership
Leadership

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RobMokkink
Expert
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.

0 Kudos
LucD
Leadership
Leadership

You need to pass some parameters to the PowerShell command.

See and also


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
RobMokkink
Expert
Expert

Thanks Luc,

Everything is working.

0 Kudos
RobMokkink
Expert
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.

0 Kudos
RobMokkink
Expert
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.

0 Kudos
CITITECHS
Contributor
Contributor

Great Script

0 Kudos
RobMokkink
Expert
Expert

Yeah, the forum is working again Smiley Wink

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

0 Kudos
LucD
Leadership
Leadership

Not from where I'm sitting Smiley Sad

Had to reply via email.

Since yesterday morning I haven't seen recent content.

The most recent entries are more than 1 day old.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
RobMokkink
Expert
Expert

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:

0 Kudos
vancod
Contributor
Contributor

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

0 Kudos
RobMokkink
Expert
Expert

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

Must be an authentication error.

0 Kudos
vancod
Contributor
Contributor

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)

0 Kudos
RobMokkink
Expert
Expert

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.

0 Kudos