Wednesday, July 6, 2016

New PowerShell function to setup a iSCSI target on a NETAPP and mount it on Windows

I don't often use my NETAPP controllers as iSCSI targets. Most of the time I just export NFS (especially to host VMWare datastores) or CIFS and that suffices my needs. Sometimes however I am asked to mount on Windows Servers disks from the NETAPP directly on startup and not just as simple shares mounted with logon scripts or whatever.
So I decided to write a pretty do-it-all PowerShell function that does all the work of configuring the NETAPP and mounting the disk via iSCSI on my server.
If I had to do this via the OnCommand GUI, I should first setup an aggregate, then a volume, then a LUN, configure the iGroup and only then move to my Windows server and manually bind the initiator with the target.
Fortunately back in 2010 NETAPP released a module (inside the NetApp PowerShell Toolkit) to do all this stuff and more. Today you need to grab at least version 1.5 to get all the cmdlets I used in my function. Personally I have the last version (which is 4.2) which can bo downloaded here.

Concerning the requirements, the script must run on the Windows Server that acts as initiator. On that server you have to be running at least version 3.0 of Windows PowerShell and to have the DataONTAP module installed.
The Windows Server that acts as initiator will run my function that
  • configures the storage controller
  • setup the target
  • setup the initiator
  • do the mapping with the LUN in a iGroup.
Once the function has finished configuring the iSCSI disk, you just have to initialize it in Disk Management, format it and assign a letter. I already showed you how to do this in PowerShell, just search my blog.

Enough said: here's the function Mount-NAiSCSIDisk.

Just one last quick note: I am going to work on this function to improve it, so please, post suggestions or ideas in the comments below! And, for sure, share!

#Requires -Version 3.0 -Modules DataONTAP

function Mount-NAiSCSIDisk
            Setup and mounts a NetApp iSCSI LUN to a Windows Server
            Setup an aggregate, a volume, a LUN, an iGroup then mounts it as a NetApp iSCSI target to a Windows Server
        .PARAMETER NaController
            Name of the NetApp controller

        .PARAMETER Aggregate
            Name of the aggregate

        .PARAMETER DiskCount
            Number of disks in the aggregate
        .PARAMETER Volume
            Name of the volume

        .PARAMETER VolumeSize
            Size of the volume

        .PARAMETER SnapshotReserveSize
            Size of the snapshot reserve

        .PARAMETER LUNPath
            Path of the LUN

        .PARAMETER LUNType
            Type of the LUN

        .PARAMETER LUNSize
            Size of the LUN

        .PARAMETER Igroup
            Nq;e of the Igroup

        .PARAMETER IgroupProtocol
            Protocl for the Igroup
        .PARAMETER IgroupType
            Type of the Igroup

            Mount-NAiSCSIDisk -NaController netapp1 -Igroup igroup1 -IgroupProtocol iscsi -IgroupType windows
            Mount-NAiSCSIDisk -NaController netapp1 -Aggregate aggr1 -DiskCount 32 -Volume vol1 -VolumeSize 10g -SnapshotReserveSize 0 -LUNPath /vol/NA1/iscsivol1 -LUNSize 1g -LUNType windows -Igroup igroup1 -IgroupProtocol iscsi -IgroupType windows

   - Carlo MANCINI

    write-verbose 'Started'
    $Error = $False

    $info = @()

    $infolastonline = @()

    Write-Verbose 'Connect to NaController'

    try {
        Connect-NaController $NaController -ErrorAction Stop


    catch {

        Write-Warning 'Unable to connect to the controller'


    write-verbose 'Starting the configuration on the netapp controlller'
    if(Get-NaAggr $Aggregate) {

        Write-Verbose 'Creating the aggregate'
        New-NaAggr $Aggregate -Use64Bit -DiskCount $DiskCount


    else {

        Write-Verbose 'Aggregate already existing'


    if(Get-NaVol $Volume) {

        Write-Verbose 'Creating the volume'

        New-NaVol $Volume -Aggregate $Aggregate $VolumeSize

        Write-Verbose 'Setting the snapshot reserve'

        Set-NaSnapshotReserve $Volume $SnapshotReserveSize


    else {

        Write-Verbose 'Volume already existing'


    if(Get-NaLun $LUNPath) {
        Write-Verbose 'Creating the LUN'
        New-NaLun -Path $LUNPath -Size $LUNSize -Type $LunType


    else {

        Write-Verbose 'LUN already existing'

    Write-Verbose 'Starting the iSCSI configuration'
    Write-Verbose 'Adding the storage controller to the iSCSI Initiator target portals'


    Write-Verbose 'Establishing a connection to the target discovered by the iSCSI Initiator'

    Get-NaHostIscsiTarget (Get-NaIscsiNodeName) | Connect-NaHostIscsiTarget

    Write-Verbose 'Creating a new initiator group'

    New-NaIgroup $Igroup $IgroupProtocol $IgroupType

    Write-Verbose 'Adding the initiator the the initiator group'
    Get-NaHostIscsiAdapter | Add-NaIgroupInitiator $Igroup

    Write-Verbose 'Mapping the LUN to the initiators in the initiator group'

    Add-NaLunMap $LUNPath $Igroup


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...