DataCore ＋ Hyper-VのHCIでiSCSI接続をPowerShellで設定する

[前回](https://qiita.com/tsurun/items/22d42224f94e42eff250)の続きです。

この構成だとiSCSIの接続時にソースIPを指定して接続する必要があります。
GUIだと意外と手間だったのでPowerShellでできるようにします。

参考URL
https://docs.microsoft.com/ja-jp/previous-versions/windows/powershell-scripting/hh826104(v=wps.620)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/115778/34e25d86-9785-307d-3e2d-be4e50d74657.png)

##フロントエンドの接続設定

[こっちの投稿](https://qiita.com/tsurun/items/c0e3fea779e6cdbae699)で作ったスクリプトを活用します。

```
#必要な変数
$BEIP1 = "0.0.0.0"
$BEIP2 = "0.0.0.0"

###前回作ったスクリプト
Import-Module 'C:\Program Files\DataCore\SANsymphony\DataCore.Executive.Cmdlets.dll' -WarningAction silentlyContinue
Connect-DcsServer
$iSCSIActivePorts = @()
$iSCSIPorts = Get-DcsPort -Machine (hostname) -Type "iSCSI"|sort Caption

$iSCSIPorts | Where-Object {
    If ($_.ServerPortProperties.Active -eq $true){
        $Server = ($_.HostId | Get-DcsServer).Caption
        $Name=$_.caption
        $Role=$_.Serverportproperties.Role
        $Iqn=$_.PortName
        $Ip = ($_.serverportproperties.iScsiPortalsConfig|select address).Address.ToString().Replace(':', '')
        $Mac=$_.PhysicalName.Substring(4,17).Replace("-",":")

        $iSCSIActivePorts += [pscustomobject]@{Name=$Name; Server = $Server; Role=$Role; IQN=$Iqn; IP=$Ip; Mac=$Mac}
    }
}

$iSCSIActivePorts |
#パス1
New-IscsiTargetPortal -TargetPortalAddress $FE1IP -InitiatorPortalAddress $BE1IP
Connect-IscsiTarget -TargetPortalAddress $FE1IP -InitiatorPortalAddress "$BE1IP -NodeAddress $FE1IQN
#パス2
New-IscsiTargetPortal -TargetPortalAddress $Server2-FE1IP -InitiatorPortalAddress $BE1IP
Connect-IscsiTarget -TargetPortalAddress $Server2-FE1IP -InitiatorPortalAddress "$BE1IP -NodeAddress $Server2-FE1IQN
#パス3
New-IscsiTargetPortal -TargetPortalAddress $FE2IP -InitiatorPortalAddress $BE2IP
Connect-IscsiTarget -TargetPortalAddress $FE2IP -InitiatorPortalAddress "$BE2IP -NodeAddress $FE2IQN
#パス4
New-IscsiTargetPortal -TargetPortalAddress $Server2-FE2IP -InitiatorPortalAddress $BE2IP
Connect-IscsiTarget -TargetPortalAddress $Server2-FE2IP -InitiatorPortalAddress "$BE1IP -NodeAddress $Server2-FE2IQN
#確認
Get-IscsiConnection
```

##ミラーパスの接続
ちゃんとインストーラーに従っていればここは本来は不要。

```
#ここではサーバーグループのもう一台のサーバーのミラーポート
$MR1IP = "0.0.0.0"
$MR1IQN = "iqn.2000-08.com.datacore:computername2-mrX"
$MR2IP = "0.0.0.0"
$MR2IQN = "iqn.2000-08.com.datacore:computername2-mrY"


#パス1
New-IscsiTargetPortal -TargetPortalAddress $MR1IP
Connect-IscsiTarget -NodeAddress $MR1IQN -TargetPortalAddress $MR1IP 
#パス2
New-IscsiTargetPortal -TargetPortalAddress $MR2IP
Connect-IscsiTarget -NodeAddress $MR2IQN -TargetPortalAddress $MR2IP
#確認
Get-IscsiConnection

```


編集中

```
#必要な変数
$FE1IP = "192.168.1.11"
$BE1IP = "192.168.1.21" #FEと同一セグメントであること
$MR1IP = "192.168.2.11"

$FE2IP = "192.168.3.11"
$BE2IP = "192.168.3.21"  #FEと同一セグメントであること
$MR2IP = "192.168.4.11"

$FE1IQN = "iqn.2000-08.com.datacore:dcs1-fe1"
$FE2IQN = "iqn.2000-08.com.datacore:dcs1-fe2"

$Server2FE1IP = "192.168.1.12"
$Server2FE2IP = "192.168.3.12"
$Server2FE1IQN = "iqn.2000-08.com.datacore:dcs2-fe1"
$Server2FE2IQN = "iqn.2000-08.com.datacore:dcs2-fe2"


#パス1
New-IscsiTargetPortal -TargetPortalAddress $FE1IP -InitiatorPortalAddress $BE1IP
Connect-IscsiTarget -TargetPortalAddress $FE1IP -InitiatorPortalAddress $BE1IP -NodeAddress $FE1IQN

#パス2
New-IscsiTargetPortal -TargetPortalAddress $Server2FE1IP -InitiatorPortalAddress $BE1IP
Connect-IscsiTarget -TargetPortalAddress $Server2FE1IP -InitiatorPortalAddress $BE1IP -NodeAddress $Server2FE1IQN

#パス3
New-IscsiTargetPortal -TargetPortalAddress $FE2IP -InitiatorPortalAddress $BE2IP
Connect-IscsiTarget -TargetPortalAddress $FE2IP -InitiatorPortalAddress $BE2IP -NodeAddress $FE2IQN

#パス4
New-IscsiTargetPortal -TargetPortalAddress $Server2FE2IP -InitiatorPortalAddress $BE2IP
Connect-IscsiTarget -TargetPortalAddress $Server2FE2IP -InitiatorPortalAddress $BE2IP -NodeAddress $Server2FE2IQN

#確認
Get-IscsiConnection

```