在单行中打印2个函数输出-powershell
我们可以在同一行中打印两个函数输出吗在单行中打印2个函数输出-powershell,powershell,Powershell,我们可以在同一行中打印两个函数输出吗 function dateHost { $date = get-date $host = "$env:compname" write-host"date=$date;hostname=$host;" } function service { if ($Service.Status -eq "running"){ Write-Host "message=$ServiceName service is alre
function dateHost {
$date = get-date
$host = "$env:compname"
write-host"date=$date;hostname=$host;"
}
function service {
if ($Service.Status -eq "running"){
Write-Host "message=$ServiceName service is already started"
}
}
我想在一行中打印这两个函数的输出
date=26 sept 2016;host=localhost;message=servicename already started
通过避免函数中的
Write Host
,而只返回字符串作为其输出,可以这样尝试。还请注意,您不能将var$host
分配给它,因为它指的是预定义的PowerShell主机,所以我将该名称更改为$hostname
function dateHost {
$date = get-date
$hostname = "$env:compname"
"date=$date;hostname=$hostname;"
}
function service{
if ($Service.Status -eq "running"){
"message=$ServiceName service is already started"
}
}
"$(dateHost)$(service)"
通过避免函数中的
Write Host
,而只返回字符串作为其输出,可以这样尝试。还请注意,您不能将var$host
分配给它,因为它指的是预定义的PowerShell主机,所以我将该名称更改为$hostname
function dateHost {
$date = get-date
$hostname = "$env:compname"
"date=$date;hostname=$hostname;"
}
function service{
if ($Service.Status -eq "running"){
"message=$ServiceName service is already started"
}
}
"$(dateHost)$(service)"
谢谢你的回复!此解决方案将仅打印datehost的输出。如果我写主机(dateHost+$(service)),将在两行中获得输出。我认为您的函数中仍有一些
Write Host
调用-将它们全部删除,然后重试(甚至可能在全新的会话中),感谢您的响应!此解决方案将仅打印datehost的输出。如果我写主机(dateHost+$(service)),将以两行不同的方式获得输出。我认为您的函数中仍有一些Write Host
调用-将它们全部删除,然后重试(甚至可能在全新的会话中)