0

I have some functions in a powershell script that I am trying to execute using params. I have did this before for other powershell scripts and it worked perfectly but now, when I try to run any of my functions I get this:

intermedia_pwsh_functions.ps1 -Add-im_Email

InvalidOperation: /home/tech/scripts/Alloy_automate/intermedia_pwsh_functions.ps1:80:3
Line |
  80 |  & $command
     |    ~~~~~~~~
     | The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a
     | CommandInfo object.

Here is my code:

param (
    $command,
    [CmdletBinding(DefaultParameterSetName = 'license')]
    $imcid,
    [CmdletBinding(DefaultParameterSetName = 'email')]
    $eoptenantID,
    [CmdletBinding(DefaultParameterSetName = 'email')]
    $emailresult,
    [CmdletBinding(DefaultParameterSetName = 'email')]
    $empw,
    [CmdletBinding(DefaultParameterSetName = 'email')]
    $emname,
    [CmdletBinding(DefaultParameterSetName = 'email')]
)

function Add-im_Email {
    #creates AD and email account
    $password = ConvertTo-SecureString "RMLword3$" -AsPlainText -Force
    $emailpass = ConvertTo-SecureString -String "TPPass1!" -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential ("[email protected]", $password)
    $ses = New-PSSession -Name "[email protected]" -ConnectionUri https://exchange.intermedia.net/powershell -ConfigurationName Hosting.PowerShell -Credential $Cred -Authentication Basic
    Invoke-Command -Session $ses -ScriptBlock {Set-ConnectionSettings -CredentialType "User" -Credential $Using:Cred -AccountID "456433"}
    Invoke-Command -Session $ses -ScriptBlock {New-User -DisplayName "pwsh test" -UserPrincipalName "[email protected]" -Password $Using:emailpass | Enable-ExchangeMailbox -Force -Confirm:$false }
    exit
}
# & $CommandName @Arguments
& $command
2
  • Remove the - in front of Add-im_email. Commented Jul 5, 2021 at 16:39
  • I am not sure what you mean by this so I tried both and it failed both times. I first tried removing - from the command like so intermedia_pwsh_functions.ps1 Add-in_email and that failed. I then tried to remove - from the actual function like so: function Addin_email then tried to run the script but I still got the same error regarding scriptblock error. Commented Jul 5, 2021 at 16:42

1 Answer 1

2

Before we get to the $command argument, there's something we need to fix.

You can only have one [CmdletBinding] attribute - and it's applied to the param(...) block as a whole - then you can use [Parameter] attributes on the individual parameter declarations to assign them to a parameter set:

[CmdletBinding(DefaultParameterSetName = 'email')]
param (
    $command,
    [Parameter(ParameterSetName = 'license')]
    $imcid,
    [Parameter(ParameterSetName = 'email')]
    $eoptenantID,
    [Parameter(ParameterSetName = 'email')]
    $emailresult,
    [Parameter(ParameterSetName = 'email')]
    $empw,
    [Parameter(ParameterSetName = 'email')]
    $emname,
)

<# function definition(s) go here#>

& $command

To pass the function name Add-im_Email as an argument to the $command parameter, invoke your script like this:

intermedia_pwsh_functions.ps1 -command Add-im_Email
Sign up to request clarification or add additional context in comments.

2 Comments

When I first tried it, it didn't work for me. however I was using the full path in the powershell interpreter. The moment I did .\intermedia_pwsh_functions.ps1 -command Add-im_Email instead it worked. (This was ofcourse after I changed my params to your suggestion.) Thank you!!
Sorry, I spoke too early. It worked as .\intermedia_pwsh_functions.ps1 -command Add-im_Email. but if I add any other params after, it doesn't work. such as '.\intermedia_pwsh_functions.ps1 -command Add-im_Email -empw "somepassword"`. I will make a new question about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.