JSDR.Setup.psm1

function Set-JSDRConfiguration {
<#
    .NOTES
    ===========================================================================
    Created by: JetStream Software Inc
    ===========================================================================
    .DESCRIPTION
    Sets up context for JSDR.Configuration

    .PARAMETER VC_Address
    vCenter ip/fqdn

    .PARAMETER VC_Credential
    administrator credentials of vCenter

    .EXAMPLE
    Set-JSDRConfiguration -VC_Address "vc.example.com" -VC_Credential <root/PSCredential Object>
#>


    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,
                    HelpMessage="vCenter ip/fqdn")]
        [String]$VC_Address,
        [Parameter(Mandatory=$true,
                    HelpMessage="administrator credentials of vCenter")]
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $VC_Credential
    )
    Begin {
        if ($VC_Credential.UserName.Contains('@')) {
            $sso_user = $VC_Credential.UserName.Split("@")[0]
            $sso_domain = $VC_Credential.UserName.Split("@")[1]
        }
        elseif ($VC_Credential.UserName.Contains('\')) {
            $sso_user = $VC_Credential.UserName.Split("\")[1]
            $sso_domain = $VC_Credential.UserName.Split("\")[0]
        }
        else {
            Write-Error "vCenter username not in valid format. Exiting..." -ErrorAction Stop
        }
        $js_url = "" #to be updated once we have the package url
        $js_upgrade_url = "" #to be updated once we have the package url
    }
    Process {
        Set-PowerCLIConfiguration -InvalidCertificateAction:Ignore -Confirm:$false | Out-Null
        Write-Host "Disconnecting any connected viservers and ssoadmin servers from the powershell session"
        while ($global:DefaultSsoAdminServers) {
            $server = $global:DefaultSsoAdminServers[0].Name
            Write-Host "Disconnect-SsoAdminServer $server"
            Disconnect-SsoAdminServer $global:DefaultSsoAdminServers[0].Name
        }

        while ($global:DefaultVIServers) {
            $server = $global:DefaultVIServers[0].Name
            Write-Host "Disconnect-VIServer $server"
            Disconnect-VIServer $global:DefaultVIServers[0].Name -confirm:$false
        }
        Write-Host "Performing Connect-VIServer on $VC_Address"
        $vi_server = Connect-VIServer -Server $VC_Address -Credential $VC_Credential
        if(!$vi_server) {
            Write-Error "Connect-VIServer failed." -ErrorAction Stop
        }
        Write-Host "Performing Connect-SsoAdminServer on $VC_Address"
        $sso_server = Connect-SsoAdminServer -Server $VC_ADDRESS -User $VC_Credential.Username -Password $VC_Credential.Password -SkipCertificateCheck
        if(!$sso_server) {
            Write-Error "Connect-SsoAdminServer failed." -ErrorAction Stop
        }
        Write-Host "Performing Connect-VcenterServerMOB on $VC_Address"
        $MOB_Connection = Connect-VcenterServerMOB -Server $VC_ADDRESS -Credential $VC_Credential -SkipCertificateCheck
        if(!$MOB_Connection) {
            Write-Error "Connect-VcenterServerMOB failed." -ErrorAction Stop
        }

        $sso_perm = Get-VIPermission -Principal "$sso_domain\$sso_user"
        $sso_role = $sso_perm.Role
        if (!$sso_role -or ($sso_role -ne "Admin")) {
            Write-Error "Could not determine the Role of the vCenter user or the user doesn't have admin privilege. Cannot proceed." -ErrorAction Stop
        }
        else {
            Write-Host "Set-JSDRConfiguration completed successfully."
        }
    }
    End {
        $global:sso_domain = $sso_domain
        $global:VC_ADDRESS = $VC_ADDRESS
        $global:MOB_Connection = $MOB_Connection
        $global:sso_role = $sso_role
        $global:js_url = $js_url
        $global:js_upgrade_url = $js_upgrade_url
    }
}

function Reset-JSDRConfiguration {
<#
    .NOTES
    ===========================================================================
    Created by: JetStream Software Inc
    ===========================================================================
    .DESCRIPTION
     Unsets the properties set by Set-JSDRConfiguration

    .PARAMETER VC_Address
    vCenter ip/fqdn

    .EXAMPLE
    Reset-JSDRConfiguration -VC_Address "vc.example.com"
#>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,
                    HelpMessage="vCenter ip/fqdn")]
        [String]$VC_Address
    )
    Begin {
        Write-Host "Unsetting the properties set by Set-JSDRConfiguration"
        Remove-Variable -Name VC_ADDRESS -Scope Global
        Disconnect-VcenterServerMOB -Server $MOB_Connection
        Disconnect-SsoAdminServer $VC_Address
        Disconnect-VIServer $VC_Address -Confirm:$false
        Remove-Variable -Name MOB_Connection -Scope Global
        Remove-Variable -Name sso_role -Scope Global
        Remove-Variable -Name sso_domain -Scope Global
        Remove-Variable -Name js_url -Scope Global
        Remove-Variable -Name js_upgrade_url -Scope Global
    }
}

function Remove-AVSAttribute {
<#
    .NOTES
    ===========================================================================
    Created by: JetStream Software Inc
    ===========================================================================
    .DESCRIPTION
     Removes AVSAttribute as it is not required for on-prem runs.

    .EXAMPLE
    Remove-AVSAttribute
#>

    [CmdletBinding()]
     param(
        [string]$ModuleName = "JSDR.Configuration"
    )
    $ModulePath = (Get-Module -Name $ModuleName -ListAvailable).ModuleBase
    $FilePath = Get-ChildItem -Path $ModulePath -Filter *.psm1 -Recurse
    Write-Host "Removing AVSAttribute from $FilePath"
    $num_lines_before = (Get-Content $FilePath).Count
    (Get-Content $FilePath) | Where-Object { $_ -notmatch '^\s*\[AVSAttribute' } | Set-Content $FilePath
    $num_lines_after = (Get-Content $FilePath).Count
    if ($num_lines_before -gt $num_lines_after) {
        Write-Host "Successfully removed $($num_lines_before - $num_lines_after) AVSAttribute occurences."
    } else {
        Write-Host "No lines with AVSAttribute found"
    }
    Remove-Module $ModuleName
}