0

I am writing one powershell script to get display one record of a table from the Azure Database. I am using select query in the powershell script to fetch the specific record. I have to declare a column name as a variable from that table and I have write the if else condition to update that particular column values with the help of update query. Below is my script:

# Import the module
 #Import-Module -Name SqlServer

#Set Azure subscription
#az account set --subscription "subscriptionid"

#Query Needs to execute
$query = "select * from Sys_Mst_VMProperties where Name = 'test7777' and idVMProperties = 1961"

# Setup your parameters
$Params = @{
    'ServerInstance' = 'instance.database.windows.net';
    'Database' = 'dbname';
    'Username' = 'userid';
    'Password' = 'pwd';
    'Query' = $query;
}

# Splat
Invoke-Sqlcmd @Params

From the above output, I have the declare the VMStatus as variable and the variable should read the value from the table.

Kindly help us for above problem.

Lokesh M
  • 117
  • 1
  • 1
  • 6
  • `$VMStatus = (Invoke-Sqlcmd @Params)[[int]$Row].VMStatus`, or just `$VMStatus = (Invoke-Sqlcmd @Params).VMStatus` if it concerns a single row. – iRon Jun 22 '22 at 08:39

1 Answers1

0

Invoke-Sqlcmd - It helps to run the SQL statements that supports the utility of SQL Server Sqlcmd.

As per the Microsoft Documentation, we have several example scenarios.

Like, below is the sample example for Invoke a script and pass in variable values from a string.

$StringA = "Var1='RAJ'", "Var2='KUMAR'" 
Invoke-Sqlcmd -Query  "SELECT `$(Var1) AS Var1, `$(Var2) AS Var2" -Variable  $String

Output: |Var1|Var2| |--|--| |RAJ|KUMAR|

As per your scenario,

I have the declare the VMStatus as variable and the variable should read the value from the table.

As suggested by @iRon, try with the mentioned two approaches it will work.

And refer, this for invoke-command.

RajkumarPalnati
  • 541
  • 2
  • 6