SDvSphereAPI.psm1

Function Get-SDvSPhereAPISession {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [String]
        $Server,
        [Parameter(Mandatory = $true)]
        [System.StringComparison]
        $User,
        # Service Account Username for vCenter
        [Parameter(Mandatory = $true)]
        [secureString]
        $Password
    )
    # Create vSphere Server Configuration with the provided Credentials.
    $serverConfiguration = New-vSphereServerConfiguration -Server $Server -User $User -Password $Password
    # Creates a Session with the vSphere API.
    $apiSession = Invoke-CreateSession -WithHttpInfo
    # Set the API Key in the vSphere Server Configuration, received with the API Session.
    $serverConfiguration = $serverConfiguration | Set-vSphereServerConfigurationApiKey -SessionResponse $apiSession
}
Function New-SDNVMeDrive {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [String]
        $vmName,
        # Size in GB
        [Parameter(Mandatory = $true)]
        [int64]
        $Size
    )

    #get the VM ID from vmName
    $vmID = Invoke-ListVM | where-object -property Name -eq $vmName | select-object -ExpandProperty vm

    #Get current VM Disks
    $disks = Invoke-ListVmHardwareDisk -Vm $vmID | ForEach-Object -Process { Invoke-GetVmDiskHardware -Vm $vmID -disk $_.disk }
    [int64]$unit = $null

    foreach ($disk in $disks) {
        $unit = $disk.nvme.unit
        $unit ++
    }

    #Convert GBs into Bytes for Command
    $sizeBytes = [int64]$Size * 1GB


    $VmHardwareNvmeAddressSpec = Initialize-VmHardwareNvmeAddressSpec -Bus 0 -Unit $unit
    $VmHardwareDiskVmdkCreateSpec = Initialize-VmHardwareDiskVmdkCreateSpec -Capacity $sizeBytes
    $VmHardwareDiskCreateSpec = Initialize-VmHardwareDiskCreateSpec -Type "NVME" -Nvme $VmHardwareNvmeAddressSpec -NewVmdk $VmHardwareDiskVmdkCreateSpec
    Invoke-CreateVmHardwareDisk -Vm $vmId -VmHardwareDiskCreateSpec $VmHardwareDiskCreateSpec -Verbose
}

#Export Module Members
Export-ModuleMember Get-SDvSPhereAPISession
Export-ModuleMember New-SDNVMeDrive