Regex unable to scan variable but working for a txt file

Nandan Hegde 30,166 Reputation points MVP
2022-11-07T11:38:28.23+00:00

@Andreas Baumgarten In continuation of my prev query : https://learn.microsoft.com/en-us/answers/questions/1077328/extracting-specific-value-within-string-via-powers.html

Regex works properly when I extract the variable $x output in text file and search for the string in accordance with prev query

257800-image.png

but when I leverage the variable directly in Regex scan without outputting it in text file I am unable to get the results :

257901-image.png

This is what I was asking as to is it even possible to scan in a variable or is it restricted only to file?

If we can scan variables, than why is there a difference in output between the 2 codes

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,434 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Andreas Baumgarten 100.3K Reputation points MVP
    2022-11-07T11:53:06.517+00:00

    Hi @Nandan Hegde ,

    what happens if you modify line 19 to this:

    [array]$x=$PipelineDataSet.Activities  
    

    This way $x is an array instead of a string variable.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 45,416 Reputation points
    2022-11-07T15:20:52.427+00:00

    The get-azdatafactoryv2pipeline cmdlet returns an object having the type "PSPipeline". The PSPipeline object itself has properties (Activities, ETag, Id, Name, and Parameters). The Activities property has the type System.Collections.Generic.IList and that list contains objects having the type Microsoft.Azure.Management.DataFactory.Models.Activity. Each of those Activity objects has a property called "Name".

    When you send the Activities to a file (i.e., Out-File), you're converting the data to a series of strings, each of which is written to a distinct record in the file. You can read that file and examine it using regular expressions.

    I don't have the means to try this, but I think all you have to do is this:

    $x = $PipelineDataSet.Activities.Name  
    

    That should create a variable that will have either the value of the only name in the only activity, or an array of all names in all activities. If you must have an array, just cast the variable $x as "[array]"


  3. Andreas Baumgarten 100.3K Reputation points MVP
    2022-11-07T15:31:57.63+00:00

    Hi @Nandan Hegde ,

    here the following code works as expected and it doesn't matter if its [array]$x or just $x:

    [array]$x = @'  
      
      
    WaitTimeInSeconds    : 1  
    AdditionalProperties : {[typeProperties, {  
                             "waitTimeInSeconds": 1  
                           }]}  
    Name                 : Wait1  
    Description          :   
    DependsOn            : {}  
    UserProperties       : {}  
      
    VariableName         : dummy  
    Value                : {value, type}  
    AdditionalProperties : {[typeProperties, {  
                             "variableName": "dummy",  
                             "value": {  
                               "value": "@pipeline().parameters.Test",  
                               "type": "Expression"  
                             }  
                           }]}  
    Name                 : Set variable1  
    Description          :   
    DependsOn            : {Microsoft.Azure.Management.DataFactory.Models.ActivityDependency}  
    UserProperties       : {}  
      
    IsSequential         :   
    BatchCount           :   
    Items                : Microsoft.Azure.Management.DataFactory.Models.Expression  
    Activities           : {Wait2, Wait3, If Condition2}  
    AdditionalProperties : {[typeProperties, {  
                             "items": {  
                               "value": "2",  
                               "type": "Expression"  
                             },  
                             "activities": [  
                               {  
                                 "name": "Wait2",  
                                 "type": "Wait",  
                                 "dependsOn": [],  
                                 "userProperties": [],  
                                 "typeProperties": {  
                                   "waitTimeInSeconds": 1  
                                 }  
                               },  
                               {  
                                 "name": "Wait3",  
                                 "type": "Wait",  
                                 "dependsOn": [  
                                   {  
                                     "activity": "Wait2",  
                                     "dependencyConditions": [  
                                       "Succeeded"  
                                     ]  
                                   }  
                                 ],  
                                 "userProperties": [],  
                                 "typeProperties": {  
                                   "waitTimeInSeconds": 1  
                                 }  
                               },  
                               {  
                                 "name": "If Condition2",  
                                 "type": "IfCondition",  
                                 "dependsOn": [  
                                   {  
                                     "activity": "Wait3",  
                                     "dependencyConditions": [  
                                       "Succeeded"  
                                     ]  
                                   }  
                                 ],  
                                 "userProperties": [],  
                                 "typeProperties": {  
                                   "expression": {  
                                     "value": "@equals(pipeline().parameters.Test,'1')",  
                                     "type": "Expression"  
                                   },  
                                   "ifFalseActivities": [  
                                     {  
                                       "name": "Wait10",  
                                       "type": "Wait",  
                                       "dependsOn": [],  
                                       "userProperties": [],  
                                       "typeProperties": {  
                                         "waitTimeInSeconds": 1  
                                       }  
                                     },  
                                     {  
                                       "name": "Wait11",  
                                       "type": "Wait",  
                                       "dependsOn": [  
                                         {  
                                           "activity": "Wait10",  
                                           "dependencyConditions": [  
                                             "Succeeded"  
                                           ]  
                                         }  
                                       ],  
                                       "userProperties": [],  
                                       "typeProperties": {  
                                         "waitTimeInSeconds": 1  
                                       }  
                                     }  
                                   ],  
                                   "ifTrueActivities": [  
                                     {  
                                       "name": "Wait8",  
                                       "type": "Wait",  
                                       "dependsOn": [],  
                                       "userProperties": [],  
                                       "typeProperties": {  
                                         "waitTimeInSeconds": 1  
                                       }  
                                     },  
                                     {  
                                       "name": "Wait9",  
                                       "type": "Wait",  
                                       "dependsOn": [  
                                         {  
                                           "activity": "Wait8",  
                                           "dependencyConditions": [  
                                             "Succeeded"  
                                           ]  
                                         }  
                                       ],  
                                       "userProperties": [],  
                                       "typeProperties": {  
                                         "waitTimeInSeconds": 1  
                                       }  
                                     }  
                                   ]  
                                 }  
                               }  
                             ]  
                           }]}  
    Name                 : ForEach1  
    Description          :   
    DependsOn            : {Microsoft.Azure.Management.DataFactory.Models.ActivityDependency}  
    UserProperties       : {}  
      
    Expression           : Microsoft.Azure.Management.DataFactory.Models.Expression  
    IfTrueActivities     : {Wait4, Wait6}  
    IfFalseActivities    : {Wait5, Wait7}  
    AdditionalProperties : {[typeProperties, {  
                             "expression": {  
                               "value": "@equals(pipeline().parameters.Test,'1')",  
                               "type": "Expression"  
                             },  
                             "ifFalseActivities": [  
                               {  
                                 "name": "Wait5",  
                                 "type": "Wait",  
                                 "dependsOn": [],  
                                 "userProperties": [],  
                                 "typeProperties": {  
                                   "waitTimeInSeconds": 1  
                                 }  
                               },  
                               {  
                                 "name": "Wait7",  
                                 "type": "Wait",  
                                 "dependsOn": [  
                                   {  
                                     "activity": "Wait5",  
                                     "dependencyConditions": [  
                                       "Succeeded"  
                                     ]  
                                   }  
                                 ],  
                                 "userProperties": [],  
                                 "typeProperties": {  
                                   "waitTimeInSeconds": 1  
                                 }  
                               }  
                             ],  
                             "ifTrueActivities": [  
                               {  
                                 "name": "Wait4",  
                                 "type": "Wait",  
                                 "dependsOn": [],  
                                 "userProperties": [],  
                                 "typeProperties": {  
                                   "waitTimeInSeconds": 1  
                                 }  
                               },  
                               {  
                                 "name": "Wait6",  
                                 "type": "Wait",  
                                 "dependsOn": [  
                                   {  
                                     "activity": "Wait4",  
                                     "dependencyConditions": [  
                                       "Succeeded"  
                                     ]  
                                   }  
                                 ],  
                                 "userProperties": [],  
                                 "typeProperties": {  
                                   "waitTimeInSeconds": 1  
                                 }  
                               }  
                             ]  
                           }]}  
    Name                 : If Condition1  
    Description          :   
    DependsOn            : {Microsoft.Azure.Management.DataFactory.Models.ActivityDependency}  
    UserProperties       : {}  
      
    On                   : Microsoft.Azure.Management.DataFactory.Models.Expression  
    Cases                : {Microsoft.Azure.Management.DataFactory.Models.SwitchCase}  
    DefaultActivities    : {Wait12}  
    AdditionalProperties : {[typeProperties, {  
                             "on": {  
                               "value": "@pipeline().parameters.Test",  
                               "type": "Expression"  
                             },  
                             "cases": [  
                               {  
                                 "value": "Case1",  
                                 "activities": [  
                                   {  
                                     "name": "Wait13",  
                                     "type": "Wait",  
                                     "dependsOn": [],  
                                     "userProperties": [],  
                                     "typeProperties": {  
                                       "waitTimeInSeconds": 1  
                                     }  
                                   }  
                                 ]  
                               }  
                             ],  
                             "defaultActivities": [  
                               {  
                                 "name": "Wait12",  
                                 "type": "Wait",  
                                 "dependsOn": [],  
                                 "userProperties": [],  
                                 "typeProperties": {  
                                   "waitTimeInSeconds": 1  
                                 }  
                               }  
                             ]  
                           }]}  
    Name                 : Switch1  
    Description          :   
    DependsOn            : {Microsoft.Azure.Management.DataFactory.Models.ActivityDependency}  
    UserProperties       : {}  
      
      
      
      
    '@  
    $x.GetType()  
    $RegexPattern='"name": "(.*?)",'  
    [array]$InnerActivities = ([regex]::Matches(($x),$RegexPattern).groups|Where-Object Name -EQ '1').Value  
    $InnerActivities  
    

    Result looks like this:

    257962-image.png

    Where do you specify the $RegexPattern in your script?

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  4. Rich Matheisen 45,416 Reputation points
    2022-11-07T19:29:57.003+00:00

    Where I used Get-Content below, you can use the REST output (which should contain the same JSON you posted:

    $x=(Get-Content C:\junk\Something.json -Raw) | ConvertFrom-JSON  
    $x.properties.activities.name  
    

    The output from the JSON you supplied would be:

    Wait1
    Set variable1
    ForEach1
    If Condition1
    Switch1

    Note the different in the number of "name" properties from the results you get from looking at the entire file as if were unorganized glob of strings. Only the names of "activities" is returned, but not the names of "ifFalseActivities" or "ifTrueActivities" which are children of activities.