Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Last active September 29, 2023 19:44
Show Gist options
  • Save mavaddat/cf61a8d79ae83645095b260a998d9361 to your computer and use it in GitHub Desktop.
Save mavaddat/cf61a8d79ae83645095b260a998d9361 to your computer and use it in GitHub Desktop.
function ConvertTo-TnsnamesOra
{
[CmdletBinding(DefaultParameterSetName = 'PipelineStringArray')]
param
(
[Parameter(ParameterSetName = 'ConnectionsJsonPath')]
[String]$ConnectionsJsonPath = "$env:APPDATA\SQL Developer\system*\o.jdeveloper.db.connection\connections.json",
[Parameter(ParameterSetName = 'PipelineStringArray', ValueFromPipeline, ValueFromPipelineByPropertyName = $true)]
[ValidateScript({ $null -ne $_ -and $null -ne ($_ | ConvertFrom-Json -Depth 99) }, ErrorMessage = 'The input string is not valid JSON.')]
[string[]]$ConnectionsJsonStrArray,
[Parameter(ParameterSetName = 'PipelineString', Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateScript({ $null -ne $_ -and $null -ne ($_ | ConvertFrom-Json -Depth 99) }, ErrorMessage = 'The input string is not valid JSON.')]
[string]$ConnectionsJson
)
<#
.SYNOPSIS
Converts SQL Developer connections.json file to tnsnames.ora file.
.DESCRIPTION
Converts SQL Developer connections.json file to tnsnames.ora file.
The connections.json file is located in the SQL Developer user settings folder.
The default location is %APPDATA%\SQL Developer\system*\o.jdeveloper.db.connection\connections.json
.PARAMETER ConnectionsJsonPath
The path to the connections.json file.
.EXAMPLE
ConvertTo-TnsnamesOra -ConnectionsJsonPath "C:\Users\user\AppData\Roaming\SQL Developer\system19.2.1.247.2212\o.jdeveloper.db.connection\connections.json"
Converts the connections.json file to tnsnames.ora file.
.EXAMPLE
ConvertTo-TnsnamesOra
Converts the connections.json file to tnsnames.ora file. The connections.json file is located in the default SQL Developer user settings folder.
.EXAMPLE
ConvertTo-TnsnamesOra | Out-File -FilePath tnsnames.ora -Encoding utf8
Converts the connections.json file to tnsnames.ora file and saves the file to the current directory.
.OUTPUTS
System.String
The tnsnames.ora file content.
.NOTES
The tnsnames.ora file content is written to the output stream. The use of the Out-File cmdlet is recommended. Save the output to a file named tnsnames.ora.
The tnsnames.ora file is not created automatically. You must create the file manually and save the output to the file.
.LINK
https://gist.github.com/mavaddat/cf61a8d79ae83645095b260a998d9361
#>
begin
{
$connectionsJsonStr = [string]::Empty
switch ($PSCmdlet.ParameterSetName)
{
'PipelineStringArray'
{
Write-Verbose -Message 'Converting JSON from string array.'
$connectionsJsonStr = $ConnectionsJsonStrArray -join [System.Environment]::NewLine
}
'PipelineString'
{
Write-Verbose -Message 'Converting JSON from string.'
$connectionsJsonStr = $ConnectionsJson
}
Default
{
Write-Verbose -Message 'Converting JSON from file at '$ConnectionsJsonPath'.'
$connectionsJsonStr = Get-Content -Path $ConnectionsJsonPath -Raw
}
}
}
process
{
$output = $connectionsJsonStr | ConvertFrom-Json -Depth 99 | Select-Object -ExpandProperty connections | ForEach-Object {
$serviceName = $_.info.serviceName
$hostName = $_.info.hostname
$port = $_.info.port
@"
$serviceName=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = $hostName)(PORT = $port))
)
(CONNECT_DATA =
(SERVICE_NAME = $serviceName)
)
)
"@
}
}
end
{
$output | Write-Output
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment