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
-in front ofAdd-im_email.-from the command like sointermedia_pwsh_functions.ps1 Add-in_emailand that failed. I then tried to remove-from the actual function like so:function Addin_emailthen tried to run the script but I still got the same error regarding scriptblock error.