locked
Beginner With a quick variable question RRS feed

  • Question

  • Hello,

    I'm editing a script to meet our needs, so far I'm about 100 lines in and just have these 3 small pieces of code left to work on.  I'll mark my confusion to the right of the hash tags.

                ### I don't understand "Replace-StringVariables" here.  Searching online in quotes for a better understanding tossed me no results.  Any idea what's going on on this line?
                $id = Replace-StringVariables $group.id $location
                
                ### I've seen this same bit of script on a few sites but a very small handful so I have no references to build off of.  Any insight in what this is doing?
                $mailNickname = $mail.split("@")[0]
                
    Any help is much appreciated!  

    Edit: Probably helps to mention this is related to an Exchange migration.


    • Edited by Lainalil Wednesday, December 19, 2018 3:50 AM
    Wednesday, December 19, 2018 3:44 AM

Answers

    1. Replace-StringVariables is not a default PowerShell cmdlet. Is this a function described elsewhere in your script?
    2. As for the second question, a few things are happening:

    Assuming $mail is a text string, like: "user@domain.com", the "split" method splits that string into pieces, every time it finds the character in the parentheses, which is "@" in your case. Therefore, it would return an array of two strings:

    "User" and "domain.com". One behavior of the split method is that it eats the thing you split on, hence no "@" symbol anymore.

    When you have a group of objects (strings in this case) like this, you have what's called an array.

    i.e.

    @("user", "domain.com")
    
    # or
    
    @(
        "user"
        "domain.com"
    )

    Every item in an PowerShell array has an index number, starting at 0. You can pick items out of an array by specifying the index number, if you know it. Since an email address, broken in half on the "@" symbol has a predictable number of parts, you can assume that you will always return the left side of the address.

    Therefore

                @("user", "domain.com")[0] # returns: user            
                @("user", "domain.com")[1] # returns: domain.com
                @("user", "domain.com")[-1] # returns: domain.com (we're saying do the last item, and counting backwards... -1, -2, -3, etc.



    Mike Crowley
    My Blog -- Baseline Technologies


    • Edited by Mike Crowley Wednesday, December 19, 2018 4:11 AM
    • Proposed as answer by Mike Crowley Wednesday, December 19, 2018 4:11 AM
    • Marked as answer by Lainalil Wednesday, December 19, 2018 4:22 AM
    Wednesday, December 19, 2018 4:10 AM

All replies

    1. Replace-StringVariables is not a default PowerShell cmdlet. Is this a function described elsewhere in your script?
    2. As for the second question, a few things are happening:

    Assuming $mail is a text string, like: "user@domain.com", the "split" method splits that string into pieces, every time it finds the character in the parentheses, which is "@" in your case. Therefore, it would return an array of two strings:

    "User" and "domain.com". One behavior of the split method is that it eats the thing you split on, hence no "@" symbol anymore.

    When you have a group of objects (strings in this case) like this, you have what's called an array.

    i.e.

    @("user", "domain.com")
    
    # or
    
    @(
        "user"
        "domain.com"
    )

    Every item in an PowerShell array has an index number, starting at 0. You can pick items out of an array by specifying the index number, if you know it. Since an email address, broken in half on the "@" symbol has a predictable number of parts, you can assume that you will always return the left side of the address.

    Therefore

                @("user", "domain.com")[0] # returns: user            
                @("user", "domain.com")[1] # returns: domain.com
                @("user", "domain.com")[-1] # returns: domain.com (we're saying do the last item, and counting backwards... -1, -2, -3, etc.



    Mike Crowley
    My Blog -- Baseline Technologies


    • Edited by Mike Crowley Wednesday, December 19, 2018 4:11 AM
    • Proposed as answer by Mike Crowley Wednesday, December 19, 2018 4:11 AM
    • Marked as answer by Lainalil Wednesday, December 19, 2018 4:22 AM
    Wednesday, December 19, 2018 4:10 AM
  • Awesome, so it's broken down how I was thinking, the array portion threw me off.  I didn't want to "assume" about the way I was thinking this was being handled.  I really appreciate the thorough and thoughtful answer.
    Wednesday, December 19, 2018 4:23 AM
  • Hello,

    I'm editing a script to meet our needs, so far I'm about 100 lines in and just have these 3 small pieces of code left to work on.  I'll mark my confusion to the right of the hash tags.

                ### I don't understand "Replace-StringVariables" here.  Searching online in quotes for a better understanding tossed me no results.  Any idea what's going on on this line?
                $id = Replace-StringVariables $group.id $location
                
                ### I've seen this same bit of script on a few sites but a very small handful so I have no references to build off of.  Any insight in what this is doing?
                $mailNickname = $mail.split("@")[0]
                
    Any help is much appreciated!  

    Edit: Probably helps to mention this is related to an Exchange migration.


    Your questions show a very basic lack of knowledge about PowerShell.  A custom function cannot be found in and Internet search and must either be in the script or part of a custom module.

    The split command is well documented and all new users (assuming they have taken a basic tutorial) should understand what "split" does.

    Start by fist learning basic PowerShell.

    1. Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
    2. PowerShell Documentation
    3. PowerShell Style Guidelines

    Once you have a basic understanding of PS,  these issues will be understandable.

    I warn you - there are many blogs and posts by users who  do not understand basic PowerShell and their posts can mislead you in your quest for knowledge.


    \_(ツ)_/

    Wednesday, December 19, 2018 4:26 AM