Ludicrous09
Enthusiast
Enthusiast

Creating DataStores and converting system.object to system.string

Jump to solution

So I have a bunch of ROBO locations, I am in needing to configure hardware to send out to these locations.  I am connecting directly to the host for provisioning.  connect-vi x.x.x.x

When i do Get-ScsiLun | Select RuntimeName,CanonicalName    It does not return a Runtime name (I am not sure if it is because I am doing it local or it is just the way the Dell VRTX does it)

I am using a piece i found from LucD that will create this property for me, and I have named it RuntimeName1. (as RuntimeName returns nothing)

Here is the code I am using for now..

it looks like it doesn't like that it is a system object and wants a string instead.  I can't figure out how to convert it.

---------------------------------------------------------

Error:

New-Datastore : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not

supported.

At line:32 char:67

----------------------------------------------------------

# Configuration:

$MyHost = "Enter Server Name or IP"

$StoreNumber = Read-Host "Store Number"

$DatastorePrefix = "$StoreNumber-Datastore"

# End Config

New-VIProperty -Name "RuntimeName1" -ObjectType "ScsiLun" -Value {

param($scsilun)

$storDev = $scsilun.VMHost.ExtensionData.Config.StorageDevice

$key = ($storDev.PlugStoreTopology.Device | where {$_.Lun -eq $scsilun.Key}).Key

$stordev.PlugStoreTopology.Path | where {$_.Device -eq $key} | %{

$device = $_

$adapterKey = ($stordev.PlugStoreTopology.Adapter | where {$_.Key -eq $device.Adapter}).Adapter

$adapter = ($stordev.HostBusAdapter | where {$_.Key -eq $adapterKey}).Device

$adapter + ":C" + $device.ChannelNumber + ":T" + $device.TargetNumber + ":L" + $device.LunNumber

}

} -Force

Get-VMHostStorage -RescanAllHba

1..1 | ForEach-Object {

  $DatastoreName = "{0:D3}" -f $_

  $LUNNum = $_ +0

  Write-Host "DS Name:  $DatastorePrefix$DatastoreName     -     LUN: $LUNNum"

  $LUN = Get-ScsiLun | Select RuntimeName1,CanonicalName | Where { $_.RuntimeName1 -like "vmhba2:C2:T*:L0" }

  New-Datastore -Vmfs -Name "$DatastorePrefix$DatastoreName" -Path $LUN.CanonicalName -FileSystemVersion 5

}

0 Kudos
1 Solution

Accepted Solutions
Ludicrous09
Enthusiast
Enthusiast

$global:DefaultVIServers  returns that i am connected directly to the one esxi host.


You are using my New-VIProperty, and you have to change the name, since the Get-ScsiLun also has a RuntimeName property (that was added after I wrote that New-VIProperty).

yes i Renamed your New-VIProperty RuntimeName1 Since RuntimeName already existed



I figured out the problem,  that when i was running the piece below it was returning both drives at the same time so it was really an array of objects so that was the issue.

$LUN = Get-ScsiLun | Select RuntimeName1,CanonicalName | Where { $_.RuntimeName1 -like "vmhba2:C2:T*:L0" }


The other issue i was having is that the :L0 number is the same on both Devices the only object that changes is the T0.  I have since updated the code and it is now working like this, and names the Created Datastore as expected.  In the code below it is only creating two datastores, but that is all i need for now.


IE


vmhba2:C2:T0:L0

vmhba2:C2:T1:L0


# Configuration:

$MyHost = "Enter Server Name or IP"

$StoreNumber = Read-Host "Store Number"

$DatastorePrefix = "$StoreNumber-Datastore"

# End Config

New-VIProperty -Name "RuntimeName1" -ObjectType "ScsiLun" -Value {

param($scsilun)

$storDev = $scsilun.VMHost.ExtensionData.Config.StorageDevice

$key = ($storDev.PlugStoreTopology.Device | where {$_.Lun -eq $scsilun.Key}).Key

$stordev.PlugStoreTopology.Path | where {$_.Device -eq $key} | %{

$device = $_

$adapterKey = ($stordev.PlugStoreTopology.Adapter | where {$_.Key -eq $device.Adapter}).Adapter

$adapter = ($stordev.HostBusAdapter | where {$_.Key -eq $adapterKey}).Device

$adapter + ":C" + $device.ChannelNumber + ":T" + $device.TargetNumber + ":L" + $device.LunNumber

}

} -Force

Get-VMHostStorage -RescanAllHba

1..2 | ForEach-Object {

  $DatastoreName = "{0:D3}" -f $_

  $LUNNum = $_ -1

  $LUNName = "vmhba2:C2:T"+$LunNum+":L0"

    Write-Host "LunName = $LUNName"

  Write-Host "DS Name:  $DatastorePrefix$DatastoreName     -     LUN: $LUNNum"

  $LUN = Get-ScsiLun | Select RuntimeName1,CanonicalName | Where { $_.RuntimeName1 -like "$LUNName" }

  New-Datastore -Vmfs -Name "$DatastorePrefix$DatastoreName" -Path $LUN.CanonicalName -FileSystemVersion 5

}

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

Since vSphere 6, the RuntimeName property that is returned by the Get-ScsiLun cmdlet, doesn't work anymore.

See also Script to get Host SCSI ID ( LUN ID)

You are using my New-VIProperty, and you have to change the name, since the Get-ScsiLun also has a RuntimeName property (that was added after I wrote that New-VIProperty).

The error you are seeing is in fact saying that you are probably providing an array of strings instead of a single string.

Could it be that you have multiple connections open (have a look ar $global:DefaultVIServers)?


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

0 Kudos
Ludicrous09
Enthusiast
Enthusiast

$global:DefaultVIServers  returns that i am connected directly to the one esxi host.


You are using my New-VIProperty, and you have to change the name, since the Get-ScsiLun also has a RuntimeName property (that was added after I wrote that New-VIProperty).

yes i Renamed your New-VIProperty RuntimeName1 Since RuntimeName already existed



I figured out the problem,  that when i was running the piece below it was returning both drives at the same time so it was really an array of objects so that was the issue.

$LUN = Get-ScsiLun | Select RuntimeName1,CanonicalName | Where { $_.RuntimeName1 -like "vmhba2:C2:T*:L0" }


The other issue i was having is that the :L0 number is the same on both Devices the only object that changes is the T0.  I have since updated the code and it is now working like this, and names the Created Datastore as expected.  In the code below it is only creating two datastores, but that is all i need for now.


IE


vmhba2:C2:T0:L0

vmhba2:C2:T1:L0


# Configuration:

$MyHost = "Enter Server Name or IP"

$StoreNumber = Read-Host "Store Number"

$DatastorePrefix = "$StoreNumber-Datastore"

# End Config

New-VIProperty -Name "RuntimeName1" -ObjectType "ScsiLun" -Value {

param($scsilun)

$storDev = $scsilun.VMHost.ExtensionData.Config.StorageDevice

$key = ($storDev.PlugStoreTopology.Device | where {$_.Lun -eq $scsilun.Key}).Key

$stordev.PlugStoreTopology.Path | where {$_.Device -eq $key} | %{

$device = $_

$adapterKey = ($stordev.PlugStoreTopology.Adapter | where {$_.Key -eq $device.Adapter}).Adapter

$adapter = ($stordev.HostBusAdapter | where {$_.Key -eq $adapterKey}).Device

$adapter + ":C" + $device.ChannelNumber + ":T" + $device.TargetNumber + ":L" + $device.LunNumber

}

} -Force

Get-VMHostStorage -RescanAllHba

1..2 | ForEach-Object {

  $DatastoreName = "{0:D3}" -f $_

  $LUNNum = $_ -1

  $LUNName = "vmhba2:C2:T"+$LunNum+":L0"

    Write-Host "LunName = $LUNName"

  Write-Host "DS Name:  $DatastorePrefix$DatastoreName     -     LUN: $LUNNum"

  $LUN = Get-ScsiLun | Select RuntimeName1,CanonicalName | Where { $_.RuntimeName1 -like "$LUNName" }

  New-Datastore -Vmfs -Name "$DatastorePrefix$DatastoreName" -Path $LUN.CanonicalName -FileSystemVersion 5

}

0 Kudos