0

I have a Powershell function that loops through an array and populates attributes to a new Exchange/Office 365 group. The recent challenge that was asked was to prepend certain text to only certain attributes from the inputObject.

Here is the core working function before attempting to prepend the text:

$allObjects = @(Get-ChildItem -path c:\tmp\json\*.json | Get-Content -Raw | ConvertFrom-Json)

function Import-UnixDL2Group {
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        $InputObject
    )

    Process{        
        New-UnifiedGroup -Alias $InputObject.alias `
        -DisplayName $InputObject.displayname `
        -Members $InputObject.members.recipients.value `
        -PrimarySmtpAddress $InputObject.emailaddresses.value `
    }
}

$allObjects | Import-UnixDL2Group

So the prepended text needs to occur either before or during the Import-UnixDL2Group function.

I have tried adding "gr-" + to the -alias and -displayname settings in the primary function, but it doesn't work:

function Import-UnixDL2Group {
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        $InputObject
    )

    Process{        
        New-UnifiedGroup -Alias "gr-" + $InputObject.alias `
        -DisplayName "gr-" + $InputObject.displayname `
        -Members $InputObject.members.recipients.value `
        -PrimarySmtpAddress $InputObject.emailaddresses.value `
    }
}

I attempted to create a new function just for the purpose of prepending text:

function prependGR {
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        $InputObject
    )

    Process {
        "gr-" + $InputObject.alias
    }
}

$allObjects | prependGR

The prependGR function produces the correct result, but it doesn't work with the main Import-UnixDL2Group function.

Adding another variable and calling the prependGR function in the process only produces gr- without the actual variables appended:

    Process{        
        $renameAlias = $InputObject.alias | prependGR
        New-UnifiedGroup -Alias $renameAlias `

I have been experimenting with adding additional parameters in the main function, but with no success.

Any guidance would be greatly appreciated.

Thank you in advance!

  • have you tried to remove `.alias` from the `Process` method in `prependGR` – No Refunds No Returns Jul 13 '18 at 18:21
  • Thank you for the reply. I did and it comes back: "Cannot validate argument on parameter 'Alias'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again." –  Jul 13 '18 at 18:48

2 Answers2

1

For an expression such as "gr-" + $InputObject.alias to be recognized as a (single, evaluated) command argument, you must enclose it in (...):

New-UnifiedGroup -Alias ("gr-" + $InputObject.alias) ...

Alternatively, use a "..." string with string interpolation (string expansion):

New-UnifiedGroup -Alias "gr-$($InputObject.alias)" ...

Note the need to enclose the embedded $InputObject.alias expression in $(...) (which is not needed for mere variable references, e.g. "I'm $HOME" - see this answer of mine for an overview of string-expansion rules).

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

How about

New-UnifiedGroup -Alias $("gr-$InputObject.alias") `
    -DisplayName $("gr-$InputObject.displayname") `
    -Members $InputObject.members.recipients.value `
    -PrimarySmtpAddress $InputObject.emailaddresses.value `
Theo
  • 57,719
  • 8
  • 24
  • 41
  • You don't need (and shouldn't use) `$(...)` around a double-quoted string, but you do need it around _expressions_ inside such an (expandable) string; e.g.: `"gr-$($InputObject.alias)"` – mklement0 Jul 13 '18 at 19:40
  • 1
    Fantastic! "gr-$($inputobject.alias)" worked like a charm. Thank you! –  Jul 13 '18 at 20:10