function Import-ITDVMwareSRMExport {
    [CmdletBinding()]
    Param
    (
        [string]
        $Path
    )

    begin {
        
    }

    process {
        [xml]$Global:SrmXml = Get-Content -Path $Path
    }

    end {

    }
}

function Get-ITDVMwareSRMDatacenter {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]
        [string[]]
        $Id,

        [Parameter(ParameterSetName = 'Name')]
        [string[]]
        $Name
    )
    
    begin {
        
    }
    
    process {
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                ForEach ($i in $Id) {
                    ((($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter | Where-Object id -EQ $i)
                }
            }
            'Name' {
                ForEach ($n in $Name) {
                    ((($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter | Where-Object name -Like "*$n*")
                }
            }
            'Default' {
                ((($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter )
            }
        }
    }
    
    end {
        
    }
}
function Get-ITDVMwareSRMVMFolder {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]
        [string[]]
        $Id,

        [Parameter(ParameterSetName = 'Name')]
        [string[]]
        $Name
    )
    
    begin {
        
    }
    
    process {
        $VMInventoryFolders = (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.vmFolder.InventoryFolder + (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.vmFolder.InventoryFolder.InventoryFolder
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                ForEach ($i in $Id) {
                    $VMInventoryFolders | Where-Object id -Like "*$i*"
                }
            }
            'Name' {
                ForEach ($n in $Name) {
                    $VMInventoryFolders | Where-Object name -Like "*$n*"
                }
            }
            'Default' {
                $VMInventoryFolders
            }
        }        
    }
    
    end {
    }
}

function Get-ITDVMwareSRMHostFolder {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]
        [string[]]
        $Id,

        [Parameter(ParameterSetName = 'Name')]
        [string[]]
        $Name
    )
    
    begin {
        
    }
    
    process {
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                Foreach ($i in $Id) {
                    (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder | Where-Object id -EQ $i
                }
            }
            'Name' {
                Foreach ($i in $Id) {
                    (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder | Where-Object name -Like "*$n*"
                }
        
            }
            'Default' {
                (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder
            }
        }
    
        
    }
    end {
        
    }
}
function Get-ITDVMwareSRMComputeResource {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]    
        [string[]]
        $Id,

        [Parameter(ParameterSetName = 'ResPoolId')]    
        [string[]]
        $ResPoolId,

        [Parameter(ParameterSetName = 'Name')]
        [string[]]
        $Name
    )
    
    begin {
        
    }
    
    process {
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                ForEach ($i in $Id) {
                    (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder.ComputeResource | Where-Object { $_.id -like "*$i*" }
                }
            }
            'ResPoolId' {
                ForEach ($i in $ResPoolId) {
                    (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder.ComputeResource | Where-Object { $_.resourcepool.id -like "*$i*" }
                }
            }
            'Name' {
                If ($Name) {
                    ForEach ($n in $Name) {
                        (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder.ComputeResource | Where-Object { $_.name -like "*$n*" }
                    }
                }
                Else {
                    (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder.ComputeResource
                }
            }
            'Default' {
                (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder.ComputeResource
            }
        }
    }
    
    end {
        
    }
}

function Get-ITDVMwareSRMVMHost {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]
        [string[]]
        $Id,

        [Parameter(ParameterSetName = 'Name')]
        [string[]]
        $Name
    )

    begin {
    
    }

    process {
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                ForEach ($i in $Id) {
                    (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder.ComputeResource.hosts | Where-Object { $_.id -like "*$i*" }
                }
            }
            'Name' {
                ForEach ($n in $Name) {
                    (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder.ComputeResource.hosts | Where-Object { $_.name -like "*$n*" }
                }
            }
            'Default' {
                (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.hostFolder.ComputeResource.hosts
            }
        }

    }

    end {
    
    }
}

function Get-ITDVMwareSRMDatastore {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]
        [string[]]
        $Id,

        [Parameter(ParameterSetName = 'Name')]
        [string[]]
        $Name
    )
    
    begin {
        
    }
    
    process {
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                ForEach ($i in $Id) {
                    (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.datastore | Where-Object id -Like "*$i*"
                }                
            }
            'Name' {
                ForEach ($n in $Name) {
                    (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.datastore | Where-Object { $_.name -like "*$n*" }
                }
            }
            'Default' {
                (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.datastore
            }
        }
    }
    
    end {
        
    }
}

function Get-ITDVMwareSRMNetwork {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]
        [string[]]
        $Id,

        [Parameter(ParameterSetName = 'Name')]
        [string[]]
        $Name
    )
    
    begin {
        
    }
    
    process {
        $dvportgrouplist = (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.NetworkFolder.InventoryFolder.DistributedVirtualPortgroup
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                ForEach ($i in $Id) {
                    #(($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.NetworkFolder.InventoryFolder.DistributedVirtualPortgroup | Where-Object { $_.id -eq "*$i*" }
                    $dvportgrouplist | where-object id -eq $i
                }
            }
            'Name' {
                ForEach ($n in $Name) {
                    #(($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.NetworkFolder.InventoryFolder.DistributedVirtualPortgroup | Where-Object { $_.name -eq "*$n*" }
                    $dvportgrouplist | where-object name -eq $n
                }
            }
            'Default' {
                $dvportgrouplist
            }
        }
    }
    end {
        
    }
}

function Get-ITDVMwareSRMVM {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]
        [string[]]
        $Id,

        [Parameter(ParameterSetName = 'Name')]
        [string[]]
        $Name
    )
    
    begin {
        
    }
    
    process {
        $VMInventory = (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.vmFolder.InventoryFolder.VirtualMachine + (($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ Inventory).Attributes.InventoryTree).root.datacenter.vmFolder.InventoryFolder.InventoryFolder.VirtualMachine
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                ForEach ($i in $Id) {
                    $VMInventory | Where-Object { $_.id -like "*$i*" }
                }
            }
            'Name' {
                ForEach ($n in $Name) {
                    $VMInventory | Where-Object { $_.name -like "*$n*" }
                }
            }
            <#'Default' {
                $VMInventory
            }#>
        }
    }
    
    end {
    
    }
}

function Get-ITDVMwareSRMProtectionGroup {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]
        [string[]]
        $Id,

        [Parameter(ParameterSetName = 'Name')]
        [string[]]
        $Name,

        [switch]
        $Detailed,
        
        [ValidateScript( { $Detailed -eq $true })]
        [switch]
        $Formatted
    )
    
    begin {
        
    }
    
    process {
        $ProtectionGroups = ($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ "ProtectionGroups").Attributes.Folder.vmProtectionGroup
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                $ProtectionGroupsSearch = $Id | ForEach-Object {
                    $ProtectionGroups | Where-Object Id -Like "*$_*"
                }        
            }
            'Name' {
                $ProtectionGroupsSearch = $Name | ForEach-Object {
                    $ProtectionGroups | Where-Object Name -Like "*$_*"
                }
            }
            'Default' {
                $ProtectionGroupsSearch = $ProtectionGroups
            }
        }

        $AllVrDatastores = Get-ChildItem -Path "vmstores:\itdvmvc2.nd.gov@443\Secondary Datacenter\" | Where-Object Name -Like "*_VR*"
        $VrFolders = $AllVrDatastores | Get-ChildItem | Where-Object ItemType -EQ "Folder"

        If ($Detailed) {
            $DetailedResult = [System.Collections.ArrayList]@()
            ForEach ($ProtectionGroup in $ProtectionGroupsSearch) {
                Write-Warning -Message ("Start Protection Group Loop for " + $ProtectionGroup.Name.TrimStart().TrimEnd() )
                $AbrDatastores = $null
                If ($ProtectionGroup.datastores) {
                    $AbrDatastores = Get-ITDVMwareSRMDatastore -Id ($ProtectionGroup.datastores | select -Unique).TrimStart().TrimEnd()
                }
                Else {
                    $AbrDatastores = $null
                }

                $VMs = (Get-ITDVMwareSRMVM -Id ($ProtectionGroup.ProtectionProperties.id)).Name
                $VrDatastores = [System.Collections.ArrayList]@()
                ForEach ($VM in $VMs) {
                    $null = $VRDatastores.add( ($VRFolders | Where-Object Name -Like "$VM*").Datastore)
                }
                $VrDatastores = $VRDatastores | Select-Object -Unique Name
                
                
                $PGobj = [PSCustomObject]@{
                    Name            = $ProtectionGroup.Name.TrimStart().TrimEnd();
                    ReplicationType = $ProtectionGroup.ReplicationProviderType.TrimStart().TrimEnd();
                    ArrayPairKey    = ( $ProtectionGroup.arrayPairKey | ForEach-Object { If ($_.GetType().Name -eq 'String') { $_.TrimStart().TrimEnd() } } | Select-Object -Unique )
                    Folder          = ( Get-ITDVMwareSRMVMFolder -Id ($ProtectionGroup.ProtectionProperties.folder | select -Unique).TrimStart().TrimEnd() ).Name.TrimStart().TrimEnd();
                    ResourceGroup   = ( Get-ITDVMwareSRMComputeResource -ResPoolId ($ProtectionGroup.ProtectionProperties.resourceGroup | select -Unique).TrimStart().TrimEnd() ).Name.TrimStart().TrimEnd();
                    Network         = ( Get-ITDVMwareSRMNetwork -Id ($ProtectionGroup.ProtectionProperties.RecoveryLocationSettings.DeviceInfo.NetworkDeviceBacking.network | select -Unique).TrimStart().TrimEnd() ).Name.TrimStart().TrimEnd();
                    AbrDatastores   = If ($AbrDatastores -ne $null) { ($AbrDatastores.name.TrimStart().TrimEnd() ) };
                    VrDatastores    = $VrDatastores.Name | Select-Object -Unique
                    VM              = ( Get-ITDVMwareSRMVM -Id ($ProtectionGroup.ProtectionProperties.id.TrimStart().TrimEnd()) ).Name.TrimStart().TrimEnd();
                }
                $null = $DetailedResult.add($PGobj)
            }

            If ($Formatted) {
                $FormattedResult = $DetailedResult | select Name, ReplicationType, ArrayPairKey, `
                @{n = 'Folder'; e = { ($_.Folder | Sort-Object | Out-String).TrimEnd() } }, `
                @{n = "ResourceGroup"; e = { ($_.ResourceGroup | Sort-Object | Out-String).TrimEnd() } }, `
                @{n = "Network"; e = { ($_.Network | Sort-Object | Out-String).TrimEnd() } }, `
                @{n = 'AbrDatastores'; e = { ($_.AbrDatastores | Sort-Object | Out-String).TrimEnd() } }, `
                @{n = 'VrDatastores'; e = { ($_.VrDatastores | Sort-Object | Out-String).TrimEnd() } }, `
                @{n = 'VM'; e = { ($_.VM | Sort-Object | Out-String).TrimEnd() } }
            }
        }
    }
    end {
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                If ($Detailed) {
                    If ($Formatted) {
                        Write-Output $FormattedResult    
                    }
                    else {
                        Write-Output $DetailedResult    
                    }
                }
                Else {
                    Write-Output $ProtectionGroupsSearch
                }
            }
            'Name' {
                If ($Detailed) {
                    If ($Formatted) {
                        Write-Output $FormattedResult    
                    }
                    else {
                        Write-Output $DetailedResult
                    }
                }
                Else {
                    Write-Output $ProtectionGroupsSearch
                }
            }
            'Default' {
                If ($Detailed) {
                    If ($Formatted) {
                        Write-Output $FormattedResult    
                    }
                    else {
                        Write-Output $DetailedResult
                    }
                }
                Else {
                    Write-Output $ProtectionGroups
                }
            }
        }
    }
}

function Get-ITDVMwareSRMVMSettings {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ParameterSetName = 'Id')]
        [string[]]
        $Id,

        [CmdletBinding(DefaultParameterSetName = 'Default')]
        [switch]
        $Detailed
    )
    
    begin {
        
    }
    
    process {
        switch ($PsCmdlet.ParameterSetName) {
            'Id' {
                $VMSettingsSearch = $Id | ForEach-Object {
                    ($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ VMSettings).Attributes.VMSettings | Where-Object Id -Like "*$_*"
                }
                
            }
            'Default' {
                $VMSettingsSearch = ($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ VMSettings).Attributes.VMSettings
            }
        }

        If ($Detailed) {
            $DetailedResult = [System.Collections.ArrayList]@()
            ForEach ($VMSetting in $VMSettingsSearch) {
                $VMSettingObj = [PSCustomObject]@{
                    Id                  = $VMSetting.id.TrimStart().TrimEnd();
                    Name                = (Get-ITDVMwareSRMVM -Id $VMSetting.Id.TrimStart().TrimEnd()).Name.TrimStart().TrimEnd();
                    #recoveryPriority = $VMSetting.recoveryPriority;
                    recoveryPriorityGui = switch ($VMSetting.recoveryPriority.TrimStart().TrimEnd()) {
                        25 { 1 }
                        40 { 2 }
                        50 { 3 }
                        60 { 4 }
                        75 { 5 }
                    }
                }
                $null = $DetailedResult.add($VMSettingObj)
            }
        }
        
    }
    
    end {
        If ($Detailed) {
            Write-Output $DetailedResult
        }
        Else {
            $VMSettingsSearch
        }
    }
}

function Get-ITDVMwareSRMRecoveryPlan {
    [CmdletBinding()]
    param (
        [string[]]
        $Name,

        [switch]
        $Formatted
    )
    
    begin {
        
    }
    
    process {
        $RecoveryPlans = ($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ RecoveryPlans).Attributes.Folder.RecoveryPlan
        If ($Name) {
            $RecoveryPlansSearch = $Name | ForEach-Object {
                $RecoveryPlans | Where-Object name -Like "*$_*"
            }
        }
        else {
            $RecoveryPlansSearch = $RecoveryPlans
        }
        $DetailedResult = [System.Collections.ArrayList]@()
        ForEach ($RecoveryPlan in $RecoveryPlansSearch) {
            Write-Warning -Message ("Start Recovery Plan Loop for " + $RecoveryPlan.Name.TrimStart().TrimEnd() )
            $ProtectionGroupDetailed = Get-ITDVMwareSRMProtectionGroup -Id $RecoveryPlan.protectionGroups.TrimStart().TrimEnd() -Detailed

            $AbrDatastores = $ProtectionGroupDetailed.AbrDatastores | Select-Object -Unique | Sort-Object
            $VrDatastores = $ProtectionGroupDetailed.VrDatastores | Select-Object -Unique | Sort-Object
            $Network = $ProtectionGroupDetailed.Network | Select-Object -Unique | Sort-Object

            $VMSettings = Get-ITDVMwareSRMVMSettings -Id (Get-ITDVMwareSRMVM -Name $ProtectionGroupDetailed.VM).Id.TrimStart().TrimEnd() -Detailed
            If ($RecoveryPlan.Callouts) {
                $Callouts = [System.Collections.ArrayList]@()
                $RecoveryPlan.Callouts.CalloutSpec | ForEach-Object {
                    
                }
            }

            $RPobj = [PSCustomObject]@{
                Name             = $RecoveryPlan.Name.TrimStart().TrimEnd();
                ProtectionGroups = $ProtectionGroupDetailed.Name | Sort-Object;
                Network          = $Network;
                AbrDatastores    = $AbrDatastores;
                VrDatastores     = $VrDatastores;
                BeforePriority1  = ($RecoveryPlan.Callouts.CallOutSpec | Where-Object position -EQ 'beforeRecoverPriority1Vms').RecoveryPrompt | select promptText, Description
                Priority1VMs     = ($VMSettings | Where-Object recoveryPriorityGui -EQ 1).Name;
                BeforePriority2  = ($RecoveryPlan.Callouts.CallOutSpec | Where-Object position -EQ 'beforeRecoverPriority2Vms').RecoveryPrompt | select promptText, Description
                Priority2VMs     = ($VMSettings | Where-Object recoveryPriorityGui -EQ 2).Name;
                BeforePriority3  = ($RecoveryPlan.Callouts.CallOutSpec | Where-Object position -EQ 'beforeRecoverPriority3Vms').RecoveryPrompt | select promptText, Description
                Priority3VMs     = ($VMSettings | Where-Object recoveryPriorityGui -EQ 3).Name;
                BeforePriority4  = ($RecoveryPlan.Callouts.CallOutSpec | Where-Object position -EQ 'beforeRecoverPriority4Vms').RecoveryPrompt | select promptText, Description
                Priority4VMs     = ($VMSettings | Where-Object recoveryPriorityGui -EQ 4).Name;
                BeforePriority5  = ($RecoveryPlan.Callouts.CallOutSpec | Where-Object position -EQ 'beforeRecoverPriority5Vms').RecoveryPrompt | select promptText, Description
                Priority5VMs     = ($VMSettings | Where-Object recoveryPriorityGui -EQ 5).Name;
            }

            $null = $DetailedResult.Add($RPobj)
        }

        If ($Formatted) {
            $FormattedResult = $DetailedResult | select Name, `
            @{n = 'ProtectionGroups'; e = { ($_.ProtectionGroups | Sort-Object | Out-String).TrimEnd() } }, `
            @{n = 'Network'; e = { ($_.Network | Sort-Object | Out-String).TrimEnd() } }, `
            @{n = 'AbrDatastores'; e = { ($_.AbrDatastores | Sort-Object | Out-String).TrimEnd() } }, `
            @{n = 'VrDatastores'; e = { ($_.VrDatastores | Sort-Object | Out-String).TrimEnd() } }, `
            @{n = 'BeforePriority1'; e = { $_.BeforePriority1 | Out-String } }, `
            @{n = 'Priority1VMs'; e = { ($_.Priority1VMs | Sort-Object | Out-String).TrimEnd() } }, `
            @{n = 'BeforePriority2'; e = { $_.BeforePriority2 | Out-String } }, `
            @{n = 'Priority2VMs'; e = { ($_.Priority2VMs | Sort-Object | Out-String).TrimEnd() } }, `
            @{n = 'BeforePriority3'; e = { $_.BeforePriority3 | Out-String } }, `
            @{n = 'Priority3VMs'; e = { ($_.Priority3VMs | Sort-Object | Out-String).TrimEnd() } }, `
            @{n = 'BeforePriority4'; e = { $_.BeforePriority4 | Out-String } }, `
            @{n = 'Priority4VMs'; e = { ($_.Priority4VMs | Sort-Object | Out-String).TrimEnd() } }, `
            @{n = 'BeforePriority5'; e = { $_.BeforePriority5 | Out-String } }, `
            @{n = 'Priority5VMs'; e = { ($_.Priority5VMs | Sort-Object | Out-String).TrimEnd() } }
        }
    }
    
    end {
        If ($Formatted) {
            Write-Output $FormattedResult
        }
        Else {
            Write-Output $DetailedResult
        }
    }
}


function Get-ITDVMwareSRMMasterPlan {
    [CmdletBinding()]
    param (
        
    )
    
    begin {
        $RecoveryPlans = ($Global:SrmXml.configurablesWrapper.data.object | Where-Object Type -EQ RecoveryPlans).Attributes.Folder.RecoveryPlan
    }
    
    process {
        
    }
    
    end {
        
    }
}