在powershell中格式化输出

时间:2014-02-14 02:11:50

标签: powershell

我正在尝试编写一个PowerShell脚本并遇到谷歌似乎无法告诉我如何修复的问题。基本上,我有4个变量:

$myDuration = $myObj.Duration
$mySubject = $myObj.Subject
$myStart = $myObj.Start
$myLocation = $myObj.Location

我正在尝试发送正文为的电子邮件:

"Your meeting ({0}) will begin {1}. It will be held at {2} and last approximately {3}."   -f $mySubject,$myStart,$myLocation,$myDuration

现在,如果我只包含一个变量,它就可以了。例如,“您的会议{0}”-f $ myLocation正确输出存储在$ myLocation中的值。

当我包含多个时,我的每个变量的输出都是确切的字符串“System.Object []”。 (IE“你的会议(System.Object [])将开始System.Object []。它将保存在System.Object []并且最后大约是System.Object []。”)任何想法为什么当我包含更多时它会出错然后一个变量?从我在谷歌上读到的所有内容来看,应该没有问题。

感谢您的时间。编辑希望能更好地澄清我的问题。

1 个答案:

答案 0 :(得分:1)

我怀疑$myObj中的一个或多个字段不是字符串,不能轻易转换为字符串,而是抛弃格式化程序。看看这段代码:

$myDuration = "Duration";
$mySubject = "Subject";
$myStart = "Starttime";
$myLocation = "Location";

"Your meeting ({0}) will begin {1}. It will be held at {2} and last approximately {3}."   -f $mySubject,$myStart,$myLocation,$myDuration

$myObj = @{"Duration"=[int32]123456;"Subject"="Subject2";"Starttime"=(get-date);"Location"="Location2"};
$myDuration = $myObj.Duration;
$mySubject = $myObj.Subject;
$myStart = $myObj.Starttime;
$myLocation = $myObj.Location;
"Your meeting ({0}) will begin {1}. It will be held at {2} and last approximately {3}."   -f $mySubject,$myStart,$myLocation,$myDuration

我的输出是:

Your meeting (Subject) will begin Starttime. It will be held at Location and last approximately Duration.
Your meeting (Subject2) will begin 2/13/2014 10:30:39 PM. It will be held at Location2 and last approximately 123456.

这一切都按预期工作。