如何在Powershell Write-Host语句中调用函数

时间:2013-03-29 16:04:50

标签: powershell

$BizTalkHelper = "d:\Scripts\BizTalkHelper.ps1"
.$BizTalkHelper # "dot source" the helper library.
Write-Host *** BEGIN ***

Write-Host $(Get-Date) " Desc:" {GetHostStateDesc 1 }
Write-Host $(Get-Date) " Desc:" GetHostStateDesc 2 

$result = GetHostStateDesc 1 
Write-Host $result 

我的函数打印“hello”,除了将1转换为'Stopped'的switch语句,2转换为'Start Pending',4转换为'Running'等等... 所以我知道在前两种情况下没有被调用。

结果:

*** BEGIN *** 
3/29/2013 11:03:34 AM  Desc: GetHostStateDesc 1 
3/29/2013 11:03:34 AM  Desc: GetHostStateDesc 2 
hello 
Function GetHostStateDesc  1 
Stopped

2 个答案:

答案 0 :(得分:8)

试试这个:

function SayHello {'Hello'}
write-host $(SayHello)

打印:

Hello

答案 1 :(得分:2)

看起来就是这样:

Write-Host $(Get-Date) " Desc:" $(GetHostStateDesc 1 )

我注意到$()周围的Get-Date语法,所以如果它是一个函数,我猜它会对我的函数起作用而且确实如此。