需要幫助對陣列和腳本塊進行故障排除或使用引數和函式可能會更好???
腳本目標:輕松更新要安裝的應用程式串列
下面出現錯誤。
' 在 C:\Temp\appinstall.ps1:7 char:10 $Firefox={
~ 賦值運算式無效。賦值運算子的輸入必須是能夠接受賦值的物件,例如變數或屬性。 CategoryInfo : ParserError: (:) [], ParseException FullyQualifiedErrorId : InvalidLeftHandSide '
Start-Transcript -Append c:\Deploy\log.txt
$ProgressPreference = 'SilentlyContinue';
#Change App Name, Source, MSI/EXE, Argument
$AppArray= (
$Firefox={
$App= "Firefox";
$App_source= "https://download.mozilla.org/?product=firefox-latest&os=win64&lang=en-US";
$destination = "c:\Deploy\$App.exe";
$Argument= "/S";
},
$Chrome=
{
$App= "Chrome";
$App_source= "https://dl.google.com/tag/s/defaultbrowser/edgedl/chrome/install/GoogleChromeStandaloneEnterprise64.msi";
$destination = "c:\Deploy\$App.exe";
$Argument= "/norestart","/qn";
}
)
$InstallScriptBlock=
{
$installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -Match "$App" });
$installed.displayname
if ($installed.displayname -Match $App) {
Write-Host "$software installed"
}else{
If ((Test-Path $destination) -eq $false) {
New-Item -ItemType File -Path $destination -Force
}
#install software
Invoke-WebRequest $App_source -OutFile $destination
Start-Process -FilePath "$destination" -ArgumentList "$Argument" -Wait
#Delete installer
Remove-Item -recurse "$destination"
}
}
ForEach ($Program in $AppArray) {Invoke-Command -ScriptBlock $InstallScriptBlock}
Stop-Transcript
uj5u.com熱心網友回復:
看起來您正在嘗試創建嵌套哈希表( @{ ... }),但您的語法有缺陷 - 請參閱鏈接檔案。
然而:
在您的情況下,創建一個哈希表陣列來迭代就足夠了
foreach無需使用單獨的腳本塊(
{ ... }) - 只需使用foreach回圈陳述句的主體。- 順便說一句:雖然
Invoke-Command用于腳本塊的本地呼叫有效,但通常沒有必要,因為&呼叫運算子會這樣做(例如$sb = { 'hi' }; & $sb)。的主要目的是在遠程機器上Invoke-Command執行腳本塊。
- 順便說一句:雖然
通常,您可以按原樣使用變數作為命令引數,而無需將它們包含在
"..."- 即使它們的值包含空格。例如,Write-Output $foo就足夠了,不需要Write-Output "$foo"
把它們放在一起:
# Create an array whose elements are hashtables.
$appArray = (
@{
App = ($thisApp = 'Firefox')
App_source = 'https://download.mozilla.org/?product=firefox-latest&os=win64&lang=en-US'
Destination = "c:\Deploy\$thisApp.exe"
Argument = '/S'
},
@{
App = ($thisApp = 'Chrome')
App_source = 'https://dl.google.com/tag/s/defaultbrowser/edgedl/chrome/install/GoogleChromeStandaloneEnterprise64.msi'
Destination = "c:\Deploy\$thisApp.exe"
Argument = '/norestart /qn'
}
)
foreach ($app in $appArray) {
# Note how $app.<key> is used to refer to the entries of the hashtable at hand,
# e.g. $app.App yields "Firefox" for the first hashtable.
$installed = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.DisplayName -Match $app.App }
$installed.displayname
if ($installed.displayname -Match $app.App) {
Write-Host "$($app.App) already installed."
}
else {
if ((Test-Path $app.Destination) -eq $false) {
New-Item -ItemType File -Path $app.Destination -Force
}
#install software
Invoke-WebRequest $app.App_source -OutFile $app.Destination
Start-Process -FilePath $app.Destination -ArgumentList $app.Argument -Wait
#Delete installer
Remove-Item -Recurse $app.Destination
}
}
筆記:
I've removed unnecessary
;and I've switched to using verbatim (single-quoted) strings ('...') when no string interpolation via expandable (double-quoted) strings ("...") is required, both for conceptual clarity and to avoid potentially unwanted expansions.Note the use of aux. variable
$thisAppin theAppkey, which allows referencing it in the laterDestinationkey, in an expandable string ("c:\Deploy\$thisApp.exe").- GitHub suggestion #13782 looks for a more elegant way to allow hashtable entries to reference one another.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/440676.html
上一篇:查找陣列中包含關鍵字的元素的索引
