【发布时间】:2011-06-29 11:59:38
【问题描述】:
我不确定我是完全疯了还是错过了什么,但我正在尝试做一些非常简单的事情,即将函数中的变量负载分开并将它们写出来。
你说“容易”?我会这么想的。但这里是我尝试编写一个 powehell 函数的一小部分摘录,以使我的其余代码更容易阅读。
$logtoFile=$false
function logEntry($entryType, $section, $message)
{
#The way I assumed woudl work
$logString1 = $(get-Date -format "yyyy-MM-dd hh:mm:ss") + "|" + $entryType + "|" + $section + "|" + $message
#Another way which should also work
$logString2 = "$(get-Date -format "yyyy-MM-dd hh:mm:ss")|$entryType|$section|$message"
#Proof that it is not the pipes causing issues
$logString3 = "$(get-Date -format "yyyy-MM-dd hh:mm:ss"),$entryType,$section,$message"
#another method I found. Not sure what the issue is there
$logString4 = $(get-Date -format "yyyy-MM-dd hh:mm:ss"), $entryType, $section, $message -join "|"
$WhatIwant = $(get-Date -format "yyyy-MM-dd hh:mm:ss") + "|0|moonin|plimsole"
if($script:logtoFile)
{
Add-Content $logFile $logString
}
else
{
write-host $logString1
write-host $logString2
write-host $logString3
write-host $logString4
write-host $whatIwant
}
}
logEntry(0,"moomin","plimsole")
输出:
2011-02-17 11:22:59|System.Object[]||
2011-02-17 11:22:59|0 moomin plimsole||
2011-02-17 11:22:59,0 moomin plimsole,,
2011-02-17 11:22:59|0 moomin plimsole||
2011-02-17 11:22:59|0|moonin|plimsole
四处搜索,我找到了几个替代方案,但似乎都没有产生正确的结果。我不确定整个“将所有变量集中在一起用空格分隔”到底是什么。
【问题讨论】:
标签: string powershell concatenation