print 2函数输出在一个 - powershell中

时间:2016-09-25 05:21:12

标签: 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 already started"
    }
}

我想在一行中打印这两个函数的输出。

date=26 sept 2016;host=localhost;message=servicename already started

1 个答案:

答案 0 :(得分:0)

通过避免函数中的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)"