0

I want to create little script that takes particular type of string and uses it in a way I can process things easily

  • %1g.%s which basically means 1 char from given name and full secondary name (output should be j.snow built from 2 parameters)
  • %g.%s which basically means full given name dot and full secondary name (output should be john.snow built from 2 parameters)
  • %5g.%s which basically means 5 chars from given name and full secondary name but if given name is shorter use shorter version (john.snow)
  • %g%s which would given givenname and secondary name without dot (johnsnow)
  • etc

Question is how do I even start processing it so that I don't create a monster if/else cases? Or should I exactly do that?

EDIT. Since this is Microsoft Exchange Email Template behaviour just wanted to explain this is for Office 365 without on-premise option which doesn't have Email Templates option. In other words I want to create a script that would mimic this behaviour in some way.

5
  • Why? Are you trying to reproduce the behavior of email address templates in Exchange? Commented Aug 1, 2016 at 17:04
  • Office 365 doesn't have email templates (except for on-premise), and I wanted to write nice script doing what Exchange Online should have in first place Commented Aug 1, 2016 at 17:05
  • OK, but you didn't say anything about that in your question. Commented Aug 1, 2016 at 17:06
  • There's not really a need to mimic the on-premises Exchange behavior if you can write your own using a regular expression, which would give you a lot more flexibility anyway. Commented Aug 1, 2016 at 17:09
  • Well that's what I am basically asking. I want to do something similar, maybe I'm starting from wrong place.. feel free to give me a hint. Commented Aug 1, 2016 at 17:11

1 Answer 1

1
$firstName = "Alice"
$lastName = "Bloggs"

$template = '%2g.%[email protected]'

function Get-NameSection {
    # Returns the first $num characters of a name
    # unless $num is 0, missing or longer than the name
    # then returns the entire name

    param([string]$name, [int]$num)

    if (-not $num -or $num -gt $name.Length) { 
        $name 
    } else {
        $name.Substring(0, $num)
    }
}

$template = [regex]::Replace($template, '%(\d*)g', {param($m) Get-NameSection $firstName $m.Groups[1].Value })
$template = [regex]::Replace($template, '%(\d*)s', {param($m) Get-NameSection $lastName $m.Groups[1].Value })

$template
Sign up to request clarification or add additional context in comments.

1 Comment

Nice! I was about to go by using more static approach but this one is great.

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.