##/**************************************************************************************
##Author                :      Guravareddy.T
##Purpose               :      O365 Modules Connectivity Status and Time Taken To Connect
##Created               :      11/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]
$prxycdls=($TestInputs[2]).ToString().Split("#")
$proxyUsr=$prxycdls[0]
$proxyPass=Eg-O365Dcr -EncStr $prxycdls[1]
$proxyserverip=$TestInputs[3]
$overrideUri=$TestInputs[4]
$Password=$Password.Replace("'","")
$proxyPass=$proxyPass.Replace("'","")
$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $password -asplaintext -force)
Write-Host "-----------Skype----------"
    try{
    $d1=Get-Date
    Import-Module SkypeOnlineConnector
	$sessionOption = New-PSSessionOption -SkipRevocationCheck
	if($proxyUsr -ne 'none' -and $proxyPass -ne 'none'){
	    $proxyCred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $proxyUsr, $(convertto-securestring $proxyPass -asplaintext -force)
	    $sessionOption = New-PSSessionOption -SkipRevocationCheck -ProxyAccessType WinHttpConfig -ProxyAuthentication basic -ProxyCredential $proxyCred  
	    #[System.Net.WebRequest]::DefaultWebProxy.Credentials = $proxyCred    
	    #$prx=(netsh winhttp show proxy)[3].Split(":")
	    #$proxyserverip=($prx[1]+':'+$prx[2]).Trim()
	    $proxyserver='http://'+$proxyserverip
	    [system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy($proxyserver)
	    [system.net.webrequest]::defaultwebproxy.credentials =$proxyCred   #[System.Net.CredentialCache]::DefaultNetworkCredentials
	    [system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true
	}
    if($overrideUri -ne 'none'){
        $sfboSession = New-CsOnlineSession -Credential $credential -SessionOption $sessionOption –OverridePowershellUri $overrideUri
    }
    else{ $sfboSession = New-CsOnlineSession -Credential $credential -SessionOption $sessionOption }
    Import-PSSession $sfboSession
    $d2=Get-Date
    $diff=New-TimeSpan -Start $d1 -End $d2
    if($sfboSession.Availability -eq 'Available' -and $sfboSession.State -eq 'Opened'){
        Write-Host 'Skype#Success' $diff.TotalSeconds
    }else{
        Write-Host 'Skype#Failed' $diff.TotalSeconds
   }

  }catch [Exception]{
    Write-Host 'Skype#Failed' $diff.TotalSeconds
   }
    Remove-PSSession $sfboSession


