How to find data and replace variable in PowerShell Command










0















I currently have a series of Dyanmic Distribution Groups that I want to edit the recipient filter on. Our company is based by location number, which is a 4 digit number. This number is part of the display name of the dynamic distribution group...example webcontact_1234_DG....1234 would be the 4 digit center number. I am wanting to replace the recipient filter to have office -eq (1234) but have it pull the number from the display name. All display names are going to be the same number of characters before the 4 digit center number, for example, webcontact_1234_DG, webcontact_2345_DG, webcontact_3456_DG, etc.



I have a replace code but it changed the office location to null.



Here is the code that I am using:



$groups=Get-DynamicDistributionGroup -filter alias -like "webcontact*"
foreach ($group in $groups)
$locationcode=$($group.alias).tostring.replace("\D", "")
set-dynamicdistributiongroup $group -recipientfilter ((((office -eq $locationcode) -and
(have the recipent filter here but can't display due to confidential information) -and
(RecipientType -eq 'UserMailbox') -and
(-not(RecipientTypeDetails -eq 'RoomMailbox')) -and
(-not(RecipientTypeDetails -eq 'SharedMailbox'))))










share|improve this question
























  • So you want to use the 4 digit value within the display name to change the office field of said contact?

    – SysEngineer
    Nov 12 '18 at 18:24











  • As an aside: It's best to avoid the use of script blocks ( ... ) as -Filter arguments.

    – mklement0
    Nov 12 '18 at 18:40















0















I currently have a series of Dyanmic Distribution Groups that I want to edit the recipient filter on. Our company is based by location number, which is a 4 digit number. This number is part of the display name of the dynamic distribution group...example webcontact_1234_DG....1234 would be the 4 digit center number. I am wanting to replace the recipient filter to have office -eq (1234) but have it pull the number from the display name. All display names are going to be the same number of characters before the 4 digit center number, for example, webcontact_1234_DG, webcontact_2345_DG, webcontact_3456_DG, etc.



I have a replace code but it changed the office location to null.



Here is the code that I am using:



$groups=Get-DynamicDistributionGroup -filter alias -like "webcontact*"
foreach ($group in $groups)
$locationcode=$($group.alias).tostring.replace("\D", "")
set-dynamicdistributiongroup $group -recipientfilter ((((office -eq $locationcode) -and
(have the recipent filter here but can't display due to confidential information) -and
(RecipientType -eq 'UserMailbox') -and
(-not(RecipientTypeDetails -eq 'RoomMailbox')) -and
(-not(RecipientTypeDetails -eq 'SharedMailbox'))))










share|improve this question
























  • So you want to use the 4 digit value within the display name to change the office field of said contact?

    – SysEngineer
    Nov 12 '18 at 18:24











  • As an aside: It's best to avoid the use of script blocks ( ... ) as -Filter arguments.

    – mklement0
    Nov 12 '18 at 18:40













0












0








0








I currently have a series of Dyanmic Distribution Groups that I want to edit the recipient filter on. Our company is based by location number, which is a 4 digit number. This number is part of the display name of the dynamic distribution group...example webcontact_1234_DG....1234 would be the 4 digit center number. I am wanting to replace the recipient filter to have office -eq (1234) but have it pull the number from the display name. All display names are going to be the same number of characters before the 4 digit center number, for example, webcontact_1234_DG, webcontact_2345_DG, webcontact_3456_DG, etc.



I have a replace code but it changed the office location to null.



Here is the code that I am using:



$groups=Get-DynamicDistributionGroup -filter alias -like "webcontact*"
foreach ($group in $groups)
$locationcode=$($group.alias).tostring.replace("\D", "")
set-dynamicdistributiongroup $group -recipientfilter ((((office -eq $locationcode) -and
(have the recipent filter here but can't display due to confidential information) -and
(RecipientType -eq 'UserMailbox') -and
(-not(RecipientTypeDetails -eq 'RoomMailbox')) -and
(-not(RecipientTypeDetails -eq 'SharedMailbox'))))










share|improve this question
















I currently have a series of Dyanmic Distribution Groups that I want to edit the recipient filter on. Our company is based by location number, which is a 4 digit number. This number is part of the display name of the dynamic distribution group...example webcontact_1234_DG....1234 would be the 4 digit center number. I am wanting to replace the recipient filter to have office -eq (1234) but have it pull the number from the display name. All display names are going to be the same number of characters before the 4 digit center number, for example, webcontact_1234_DG, webcontact_2345_DG, webcontact_3456_DG, etc.



I have a replace code but it changed the office location to null.



Here is the code that I am using:



$groups=Get-DynamicDistributionGroup -filter alias -like "webcontact*"
foreach ($group in $groups)
$locationcode=$($group.alias).tostring.replace("\D", "")
set-dynamicdistributiongroup $group -recipientfilter ((((office -eq $locationcode) -and
(have the recipent filter here but can't display due to confidential information) -and
(RecipientType -eq 'UserMailbox') -and
(-not(RecipientTypeDetails -eq 'RoomMailbox')) -and
(-not(RecipientTypeDetails -eq 'SharedMailbox'))))







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 18:11









LotPings

18.4k61532




18.4k61532










asked Nov 12 '18 at 18:00









Nick AndersonNick Anderson

31




31












  • So you want to use the 4 digit value within the display name to change the office field of said contact?

    – SysEngineer
    Nov 12 '18 at 18:24











  • As an aside: It's best to avoid the use of script blocks ( ... ) as -Filter arguments.

    – mklement0
    Nov 12 '18 at 18:40

















  • So you want to use the 4 digit value within the display name to change the office field of said contact?

    – SysEngineer
    Nov 12 '18 at 18:24











  • As an aside: It's best to avoid the use of script blocks ( ... ) as -Filter arguments.

    – mklement0
    Nov 12 '18 at 18:40
















So you want to use the 4 digit value within the display name to change the office field of said contact?

– SysEngineer
Nov 12 '18 at 18:24





So you want to use the 4 digit value within the display name to change the office field of said contact?

– SysEngineer
Nov 12 '18 at 18:24













As an aside: It's best to avoid the use of script blocks ( ... ) as -Filter arguments.

– mklement0
Nov 12 '18 at 18:40





As an aside: It's best to avoid the use of script blocks ( ... ) as -Filter arguments.

– mklement0
Nov 12 '18 at 18:40












2 Answers
2






active

oldest

votes


















0














This is the best answer I can come up with based on the given information. I assume you are having a problem getting that number out of your webcontact_1234_DG, I would use regex to get those numbers out and into another variable.



$locationcode = [regex]::Match($group.alias,'^[^_]+_([^_]+)_[^_]+$').Groups[1].Value


The above code will grab everything in between the two underscores.



Try that and let me know.






share|improve this answer






























    0














    It's easiest to use the -split operator to extract the number (text) from your values[1]:



    $locationcode = ($group.Alias -split '_')[1]


    -split '_' returns the array of tokens that result when you split the input string by _ chars., and [1] returns the 2nd token, which is the desired location number.



    Simple example:



    PS> ('webcontact_3456_DG' -split '_')[1]
    3456


    Alternatively, a corrected version of your own attempt (see below) would use the -replace operator:



    PS> 'webcontact_3456_DG' -replace 'D' # remove all non-digit chars.
    3456



    As for what you tried:




    $($group.alias).tostring.replace("\D", "")




    • The [string] type's .Replace() searches by literal strings, so the search for \D in your names will fail, and no replacement will occur.



      • Additionally, note that PowerShell's escape character is ` (backtick), not , and that therefore doesn't require escaping: in PowerShell "\D" literally becomes \D.


    • As an aside: There is generally no need to put $(...) around expressions.



    [1] You could also use the [string] type's .Split() .NET method in this simple case, but I suggest using the far more flexible -split PS operator as a matter of habit.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267666%2fhow-to-find-data-and-replace-variable-in-powershell-command%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      This is the best answer I can come up with based on the given information. I assume you are having a problem getting that number out of your webcontact_1234_DG, I would use regex to get those numbers out and into another variable.



      $locationcode = [regex]::Match($group.alias,'^[^_]+_([^_]+)_[^_]+$').Groups[1].Value


      The above code will grab everything in between the two underscores.



      Try that and let me know.






      share|improve this answer



























        0














        This is the best answer I can come up with based on the given information. I assume you are having a problem getting that number out of your webcontact_1234_DG, I would use regex to get those numbers out and into another variable.



        $locationcode = [regex]::Match($group.alias,'^[^_]+_([^_]+)_[^_]+$').Groups[1].Value


        The above code will grab everything in between the two underscores.



        Try that and let me know.






        share|improve this answer

























          0












          0








          0







          This is the best answer I can come up with based on the given information. I assume you are having a problem getting that number out of your webcontact_1234_DG, I would use regex to get those numbers out and into another variable.



          $locationcode = [regex]::Match($group.alias,'^[^_]+_([^_]+)_[^_]+$').Groups[1].Value


          The above code will grab everything in between the two underscores.



          Try that and let me know.






          share|improve this answer













          This is the best answer I can come up with based on the given information. I assume you are having a problem getting that number out of your webcontact_1234_DG, I would use regex to get those numbers out and into another variable.



          $locationcode = [regex]::Match($group.alias,'^[^_]+_([^_]+)_[^_]+$').Groups[1].Value


          The above code will grab everything in between the two underscores.



          Try that and let me know.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 19:02









          SysEngineerSysEngineer

          1236




          1236























              0














              It's easiest to use the -split operator to extract the number (text) from your values[1]:



              $locationcode = ($group.Alias -split '_')[1]


              -split '_' returns the array of tokens that result when you split the input string by _ chars., and [1] returns the 2nd token, which is the desired location number.



              Simple example:



              PS> ('webcontact_3456_DG' -split '_')[1]
              3456


              Alternatively, a corrected version of your own attempt (see below) would use the -replace operator:



              PS> 'webcontact_3456_DG' -replace 'D' # remove all non-digit chars.
              3456



              As for what you tried:




              $($group.alias).tostring.replace("\D", "")




              • The [string] type's .Replace() searches by literal strings, so the search for \D in your names will fail, and no replacement will occur.



                • Additionally, note that PowerShell's escape character is ` (backtick), not , and that therefore doesn't require escaping: in PowerShell "\D" literally becomes \D.


              • As an aside: There is generally no need to put $(...) around expressions.



              [1] You could also use the [string] type's .Split() .NET method in this simple case, but I suggest using the far more flexible -split PS operator as a matter of habit.






              share|improve this answer





























                0














                It's easiest to use the -split operator to extract the number (text) from your values[1]:



                $locationcode = ($group.Alias -split '_')[1]


                -split '_' returns the array of tokens that result when you split the input string by _ chars., and [1] returns the 2nd token, which is the desired location number.



                Simple example:



                PS> ('webcontact_3456_DG' -split '_')[1]
                3456


                Alternatively, a corrected version of your own attempt (see below) would use the -replace operator:



                PS> 'webcontact_3456_DG' -replace 'D' # remove all non-digit chars.
                3456



                As for what you tried:




                $($group.alias).tostring.replace("\D", "")




                • The [string] type's .Replace() searches by literal strings, so the search for \D in your names will fail, and no replacement will occur.



                  • Additionally, note that PowerShell's escape character is ` (backtick), not , and that therefore doesn't require escaping: in PowerShell "\D" literally becomes \D.


                • As an aside: There is generally no need to put $(...) around expressions.



                [1] You could also use the [string] type's .Split() .NET method in this simple case, but I suggest using the far more flexible -split PS operator as a matter of habit.






                share|improve this answer



























                  0












                  0








                  0







                  It's easiest to use the -split operator to extract the number (text) from your values[1]:



                  $locationcode = ($group.Alias -split '_')[1]


                  -split '_' returns the array of tokens that result when you split the input string by _ chars., and [1] returns the 2nd token, which is the desired location number.



                  Simple example:



                  PS> ('webcontact_3456_DG' -split '_')[1]
                  3456


                  Alternatively, a corrected version of your own attempt (see below) would use the -replace operator:



                  PS> 'webcontact_3456_DG' -replace 'D' # remove all non-digit chars.
                  3456



                  As for what you tried:




                  $($group.alias).tostring.replace("\D", "")




                  • The [string] type's .Replace() searches by literal strings, so the search for \D in your names will fail, and no replacement will occur.



                    • Additionally, note that PowerShell's escape character is ` (backtick), not , and that therefore doesn't require escaping: in PowerShell "\D" literally becomes \D.


                  • As an aside: There is generally no need to put $(...) around expressions.



                  [1] You could also use the [string] type's .Split() .NET method in this simple case, but I suggest using the far more flexible -split PS operator as a matter of habit.






                  share|improve this answer















                  It's easiest to use the -split operator to extract the number (text) from your values[1]:



                  $locationcode = ($group.Alias -split '_')[1]


                  -split '_' returns the array of tokens that result when you split the input string by _ chars., and [1] returns the 2nd token, which is the desired location number.



                  Simple example:



                  PS> ('webcontact_3456_DG' -split '_')[1]
                  3456


                  Alternatively, a corrected version of your own attempt (see below) would use the -replace operator:



                  PS> 'webcontact_3456_DG' -replace 'D' # remove all non-digit chars.
                  3456



                  As for what you tried:




                  $($group.alias).tostring.replace("\D", "")




                  • The [string] type's .Replace() searches by literal strings, so the search for \D in your names will fail, and no replacement will occur.



                    • Additionally, note that PowerShell's escape character is ` (backtick), not , and that therefore doesn't require escaping: in PowerShell "\D" literally becomes \D.


                  • As an aside: There is generally no need to put $(...) around expressions.



                  [1] You could also use the [string] type's .Split() .NET method in this simple case, but I suggest using the far more flexible -split PS operator as a matter of habit.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 '18 at 18:56

























                  answered Nov 12 '18 at 18:44









                  mklement0mklement0

                  128k20241270




                  128k20241270



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267666%2fhow-to-find-data-and-replace-variable-in-powershell-command%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Darth Vader #20

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Ondo