2013-02-22 34 views
1

PowerShell的設置變量,我不知道下面到底我做錯了,但代碼返回以下錯誤信息:從函數的結果

術語「identityTest未被識別爲cmdlet的名稱,功能,腳本文件或可操作的程序。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然後重試。

這裏是我的樣品/測試代碼:

#Bunch of Global vars 
$UrlToUse = identityTest 

function identityTest 
{ 
    $internalMatch = ".*inbound" 
    $externalMatch = ".*outbound" 
    $fqdn = "blahblah_inbound" 

    if ($fqdn -match $internalMatch) 
    { 
     return "http://in3" 
     Write-Host "Inbound Hit" 
    } 
    if ($fqdn -match $externalMatch) 
    { 
     return "http://out" 
     Write-Host "Outbond Hit" 
    } 
    else 
    { 
     return "http://default" 
     write-host "Default Hit" 
    } 
}  


function sampleTest 
{ 
    write-host "will upload to the following URL: + $UrlToUse 
} 

Write-Host $UrlToUse 

不知道如果我走的是正確的做法,但在這裏這就是我試圖完成。我打算將UrlToUse設置爲全局變量,具體取決於indetityTest函數中的if語句的結果,該語句將確定並返回正確的語句。從那裏我將在我的其他代碼中使用相同的全局變量。我創建的一個例子會在另一個函數中使用相同的var $ UrlToUse,在這種情況下,名稱爲sampleTest。

我不明白爲什麼這不起作用。我來自Perl背景,可能會令人困惑的事情如何在PowerShell中工作。任何提示指針等將真的很感激。

非常感謝!

+0

腳本文件是從上到下讀取的。每個對象/腳本/函數/等等。您必須在會話的早期或腳本中初始化您的參考。 – 2013-02-22 14:44:28

回答

1

在調用函數之前移動函數identityTest。像這樣:

function identityTest 
{ 
    $internalMatch = ".*inbound" 
    $externalMatch = ".*outbound" 
    $fqdn = "blahblah_inbound" 

    if ($fqdn -match $internalMatch) 
    { 
     return "http://in3" 
     Write-Host "Inbound Hit" 
    } 
    if ($fqdn -match $externalMatch) 
    { 
     return "http://out" 
     Write-Host "Outbond Hit" 
    } 
    else 
    { 
     return "http://default" 
     write-host "Default Hit" 
    } 
}  

#Bunch of Global vars 
$UrlToUse = identityTest 



function sampleTest 
{ 
    write-host "will upload to the following URL: + $UrlToUse" 
} 

Write-Host $UrlToUse 
+0

謝謝你的工作。沒有意識到函數順序的重要性。 – user1048209 2013-02-22 15:09:54