Invoke-ASCmd XMLA Results to storage results in Azure Data Lake with either .csv or .txt format - azure-powershell

I've built an Azure Powershell runbook (5.0) to run a MDX query against a SSAS server , the query is just to get the partition names
SELECT
[Name]
FROM $SYSTEM.TMSCHEMA_PARTITIONS
WHERE [Name] <>'Partition'
If I run it in SSMS I get the desired results
The problem comes when I run the query using the powershell runbook because the results are basically an XMLA output
What I need is the Azure powershell runbook to extract just that column from the $Results variable and then create either a .txt file or .csv in the Azure data lake storage V2 with that data ( like the SSMS). That file will be consumed later by Data Factory.
This is the full script:
Param
(
[Parameter (Mandatory= $true)]
[String] $Query,
[Parameter (Mandatory= $true)]
[String] $OperationType,
[Parameter (Mandatory= $true)]
[String] $ServerName,
[Parameter (Mandatory= $true)]
[String] $DatabaseName,
[Parameter (Mandatory= $true)]
[String] $SecretName
)
$cred = Get-AutomationPSCredential -Name $SecretName
$Results = Invoke-ASCmd -Server $ServerName -Database $DatabaseName -Credential $cred -Query $Query
Write-Output $Results

I have reproduced in my environment and got expected results as below:
SSMS:
SELECT [Name] FROM $SYSTEM.TMSCHEMA_PARTITIONS
NOTE:
Your $Results is taking the output as string, you should be converting it to Xml using [XML] in PowerShell script.
Then use below code instead of your code :
$Results = [XML] (Invoke-ASCmd -Server $ServerName -Database $DatabaseName -Credential $cred -Query $Query)
Now use below code:
Write-Output $Results.return.root.row.Name
Output:
You need to incorporate the above 2 codes into your code.After that you need to send the output to your requirement.

Related

PS Script to Import users from Azure SQL Table to AAD

I need help with a baseline script (if possible at all) to extract users from Azure SQL to Azure AD with the fields in the SQL tables. Is there such a way to get this into AAD, not On Premise AD? The fields are phone, department..etc However does such a way even exist? I know for On Premise yes, but I cannot seem to locate anything for Azure.
Thank you all in Advance!
to extract users from Azure SQL to Azure AD with the fields in the SQL tables
Achieving this you have to follow the below steps.
Get the user table data from the SQL table
Export the user data into csv file.
create the users in Azure AD using the csv file.
You can use the below script to export Azure SQL Table data. Then exported data can be saved in a csv file.
Import-Module sqlps
$SQLServer = "<Your Azure SQL server>"
$DatabaseName = "<Database Name>"
$ExportLocation = "<Path to Export>"
$ResourceGroupName ="<Your resource group name>"
#Select Database
Get-AzureRmSqlDatabase -ServerName $SQLServer -ResourceGroupName $ResourceGroupName | select $DatabaseName
# Get Server Instance
$ServerInstance = Get-AzureRmSqlServer -ResourceGroupName $ResourceGroupName | where {$_.ServerName -eq $SQLServer}
$params = #{
'Database' = $DatabaseName
'ServerInstance' = $ServerInstance
'Username' = '<userName>'
'Password' = '<Password>'
'Query' = 'SELECT * FROM UsersTable'
}
$userResult = Invoke-Sqlcmd #params
$result |export-csv "$ExportLocation$userinfo.csv" -notypeinformation
Fetching all information from table
# Get user information from table
$Tables = (Get-SqlDatabase -ServerInstance $ServerInstance -Name $DatabaseName).tables
foreach($table in $Tables) {
$SQLquery="select * from $($table)"
$result=invoke-sqlcmd -query $SQLquery -serverinstance $SQLServer -database $DatabaseName
Write-Host "Now Exporting Table $table"
$result |export-csv "$ExportLocation$($table.Name).csv" -notypeinformation
}
Or in an Azure portal -> Azure SQL Database -> Query Editor also you can export the csv file.
After exported the CSV file import the user information into Azure AD.
Refer Link 1 & Link 2 to import users into Azure AD

creating an output variable azure devops from the Azure SQL InlineSqlTask task

I have this in yaml with azure devops :
- task: SqlAzureDacpacDeployment#1
displayName: 'Azure SQL InlineSqlTask'
inputs:
azureSubscription: 'Service Connection'
AuthenticationType: servicePrincipal
ServerName: 'xxx.database.windows.net'
DatabaseName: 'xxx-Dev'
deployType: InlineSqlTask
SqlInline: |
select name as username,
create_date,
modify_date,
type_desc,
authentication_type_desc as authentication_type
from sys.database_principals where type_desc = 'EXTERNAL_USER'
I need to be able to get the output of this t-sql command stored in a variable so i can use it later on in my pipeline. Any ideas how i get this output and store it in the variable.
I would usually use a powershell command to set the environment variable but obviously this cannot be done within this task.
Any ideas would be amazing.
I have sorted this out. So I can use a powershell script within Azure Devops so i can directly get the variable and then use the setvariable task to assign the output as an env:variable.
$query = "select name as username,
create_date,
modify_date,
type_desc,
authentication_type_desc as authentication_type
from sys.database_principals where type_desc = 'EXTERNAL_USER'"
$clientid = "guid"
$tenantid = "guid"
$secret = "guid"
$request = Invoke-RestMethod -Method POST `
-Uri "https://login.microsoftonline.com/$tenantid/oauth2/token"`
-Body #{ resource="https://database.windows.net/"; grant_type="client_credentials"; client_id=$clientid; client_secret=$secret }`
-ContentType "application/x-www-form-urlencoded"
$access_token = $request.access_token
$sqloutput = Invoke-Sqlcmd -ServerInstance $.database.windows.net -Database db$ -AccessToken $access_token -query $query
Write-Host "##vso[task.setvariable variable=GetVar;]$sqloutput"

Can we display the sql result filled in $dataset on the ISE console or do we need a GUI?

I want to display sql results in powershell console
I have a query that list documents based on the customer code, I was able to output them to a CSV, but I am thinking if I could display them on the console and then do operations like Copy, delete etc.
Or do I need .net web UI to display the results?
cls
$SQLserver = "YourSQLserverName"
$SQLDB = "YourDbName"
$SQLQuery = "SELECT * from [DBName].[dbo].[YourTableName]" # Whatever is your query
$Result = SQLCMD -S $SQLserver -d $SQLDB -Q $SQLQuery -s "~" -W
Write-Host "Display SQL Dataset in the console"
Write-Output $Result # Display the dataset in console

Powershell Job within SQL Server Agent

I am having trouble with a powershell job returning a result set when it runs, it runs successfully but no results. I am running it against a list of servers. If I run the script against a specific server that I know I can connect to, it runs fine and gives results.
This is a script within the sql server agent running powershell. Am I using the SMO object right? Ive tried to use a try/catch (job fails), ive tried to add -ErrorAction "Continue" to the script (job fails), using smo to resolve a server name in order to use the if statement (job succeeds) but no results. Here is the script:
$ErrorActionPreference = "Continue";
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
$instanceNameList = Get-Content "c:\Scripts\InPutFiles\servers.txt";
$results = #()
foreach($instanceName in $instanceNameList)
{
$serverObject = New-Object Microsoft.SqlServer.Management.Smo.Server($instanceName)
$serverName = $serverObject.ComputerNamePhysicalNetBIOS;
if($serverName -ne $null) {
$results += Invoke-Sqlcmd -Query "
(My Query is in here!)
" -ServerInstance $instanceName.Name}
$instanceName| Where-Object {$_} | Export-csv 'C:\scripts\HungJobs_UnabletoConnect.csv' -NoTypeInformation
}
$results| Where-Object {$_} | Export-csv 'C:\scripts\HungJobs.csv' -NoTypeInformation
Mid-way through your script, is the $scripts variable being populated properly by the Invoke-Sqlcmd?
If so, I'm thinking that you should be able to do away with Where-Object {$_} on your last two commands. Where-Object is used as a filter, and in these two cases it doesn't seem to be filtering anything. You should just be able to directly export the $instanceName and $results directly to csv.

Script to use Windows Authentication to fetch data from SQL table and to write it back in .txt file

I am looking for some Power Shell script to fetch data from a SQL Server table using windows authentication and write it back to .txt file.
I am very new to Power Shell thing and never worked on this before. So if somebody could guide me in this effort.
Thanks to both of you guys. I am able to google and make a script working according to requirement.
Here it is
#Script parameters
param(
#Name of MS SQL Server instance
[parameter(Mandatory=$true,
HelpMessage="Specify the SQL Server name where will be run a T-SQL code",Position=0)]
[String]
[ValidateNotNullOrEmpty()]
$server = $(throw "sqlserver parameter is required."),
#Database name for execution context
[parameter(Mandatory=$true,
HelpMessage="Specify the context database name",Position=1)]
[String]
[ValidateNotNullOrEmpty()]
$dbname = $(throw "dbname parameter is required.")
)
$connectionString = “Server=$server;Database=$dbname;Integrated Security=True;”
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$query = “SELECT type,name FROM sys.tables"
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
$table.Load($result)
$table | Out-File D:\test\swedes$dbname.txt
$connection.Close()
Now I am looking for a way to run this script on a remote SQL Server and save the results.
I am googling about this but still if someone knows a way to run this script using SSIS please help.