Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@
//-------- Editor configuration --------
"editor.insertSpaces": true,
"editor.tabSize": 4,

//-------- Files configuration --------
"files.autoGuessEncoding": false,
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"search.exclude": {
"**/Tests/Data*": true
},

//-------- PowerShell configuration --------
"powershell.codeFormatting.alignPropertyValuePairs": true,
"powershell.codeFormatting.preset": "Allman",
"powershell.scriptAnalysis.settingsPath": "./ScriptAnalyzerSettings.psd1",

//-------- Language configuration --------
"[json]": {
"editor.tabSize": 2
},

"[xml]": {
"editor.tabSize": 2
},
Expand Down
19 changes: 17 additions & 2 deletions Docs/en-US/Invoke-SnowSql.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Invokes a Snowflake SQL statement

### QueryConnection (Default)
```
Invoke-SnowSql [-Connection <Object>] [-Query <String[]>] [-WhatIf] [-Confirm] [<CommonParameters>]
Invoke-SnowSql [-Connection <Object>] [-Query <String[]>] [-Timeout <Int>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### PathCred
Expand All @@ -25,7 +25,7 @@ Invoke-SnowSql -Endpoint <String> -Credential <PSCredential> [-Path <String>] [-

### QueryCred
```
Invoke-SnowSql -Endpoint <String> -Credential <PSCredential> [-Query <String[]>] [-WhatIf] [-Confirm]
Invoke-SnowSql -Endpoint <String> -Credential <PSCredential> [-Query <String[]>] [-Timeout <Int>] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

Expand Down Expand Up @@ -123,6 +123,21 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -Timeout
Snowflake timeout for connexion

```yaml
Type: Integer
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: 10
Accept pipeline input: False
Accept wildcard characters: False
```

### -Confirm
Prompts you for confirmation before running the cmdlet.

Expand Down
22 changes: 21 additions & 1 deletion Docs/en-US/Open-SnowSqlConnection.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Opens a connection to Snowflake
## SYNTAX

```
Open-SnowSqlConnection [-Endpoint] <String> [-Credential] <PSCredential> [-WhatIf] [-Confirm]
Open-SnowSqlConnection [-Endpoint] <String> [-Credential] <PSCredential> [-Timeout] <Int> [-WhatIf] [-Confirm]
[<CommonParameters>]
```

Expand All @@ -27,6 +27,11 @@ Establishes a few important environment values for connecting to snowflake
Open-SnowSqlConnection -Endpoint contoso.east-us-2.azure -Credential (Get-Credential)
```

### EXAMPLE 2
```
Open-SnowSqlConnection -Endpoint contoso.east-us-2.azure -Credential (Get-Credential) -Timeout 20
```

## PARAMETERS

### -Credential
Expand Down Expand Up @@ -59,6 +64,21 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -Timeout
Snowflake timeout for connexion

```yaml
Type: Integer
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: 10
Accept pipeline input: False
Accept wildcard characters: False
```

### -Confirm
Prompts you for confirmation before running the cmdlet.

Expand Down
27 changes: 22 additions & 5 deletions SnowSQL/public/Invoke-SnowSql.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ function Invoke-SnowSql
[string[]]
$Query = '!help',

# Timeout query
Comment thread
aikiox marked this conversation as resolved.
Outdated
[Parameter(ParameterSetName = 'QueryCred')]
[Parameter(ParameterSetName = 'QueryConnection')]
Comment thread
aikiox marked this conversation as resolved.
[int]
$timeout = 10,
Comment thread
aikiox marked this conversation as resolved.
Outdated

# SnowSql script file to execute
[Parameter(ParameterSetName = 'PathCred')]
[Parameter(ParameterSetName = 'PathConnection')]
Expand All @@ -56,16 +62,16 @@ function Invoke-SnowSql
try
{
$snowSql = Get-Command snowsql -ErrorAction Stop |
Select-Object -First 1 -ExpandProperty Source
Select-Object -First 1 -ExpandProperty Source
Comment thread
aikiox marked this conversation as resolved.
Outdated
}
catch
{
Write-Error "Could not find [snowsql.exe] on local system. Install snowsql.exe and make it available in the %path%. Install instructions can be found here [https://docs.snowflake.net/manuals/user-guide/snowsql-install-config.html]" -ErrorAction Stop
}

if($PSCmdlet.ParameterSetName -match 'Connection$')
if ($PSCmdlet.ParameterSetName -match 'Connection$')
{
if( -not $Connection )
if ( -not $Connection )
{
$Connection = Get-SnowSqlConnection
}
Expand Down Expand Up @@ -100,6 +106,10 @@ function Invoke-SnowSql
{
'--option', 'log_level=DEBUG'
}
if ($timeout)
{
'--option', $('login_timeout=' + $timeout)
}
if ($Path)
{
'--filename', $Path
Expand All @@ -113,8 +123,15 @@ function Invoke-SnowSql
Write-Debug ("Executing [& '$snowsql' $snowSqlParam]" -f $snowsql)
if ($PSCmdlet.ShouldProcess("Execute SnowSql on [$Endpoint] as [$($Credential.UserName)]. Use -Debug to see full command"))
{
$env:SNOWSQL_PWD = $Credential.GetNetworkCredential().password
$results = & $snowsql @snowSqlParam | ConvertFrom-Csv
$env:SNOWSQL_PWD = $Credential.GetNetworkCredential().Password
try
{
$results = & $snowsql @snowSqlParam | ConvertFrom-Csv
}
catch
{
$results = $null
}
$env:SNOWSQL_PWD = ""
Write-Verbose "LastExitCode[$LastExitCode]"
}
Expand Down
29 changes: 23 additions & 6 deletions SnowSQL/public/Open-SnowSqlConnection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function Open-SnowSqlConnection
#>

[OutputType('SnowSql.Connection')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Implemented in Invoke-SnowSql")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Implemented in Invoke-SnowSql")]
[cmdletbinding(SupportsShouldProcess)]
param(
# Snowflake endpoint (ex: contoso.east-us-2.azure)
Expand All @@ -26,8 +26,19 @@ function Open-SnowSqlConnection
# Credential for snowflake endpoint
[Parameter(Mandatory)]
[PSCredential]
$Credential
$Credential,

# Timeout connexion
[int]
$Timeout = 10
)
begging
{
if ($Endpoint -match '(http[s]?)(:\/\/)([^\s,]+)')
{
Write-Error ("Snowflake endpoint must not be a URL {0}" -f $Endpoint) -ErrorAction Stop
}
}

end
{
Expand All @@ -42,10 +53,16 @@ function Open-SnowSqlConnection
Query = '!help'
ErrorAction = 'Stop'
Connection = $SnowSqlConnection
timeout = $Timeout
}
$Result = Invoke-SnowSql @invokeSnowSqlSplat
if (-not $Result)
{
Write-Error ("Unable to connect to SnowSql endpoint {0}" -f $Endpoint) -ErrorAction Stop
Comment thread
aikiox marked this conversation as resolved.
Outdated
}
else
{
return $Script:SnowSqlConnection
}
$null = Invoke-SnowSql @invokeSnowSqlSplat
$Script:SnowSqlConnection = $SnowSqlConnection

return $Script:SnowSqlConnection
}
}