1

I've built a script for dynamically changing an HTML template and sending it as an email, it uses a hashtable for the items to be changed due to a fluctuating number of items it may need to change (seemed a better option than an array)

The emailing and replacing of variables etc is all working correctly, where I'm having issues is when I try to call it from another script and pass in a hashtable with the replacements and to/fromsubject, I'm using Splatting as it seemed the best option, I have tried just passing in the hashtable as is, and tried start-job and Invoke-Expression

powershell version: 5.1

This is the code I'm using to call the emailing script

$TemplateReplacements = @{}
$TemplateReplacements.Add("EmailTo","[email protected]")
$TemplateReplacements.Add("EmailFrom","[email protected]")
$TemplateReplacements.Add("EmailSubject","Test Test Test")
$TemplateReplacements.Add("EmailTemplate","\ZeroLicenses.html")
$TemplateReplacements.Add("Heading","Heading replaced from main script")
$TemplateReplacements.Add("1","Variable1 repalced from main script")
$TemplateReplacements.Add("2","Variable2 replaced from main script")
$TemplateReplacements.Add("3","Variable3 replaced from main script")
$TemplateReplacements.Add("4","Variable4 replaced from main script")

$ScriptToRun = "C:\Users\User1\Desktop\Projects\Powershell\EmailVarReplace-Test\Test3.ps1"

$params = @{
                Replacements = $TemplateReplacements
            }

Invoke-Command $ScriptToRun @Params


#start-job $ScriptToRun @params

In the email script thats been called I have this at the top hich I believe is all I need for it to accept the parameter?

[CmdletBinding()]
param ($Replacements)

I've been stumped on this for ages and just keep getting the error

Invoke-Command : A parameter cannot be found that matches parameter name 'Replacements'.
At C:\Users\User1\Desktop\Projects\Powershell\EmailTest.ps1:19 char:29
+ Invoke-Command $ScriptToRun @Params
+                             ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

What I'm expecting to happen is it passes the hashtable into the script so it can get the variables for where to send the email to, what address from, subject plus the replacements of variables in the email

I'm building the email script as a standalone module that later just needs templates created adn scripts calling it with the replacements made

Any help would be very appreciated

Also ignore the hard-coded path etc as it's still a testing script :p

2
  • i think you need to use one of the "pass $Vars into the scriptblock" methods. i prefer to use the $Using: scope modifier, but you can also use the -ArgumentList method. Commented Nov 18, 2019 at 23:27
  • 1
    I don't think invoke-command even works with a script unless you specify a host. I don't even think splatting works with invoke-command scriptblocks. Passing arrays are hard enough. Commented Nov 19, 2019 at 2:23

1 Answer 1

1

Don't use Invoke-Command to invoke your script - Invoke-Command is virtually only ever useful in the context of remoting, i.e. if you use with the -ComputerName parameter to run a script of script block on one or more remote computers.

Instead, use &, the call operator, to invoke your script, then splatting should work as intended:

& $ScriptToRun @Params

As for what you tried:

If you try splatting via Invoke-Command, the splatting is applied to its parameters, not to the parameters of the script you're invoking.

Therefore, a -Replacements parameter is looked for among Invoke-Command's parameters rather than your script's - and there is no such parameter, which explains the error you got.

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

Comments

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.