##############################
#1.1 => Add CreationTime for PortInUse FilePath
#1.2 => Show the owning process of the PortInUse file
#1.3 => Add "$ENV:APPDATA\Thomson Reuters\Eikon API Proxy\.portInUse is not found" to output.txt
#1.3 => Collect Floatingtoolbar and SXS logs 
#1.4 => Check "$ENV:APPDATA\Refinitiv\Data API Proxy\.portInUse"  file
#1.5 => Log APIProxy and SXS log files, remove floating tool bar log
#1.6 => Add verifying http://127.0.0.1:$PortInUse/api/handshake
$PortInUse=0
$OutputFile="output.txt"
$Version="1.6"
$EikonLogPath="$ENV:PROGRAMDATA\Thomson Reuters\Eikon Data\Logs\TRD\"
$EikonProcessName="Eikon"
$EikonProcessId=0

"Script Version: $Version" | Tee-Object -FilePath $OutputFile 
'1. List all Eikon processes' | Tee-Object -FilePath $OutputFile -Append
'===========================' | Tee-Object -FilePath $OutputFile  -Append
Get-Process -Name Eikon* | Tee-Object -FilePath $OutputFile  -Append

$EikonProcesses =  Get-Process -name "Eikon*" | Select-Object Name | Get-Unique -AsString

Foreach ($Obj in $EikonProcesses){
	$Process = $Obj."Name"
	$EXEFile = "$Process.exe"
	"Process: $EXEFile" | Tee-Object -FilePath $OutputFile  -Append
		
	Get-WmiObject Win32_Process -Filter "name = '$EXEFile'"  | Select-Object ProcessId, CommandLine | Format-Table -AutoSize -Wrap | Tee-Object -FilePath $OutputFile  -Append

}

$EikonObject = Get-Process -name "$EikonProcessName" | Select-Object Id

If($EikonObject){
	$EikonProcessId = $EikonObject."Id"
}


'' | Tee-Object -FilePath $OutputFile -Append
'2. List all listening ports of Eikon processes' | Tee-Object -FilePath $OutputFile -Append
'==============================================' | Tee-Object -FilePath $OutputFile  -Append
Get-NetTCPConnection -state Listen -OwningProcess (Get-Process -name Eikon*).Id | Select-Object -Property LocalAddress, LocalPort, State, OwningProcess | Tee-Object -FilePath $OutputFile -Append


'' | Tee-Object -FilePath $OutputFile -Append
'3. Check portInUse file' | Tee-Object -FilePath $OutputFile -Append
'=======================' | Tee-Object -FilePath $OutputFile  -Append
If((Test-Path "$ENV:APPDATA\Thomson Reuters\Eikon API Proxy\.portInUse") -eq $True){
	"$ENV:APPDATA\Thomson Reuters\Eikon API Proxy\.portInUse is found" | Tee-Object -FilePath $OutputFile -Append
	Get-Item "$ENV:APPDATA\Thomson Reuters\Eikon API Proxy\.portInUse" | Select-Object -Property CreationTimeUtc, LastAccessTimeUTc, LastWriteTimeUtc | Format-Table -AutoSize -Wrap | Tee-Object -FilePath $OutputFile  -Append
	$PortInUse = Get-Content -Path "$ENV:APPDATA\Thomson Reuters\Eikon API Proxy\.portInUse"
	Get-Content -Path "$ENV:APPDATA\Thomson Reuters\Eikon API Proxy\.portInUse" | Tee-Object -FilePath $OutputFile -Append
}ElseIf((Test-Path "$ENV:APPDATA\Thomson Reuters\Eikon Scripting Proxy\.portInUse") -eq $True){
	"$ENV:APPDATA\Thomson Reuters\Eikon API Proxy\.portInUse is not found" | Tee-Object -FilePath $OutputFile -Append
	"$ENV:APPDATA\Thomson Reuters\Eikon Scripting Proxy\.portInUse is found" | Tee-Object -FilePath $OutputFile -Append
	Get-Item "$ENV:APPDATA\Thomson Reuters\Eikon Scripting Proxy\.portInUse" | Select-Object -Property CreationTimeUtc, LastAccessTimeUTc, LastWriteTimeUtc | Format-Table -AutoSize -Wrap | Tee-Object -FilePath $OutputFile  -Append
	$PortInUse = Get-Content -Path "$ENV:APPDATA\Thomson Reuters\Eikon Scripting Proxy\.portInUse"
	Get-Content -Path "$ENV:APPDATA\Thomson Reuters\Eikon Scripting Proxy\.portInUse" | Tee-Object -FilePath $OutputFile -Append
}ElseIf((Test-Path "$ENV:APPDATA\Refinitiv\Data API Proxy\.portInUse") -eq $True){
	"$ENV:APPDATA\Refinitiv\Data API Proxy\.portInUse is found" | Tee-Object -FilePath $OutputFile -Append
	Get-Item "$ENV:APPDATA\Refinitiv\Data API Proxy\.portInUse" | Select-Object -Property CreationTimeUtc, LastAccessTimeUTc, LastWriteTimeUtc | Format-Table -AutoSize -Wrap | Tee-Object -FilePath $OutputFile  -Append
	$PortInUse = Get-Content -Path "$ENV:APPDATA\Refinitiv\Data API Proxy\.portInUse"
	Get-Content -Path "$ENV:APPDATA\Refinitiv\Data API Proxy\.portInUse" | Tee-Object -FilePath $OutputFile -Append	
}Else{
	"$ENV:APPDATA\Thomson Reuters\Eikon API Proxy\.portInUse is not found" | Tee-Object -FilePath $OutputFile -Append
	"$ENV:APPDATA\Thomson Reuters\Eikon Scripting Proxy\.portInUse is not found" | Tee-Object -FilePath $OutputFile -Append
	"$ENV:APPDATA\Refinitiv\Data API Proxy\.portInUse is not found" | Tee-Object -FilePath $OutputFile -Append
}

If( $PortInUse -ne 0){
	'' | Tee-Object -FilePath $OutputFile -Append
	"4. Verify the connection to TCP Port: $PortInUse" | Tee-Object -FilePath $OutputFile -Append
	'======================================' | Tee-Object -FilePath $OutputFile -Append
	
	try{
		"Check owning process of $PortInUse" | Tee-Object -FilePath $OutputFile -Append
		Get-NetTCPConnection -LocalPort "$PortInUse" | Select-Object -Property LocalAddress, LocalPort, State, OwningProcess | Format-Table -AutoSize -Wrap | Tee-Object -FilePath $OutputFile  -Append
	}catch{
		"Exception: $_" | Tee-Object -FilePath $OutputFile -Append	
	}
	
	try{
		$StatusCode = (Invoke-WebRequest -Uri "http://127.0.0.1:$PortInUse/sxs/v1" -Method GET).StatusCode	
		"GET: http://127.0.0.1:$PortInUse/sxs/v1 => Status Code: $StatusCode" | Tee-Object -FilePath $OutputFile -Append		
	}catch{
		"GET: http://127.0.0.1:$PortInUse/sxs/v1" | Tee-Object -FilePath $OutputFile -Append
		"Exception: $_" | Tee-Object -FilePath $OutputFile -Append	
	}
	
	try{	
		$StatusCode = (Invoke-WebRequest -Uri "http://127.0.0.1:$PortInUse/ping" -Method GET).StatusCode
		"GET: http://127.0.0.1:$PortInUse/ping => Status Code: $StatusCode" | Tee-Object -FilePath $OutputFile -Append	
	}catch{
		"GET: http://127.0.0.1:$PortInUse/ping" | Tee-Object -FilePath $OutputFile -Append
		"Exception: $_" | Tee-Object -FilePath $OutputFile -Append	
	}
	
	try{
		$StatusCode = ( Invoke-WebRequest -Uri "http://127.0.0.1:$PortInUse/api/v1/data" -Method POST).StatusCode
		"POST: http://127.0.0.1:$PortInUse/api/v1/data => Status Code: $StatusCode" | Tee-Object -FilePath $OutputFile -Append
	}catch{
		"POST: http://127.0.0.1:$PortInUse/api/v1/data" | Tee-Object -FilePath $OutputFile -Append
		"Exception: $_" | Tee-Object -FilePath $OutputFile -Append	
	}
	
	try{
		$StatusCode = ( Invoke-WebRequest -Method POST -Body (@{"AppKey"="<appkey>";"AppScope"="trapi";"ApiVersion"="1";"LibraryName"="RDP Python Library";"LibraryVersion"="1.1.7"}|ConvertTo-Json) -Uri http://127.0.0.1:$PortInUse/api/handshake -ContentType application/json).StatusCode
		"POST: http://127.0.0.1:$PortInUse/api/handshake => Status Code: $StatusCode" | Tee-Object -FilePath $OutputFile -Append
	}catch{
		"POST: http://127.0.0.1:$PortInUse/api/handshake" | Tee-Object -FilePath $OutputFile -Append
		"Exception: $_" | Tee-Object -FilePath $OutputFile -Append	
	}
	
	
		
}

If( $EikonProcessId -ne 0){
	'' | Tee-Object -FilePath $OutputFile -Append
	"5. Find Eikon Log Path" | Tee-Object -FilePath $OutputFile -Append
	'======================================' | Tee-Object -FilePath $OutputFile -Append
	"Eikon Process ID ($EikonProcessName): $EikonProcessId" | Tee-Object -FilePath $OutputFile -Append
	If((Test-Path "$EikonLogPath") -eq $True){
		Get-ChildItem -Path "$EikonLogPath"| Tee-Object -FilePath $OutputFile -Append
	}Else{
		"$EikonLogPath is not found" | Tee-Object -FilePath $OutputFile -Append
	}
}

If( $EikonProcessId -ne 0){
	'' | Tee-Object -FilePath $OutputFile -Append
	"6. Get Eikon Log Files" | Tee-Object -FilePath $OutputFile -Append
	'======================================' | Tee-Object -FilePath $OutputFile -Append
	"Eikon Process ID ($EikonProcessName): $EikonProcessId" | Tee-Object -FilePath $OutputFile -Append
	If((Test-Path "$EikonLogPath") -eq $True){
		$LogPaths = Get-ChildItem -Path "$EikonLogPath" -Filter "*p$EikonProcessId" | Select-Object Name
	Foreach ($TmpLogPath in $LogPaths ){
		$PathName =$TmpLogPath."Name"
		'' | Tee-Object -FilePath $OutputFile -Append
		$FullLogPath = "$EikonLogPath$PathName"
		"===>$FullLogPath" | Tee-Object -FilePath $OutputFile  -Append
		Get-ChildItem -Path "$FullLogPath" | Tee-Object -FilePath $OutputFile  -Append
		$LogFiles = Get-ChildItem -Path "$FullLogPath"| Select-Object Name
		
		Foreach($TmpLogFile in $LogFiles){
			$LogFile = $TmpLogFile."Name"			
			If($LogFile -like "SxS*"){
				"====> $FullLogPath\$LogFile" | Tee-Object -FilePath $OutputFile -Append
				Get-Content -Path "$FullLogPath\$LogFile" | Tee-Object -FilePath $OutputFile -Append
				'======================================' | Tee-Object -FilePath $OutputFile -Append
				'' | Tee-Object -FilePath $OutputFile -Append
			}ELseIf($LogFile -like"*APIProxy*"){
				"====> $FullLogPath\$LogFile" | Tee-Object -FilePath $OutputFile -Append
				Get-Content -Path "$FullLogPath\$LogFile" | Tee-Object -FilePath $OutputFile -Append
				'======================================' | Tee-Object -FilePath $OutputFile -Append
				'' | Tee-Object -FilePath $OutputFile -Append
			}

		}
	}
	}Else{
		"$EikonLogPath is not found" | Tee-Object -FilePath $OutputFile -Append
	}
	
}