2016-03-08 88 views
0

的功能,我預計今年的代碼運行Foo():在PowerShell中運行變量名

function Foo { 
    [Console]::WriteLine("Foo") 
} 
$Bar = [Console]::ReadLine() 
# If $Bar is Foo then it should do the function "Foo", right? 

$Bar() 

回答

0

功能遵循verb-noun模式,大多數.NET方法有一個PowerShell相當於稱爲cmdlet所以你的函數可以是:

function Get-Foo { 
    #[Console]::WriteLine('Foo') 
    Write-Host 'This is foo' 
} 

Get-Foo 

$Bar = Get-Foo 

$Bar 

Get-CommandGet-Help是你的朋友,試試吧。

+0

沒有完全按照我想要的去做,我認爲這是我的錯,因爲它太寬泛,我固定它是我真正想要做的。 –

+0

你用'[console] :: readline()的用戶輸入提供$ bar,它總是一個字符串.... foo'function'和foo'string'不是一回事 – Kiran