VMware Cloud Community
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Create new iSCSI datastores

Hi,

I'm trying to scan my iSCSI HBA and create two datastores.  There are two LUNs presented to the hosts, both empty.  Trying this:

$i = 0

ForEach ($VMHost in $esxi_array){

$i=$i+1

Get-VMHostStorage $VMHost -RescanAllHba

$lun = Get-VMHost $VMHost | Get-ScsiLun | Where { $_.Vendor -eq "MSFT"}

New-Datastore -VMHost $VMHost -Name iSCSI_LUN$i –Path $lun.CanonicalName -Vmfs -Confirm:$false

}

The error I get is "New-Datastore : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported.", which I guess is because I'm passing an array ($lun) to something requiring a string.

Any ideas on how I can fix this?

Many thanks,

-Mark

Reply
0 Kudos
1 Solution

Accepted Solutions
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Okay, got the error checking working:

ForEach ($VMHost in $esxi_array){

$i = 0

Get-VMHostStorage $VMHost -RescanAllHba -Rescanvmfs

$datastores = Get-Datastore -VMHost $VMHost

if (-Not ($datastores -like "iSCSI*")){

ForEach ($lun in $luns){

$i=$i+1

$luns = Get-VMHost $VMHost | Get-ScsiLun | Where { $_.Vendor -eq "MSFT"}

New-Datastore -VMHost $VMHost -Name iSCSI_LUN$i –Path $lun.CanonicalName -Vmfs -Confirm:$false

}

}

}

It checks to see if any datastores exist named "iSCSI*" and if not, creates them.  If it finds any (say on the next host) then it skips creation and moves along.

Only slightly uglier than my first attempt.  Maybe LucD can offer a prettier version? 🙂

-Mark

View solution in original post

Reply
0 Kudos
4 Replies
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

So far I have come up with this:

ForEach ($VMHost in $esxi_array){

$i = 0

Get-VMHostStorage $VMHost -RescanAllHba

$luns = Get-VMHost $VMHost | Get-ScsiLun | Where { $_.Vendor -eq "MSFT"}

ForEach ($lun in $luns){

$i=$i+1

Write-Host "The disk ID is" $lun

New-Datastore -VMHost $VMHost -Name iSCSI_LUN$i –Path $lun.CanonicalName -Vmfs -Confirm:$false

}

}

It's far from pretty, but it works.  As the hosts are in a cluster, when it tries to add the datastore on the second host it throws an error.  Working on that now.

-Mark

Reply
0 Kudos
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Okay, got the error checking working:

ForEach ($VMHost in $esxi_array){

$i = 0

Get-VMHostStorage $VMHost -RescanAllHba -Rescanvmfs

$datastores = Get-Datastore -VMHost $VMHost

if (-Not ($datastores -like "iSCSI*")){

ForEach ($lun in $luns){

$i=$i+1

$luns = Get-VMHost $VMHost | Get-ScsiLun | Where { $_.Vendor -eq "MSFT"}

New-Datastore -VMHost $VMHost -Name iSCSI_LUN$i –Path $lun.CanonicalName -Vmfs -Confirm:$false

}

}

}

It checks to see if any datastores exist named "iSCSI*" and if not, creates them.  If it finds any (say on the next host) then it skips creation and moves along.

Only slightly uglier than my first attempt.  Maybe LucD can offer a prettier version? 🙂

-Mark

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Isn't that because these datastores are automatically discovered on the other nodes in the cluster ?


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

Reply
0 Kudos
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Yep... exactly.

I always knew that would happen.  I could of course target just the host... but I don't like doing that.

-Mark

Reply
0 Kudos