$applicationArray = $filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe", $notepad = "C:\Program Files\Notepad \notepad .exe", $cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe", $adobe = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
foreach ($application in $applicationArray)
{
Test-path
if (Test-path)
{
Write-Host "Folder Exists"
}
else
{
Write-Host "Folder Does Not Exist"
}
}
我正在嘗試撰寫一個腳本來檢查是否使用“測驗路徑”命令安裝了上述程式。當我在單獨的“if”陳述句中運行它們時,它們都可以作業,但我希望回圈它,以便我可以壓縮腳本以盡可能高效。但是當我嘗試運行上述內容時,出現以下錯誤:
At C:\Users\Nicholas.McKinney\OneDrive - Brandmuscle\Documents\Scripts\Laptop_Setups.ps1:1 char:34
... filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe", $n ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
At C:\Users\Nicholas.McKinney\OneDrive - Brandmuscle\Documents\Scripts\Laptop_Setups.ps1:1 char:100
... a.exe", $notepad = "C:\Program Files\Notepad \notepad .exe", $cisco ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
At C:\Users\Nicholas.McKinney\OneDrive - Brandmuscle\Documents\Scripts\Laptop_Setups.ps1:1 char:153
... ", $cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mob ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : InvalidLeftHandSide
uj5u.com熱心網友回復:
看起來您正在嘗試創建哈希表,但語法不太正確。讓我們像這樣嘗試,
$apps = @{
filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe"
notepad = "C:\Program Files\Notepad \notepad .exe"
cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
adobe = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
}
要使用哈希表值,請遍歷其鍵并按鍵值訪問每個專案,如下所示,
foreach($k in $apps.keys) {
"Testing path for {0} -> {1}: {2}" -f $k, $apps[$k], $(test-path $apps[$k])
}
# output
Testing path for adobe -> C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe: False
Testing path for notepad -> C:\Program Files\Notepad \notepad .exe: True
Testing path for cisco -> C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe: False
Testing path for filezilla -> C:\Program Files\FileZilla FTP Client\filezilla.exe: False
uj5u.com熱心網友回復:
這不起作用:
$applicationArray = $filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe", $notepad = "C:\Program Files\Notepad \notepad .exe", $cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe", $adobe = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
你可以做一組檔案路徑來驗證,例如:
$paths = @(
"C:\Program Files\FileZilla FTP Client\filezilla.exe",
"C:\Program Files\Notepad \notepad .exe"
)
完成后,您可以遍歷陣列并運行 test-path foreach 元素,例如:
foreach ($path in $paths){
If (Test-path -path $path){
write-host "Folder Exists"
}
Else {
Write-Host "Folder Does Not Exist"
}
}
請參閱:https ://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.2
uj5u.com熱心網友回復:
您可以使用哈希表來提高可讀性并遍歷每個測驗路徑。
@{
filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe"
notepad = "C:\Program Files\Notepad \notepad .exe"
cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
adobe = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
}.GetEnumerator() | % {
if (Test-Path -LiteralPath $_.Value) {
'{0,-10} - Exists.' -f $_.Key
}
else {
'{0,-10} - Not Found.' -f $_.Key
}
}
# Results
adobe - Not Found.
notepad - Not Found.
cisco - Exists.
filezilla - Not Found.
我喜歡 vonPryz 的方法,所以我更新了我的方法來模仿他/她的方法。只是將條件陳述句合并到字串格式值中,本質上是相同的。
'{0,-10} - {1}' -f $_.Key, $(
if (Test-Path -LiteralPath $_.Value) { 'Exists' } else { 'Not Found' }
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/521946.html
標籤:数组电源外壳前锋脚本
