我们正在尝试为我们的脚本创建输入文件,而不是使用此处的字符串(@"E-mail text"@
)在代码内对电子邮件的文本进行硬编码。然而,似乎很难填充像$MyVariable
这样不简单和简单但需要像$MyVariable.User.Name
那样解决的变量。展开文件中的变量
无论如何,下面的代码演示了这个问题。
HTML输入文件
Dear $($Object.User.GivenName)
Thank you for joining the program.
代码
$SamAccountNameManager = 'Mike'
$SamAccountNameUser = 'Bob'
$File = 'C:\Test.html'
$Object = [PSCustomObject]@{
User = Get-ADUser $SamAccountNameUser -Properties GivenName
Manager = Get-ADUser $SamAccountNameManager -Properties GivenName
}
$Template = Get-Content $File
$ExecutionContext.InvokeCommand.ExpandString($Template)
错误
Exception calling "ExpandString" with "1" argument(s): "Object reference not set to an instance of an obj
ect."
怎么可能使用VAR在文本文件中输入$Object.User.GivenName
并使其填充正确的值?
我无法从给定的(测试PowerShell中的例子重现错误2和5)。 –
试了一遍,只是为了确定,仍然是同样的错误。在Windows Server 2012上使用PowerShell 4.0。 – DarkLite1