##/******************************************************************************
##Author                :   Vishnu V
##Purpose               :   Display the list of services and their status in O365
##Created               :   12/01/2018
##Modified By		    : 

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$egurkhaPath=(Get-Item Env:EGURKHA_INSTALL_DIR).value.ToString()
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$egEncryPath=$scriptPath+"\EGFileEncryption.psm1"
$egDatnKy=$scriptPath+"\GetDatnKeyFiles.psm1"
Import-Module $egEncryPath,$egDatnKy

clear

$TestInputs=$args
$userName=$TestInputs[0]
$Password=Eg-O365Dcr -EncStr $TestInputs[1]
$proxyUsr=$TestInputs[2]
$proxyPass=Eg-O365Dcr -EncStr $TestInputs[3]
$reptnameProxy=($TestInputs[4]).ToString().Split("#")
$reportingNam=$reptnameProxy[0]
$proxyserverip=$reptnameProxy[1]
$azureVal=Eg-GetAzureEnv -UserName $userName
$azureEnvArr=$azureVal.Split(',')
if($proxyUsr.toString().toLower().Trim() -eq 'none') { $proxyUsr=$proxyUsr.toString().toLower().Trim() }
if($proxyPass.toString().toLower().Trim() -eq 'none') { $proxyPass=$proxyPass.toString().toLower().Trim() }
if($proxyserverip.toString().toLower().Trim().Contains('none')) { $proxyserverip=$proxyserverip.toString().toLower().Trim() }


$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)
if(!$proxyserverip.Contains('none')){

    $proxyserver='http://'+$proxyserverip
    [system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy($proxyserver)
    if($proxyUsr -ne 'none' -and $proxyPass -ne 'none'){
        $proxyCred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $proxyUsr, $(convertto-securestring $proxyPass -asplaintext -force)
        [system.net.webrequest]::defaultwebproxy.credentials =$proxyCred   #[System.Net.CredentialCache]::DefaultNetworkCredentials
        [system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true
    }
 }


Connect-MsolService -Credential $cred -AzureEnvironment $azureEnvArr[0]
$rptPath='O365/'+$reportingNam
$LicensesCnt=0
 
 #------------------------UserlogTest-----------------------------#
    Try{
        $writArr = Eg-WriteFile -ComntRptPath $rptPath -FileName "O365msolUsr" -keyFileName "kO365msolUsr" -EgPath $egurkhaPath
        Get-MsolUser -All | select DisplayName,UserPrincipalName,SignInName,Licenses,IsLicensed,UsageLocation,PasswordNeverExpires,BlockCredential,LastPasswordChangeTimestamp,StrongAuthenticationRequirements| ForEach-Object {$val=$_.Licenses.AccountSkuId -join '#';$MFAstate=$_.StrongAuthenticationRequirements.State -join '#';$_ | Add-Member -MemberType NoteProperty -Name 'AccId' -Value $val -PassThru| Add-Member -MemberType NoteProperty -Name 'MFAStatus' -Value $MFAstate -PassThru}  | Export-Csv $writArr[1]
        $null=Protect-File  $writArr[1] -Algorithm AES -KeyAsPlainText $writArr[0] #-RemoveSource
    }Catch{Write-Host $_.Exception.Message}

    Try{
        $delUsrWArr = Eg-WriteFile -ComntRptPath $rptPath -FileName "O365msolDelUsr" -keyFileName "kO365msolDelUsr" -EgPath $egurkhaPath
        Get-MsolUser -All -ReturnDeletedUsers| select UserPrincipalName,SignInName,Licenses,IsLicensed,UsageLocation | ForEach-Object {$val=$_.Licenses.AccountSkuId -join '#';$_ | Add-Member -MemberType NoteProperty -Name 'AccId' -Value $val -PassThru} |Export-Csv $delUsrWArr[1]
        $null=Protect-File  $delUsrWArr[1] -Algorithm AES -KeyAsPlainText $delUsrWArr[0] -RemoveSource
    }Catch{}

    Try{
        $dmUsrWArr = Eg-WriteFile -ComntRptPath $rptPath -FileName "O365msolDomUsr" -keyFileName "kO365msolDomUsr" -EgPath $egurkhaPath
        Get-MSOLDomain | select Name |Export-Csv $dmUsrWArr[1]
        $null=Protect-File  $dmUsrWArr[1] -Algorithm AES -KeyAsPlainText $dmUsrWArr[0] -RemoveSource
    }Catch{}

 #----------------------------------------------------------------#


 #------------------------Service Provisioning-----------------------------#
  try{
        $writmsolLUsr = Eg-WriteFile -ComntRptPath $rptPath -FileName "O365PSmsolAccSku" -keyFileName "kO365PSmsolAccSku" -EgPath $egurkhaPath
        Get-MsolAccountSku |Select-Object AccountSKUID,ServiceStatus| ForEach-Object {$val=$_.ServiceStatus.ServicePlan.ServiceName -join '~';$_ | Add-Member -MemberType NoteProperty -Name 'Status' -Value $val -PassThru} | Export-Csv -Path $writmsolLUsr[1]
        $null=Protect-File  $writmsolLUsr[1] -Algorithm AES -KeyAsPlainText $writmsolLUsr[0] -RemoveSource
    }Catch{Write-Host $_.Exception.Message}

    Try{
        $writmsolLUsr1 = Eg-WriteFile -ComntRptPath $rptPath -FileName "O365PSmsolUsr" -keyFileName "kO365PSmsolUsr" -EgPath $egurkhaPath


        $sss=Get-MsolUser -All |select UserPrincipalName,Licenses
        
        foreach($dmn in $sss){
            $usr1=$dmn.UserPrincipalName 
            foreach($dn in $dmn.Licenses){
                $valid=$dn| Select-Object -Property @{Name='UserName';Expression={$dmn.UserPrincipalName }},@{Name='AccId';Expression={$dn.AccountSkuId}},@{Name='ServiceName';Expression={$dn.ServiceStatus.ServicePlan.ServiceName}},@{Name='ProvisioningStatus';Expression={$dn.ServiceStatus.ProvisioningStatus}}
                if($valid -ne $null -and $valid -ne ''){
                    $valid|Export-Csv -Append -LiteralPath $writmsolLUsr1[1] #-Force
                }
            }
        }
	$null=Protect-File  $writmsolLUsr1[1] -Algorithm AES -KeyAsPlainText $writmsolLUsr1[0] -RemoveSource
    }Catch{
	Write-Host $_.Exception.Message
	}
 #--------------------------------------------------------------------------#