Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function in a Powershell Write-Host statement

Tags:

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 

My functions prints "hello", in addition to a switch statement to translate 1 to 'Stopped', 2 to 'Start Pending', 4 to 'Running', etc... So I know it's not getting called in the first two cases.

Results:

*** 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
like image 846
NealWalters Avatar asked Mar 29 '13 16:03

NealWalters


People also ask

How do you call a function in a PowerShell script?

A function in PowerShell is declared with the function keyword followed by the function name and then an open and closing curly brace. The code that the function will execute is contained within those curly braces.

How do you call function in a script to another script in PowerShell?

How to call a function: The inline approach. The easiest way to work with a PowerShell function is to include it in the script. The term for this is an inline function. Wherever the code is for the function, it must go above the first line that calls the function.

What does Write-host do in PowerShell?

Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information stream. This enables the capture or suppression of data written using Write-Host while preserving backwards compatibility.


1 Answers

Try this:

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

This prints:

Hello
like image 82
mjolinor Avatar answered Sep 28 '22 22:09

mjolinor