我在寫一組PowerShell腳本來監視各種文件夾的大小。我遇到了一個錯誤,我不知道是什麼原因造成的。Powershell函數傳遞太多參數
下面是代碼,與Write-Host
顯示什麼,我期待和什麼變量$ip
和$loc
實際上包含:
function getDriveLetter($ip) {
Write-Host $ip # prints: 192.168.10.10 myfolder1\myfolder2\
# expected: 192.168.10.10
switch($ip) {
"192.168.10.10" {return "E`$"; break}
"192.168.10.20" {return "D`$"; break}
default {"Unknown"; break}
}
}
function getFullPath($loc,$folder) {
Write-Host $loc # prints: 192.168.10.10 myfolder1\myfolder2\
# expected: 192.168.10.10
$drive = getDriveLetter("$loc")
$str = "\\$loc\$drive\DATA\$folder"
return $str
}
function testPath($loc,$folder) {
$mypath = getFullPath("$loc","$folder")
if (Test-Path $mypath) {
return $true
} else {
return $false
}
}
當我運行命令:
testPath("192.168.10.10","myfolder1\myfolder2\")
我越來越一個「假」的結果,但如果我運行:
Test-Path "\\192.168.10.10\E`$\DATA\myfolder1\myfolder2\"
該命令返回True(因爲它應該)。
我錯過了什麼?我試過強迫變量設置爲:
$mypath = getFullPath -loc "$loc" -folder "$folder"
但是沒有變化。如果它改變了任何東西,這是在Powershell版本4上。
這解決了這個問題,太感謝你了!我還會用其他建議更新腳本(以及其他一些其他的內容)。我希望Powershell支持帶括號的多個參數,我覺得空格分隔的參數看起來不那麼幹淨。 – Elmepo