1

I have a code:

$x = Invoke-AzVMRunCommand -ResourceGroupName 'testrg1' -Name 'sqlvm1'-CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\joe\OneDrive - Microsoft\Documents\joe\Miscellaneous\run_sqlserver_script.ps1' -Parameter @{"VmName" = 'sqlvm1'}

Output of $x: enter image description here

Type of X: enter image description here

I want the stuff in the Message to be in an array so that I can access each value. But right now $x.Value[0].Message.gettype() is a String. When I run $x.Value[0].Message.ComputerName nothing shows up. How do I convert the stuff in the Message from String to Array?

enter image description here enter image description here enter image description here

3
  • you will have to post the contents of the script run_sqlserver_script.ps1...its probably either converting or assigning string values to the message property. Commented Jul 30, 2021 at 6:06
  • Or ConvertTo-Json or ConvertTo-Expression $x and add the results (as text) to the question. Commented Jul 30, 2021 at 6:28
  • @Kiran This is the content #Calling Named Parameters param([string]$VmName) #Invoke SQL Script on the VMs Invoke-Sqlcmd -ServerInstance $VmName -query "SELECT SERVERPROPERTY('MachineName') AS 'VirtualMachineName', SERVERPROPERTY('ServerName') AS 'ServerName', SERVERPROPERTY('Edition') AS 'Edition', SERVERPROPERTY('EngineEdition') AS 'EngineEdition', SERVERPROPERTY('ProductVersion') AS 'ProductVersion', SERVERPROPERTY('ProductLevel') AS 'ProductLevel'" Commented Jul 30, 2021 at 22:43

1 Answer 1

1

If as you show $x.Value[0].Message.gettype() is a string looking like the output of Format-List

ComputerName     : sqlvm1
InstanceName     : sqlvm1
Edition          : Developer Edition (64-bit)
EngineEdition    : 3
ProductVersion   : 15.0.4138.2
ProductLevel     : RTM

you can parse that out like:

($x.Value[0].Message -replace '(?<!:.*):', '=' | ConvertFrom-StringData).ComputerName

Regex details on the -replace to replace only the first occurrence of the colon:

(?<!        Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind)
   :        Match the character “:” literally
   .        Match any single character
      *     Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)          
:           Match the character “:” literally
Sign up to request clarification or add additional context in comments.

Comments

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.