1

I have a Azure SQL Database with a SQL View - that I want to view in PowerShell.

I have been looking and working with :

Read-SqlViewData -ServerInstance "xxx.database.windows.net" -DatabaseName "dbname" -ViewName "viewNAme" -TopN 2 -Credential $cred -SchemaName "dbo"

I have got the $cred by get-credential.

I am wondering if this is now for Azure SQL and it is for a SQL installation on a server, as I am getting an error:

ItemNotFoundException on the Server Instance.

Any help would be great as this is my first time getting sql azure data with PowerShell.

1 Answer 1

1

Honestly never used this command but I was able to use other Powershell command from the same module like "Invoke-Sqlcmd" against Azure SQL DB, therefore the command should be compatible with Azure SQL DB.

I used Invoke-Sqlcmd several time using dbname, servername, username, password and the query, maybe you can give a try, e.g:

$params = @{
    'Database' = ${DbName}
    'ServerInstance' = $ServerInstance.FullyQualifiedDomainName
    'Username' = ${UserDB}
    'Password' = ${dbPassword}
    'Query' = ${SqlQuery}
} 
$val = Invoke-Sqlcmd @params

Other option is to use SqlConnection like (auth with token in this case):

    $conn = New-Object System.Data.SqlClient.SQLConnection
    $conn.ConnectionString = "Data Source=$SQLSERVERNAME.database.windows.net;Initial Catalog=$DBNAMEe;Connect Timeout=30"
    $conn.AccessToken = $tokenAuth
    $conn.Open() 
    $conn
    $query = "YOUR QUERY"
    $command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $conn)
    $Result = $command.ExecuteScalar()
Sign up to request clarification or add additional context in comments.

3 Comments

thats amazing, although i have been given a view on the SQL to use and not really an SQL TSQL query...
if you look at the command i was using it is asking for a serverinstance but I cannot seem to figure out what the instance is on Azure SQL .
I have got it working with your help... cheers for that man !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.