Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在单行中打印2个函数输出-powershell_Powershell - Fatal编程技术网

在单行中打印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
调用-将它们全部删除,然后重试(甚至可能在全新的会话中)