[英]Why does my PowerShell script prompt for permission to add items to arrays?
我有一個具有以下摘錄的PowerShell腳本:
foreach ($pc in $ComputerName) {
$appnames = $appnames | Sort-Object
Write-Debug "Number of entries in `$appnames = $($appnames.count)"
if ($AsHTML) {#Switch Parameter
Write-Verbose "Generating HTML Report..."
$th = "<TR><TH>Application Name</TH>" #Create Top header
foreach ($pc in $ComputerName) {
$th += "<TH>$pc</TH>" #Another header for each pc
}
$th += "</TR>" #Header finished
$rows = ""
foreach ($app in $appnames) {
$rows += "<TR><TH>$app</TH>"
foreach ($pc in $ComputerName) {
Write-Debug $RegistryEntries[$pc].Value[$app]
$currentApp = $RegistryEntries[$pc].Value[$app]
if ($currentApp) {
if ($currentApp.DisplayVersion) {
$status = $currentApp.DisplayVersion
} else {
$status = "Version-nr. N/A"
}
} else {
$status = "Application N/A"
}
$rows += "<TD>$status</TD>" #Intersection cell for each pc
}
$rows += "</TR>"
}
Write-Verbose "Finishing html report..."
$html = "
<html>
<head>
<style>
body { background-color:#FFFFCC;
font-family:Tahoma;
font-size:12pt; }
td, th { border:1px solid #000033;
border-collapse:collapse; }
th { color:white;
background-color:#000033; }
table, tr, td, th { padding: 0px; margin: 0px }
table { margin-left:10px; }
</style>
<Title>Application versions Report</Title>
</head>
<body>
<table>
$th
$rows
</table>
</body>
</html>"
}
}
因此,在上面稍微解釋一下文字牆;
再次考慮到這一點,有人可以幫助我理解為什么我的腳本在運行時會提示我在外殼中向我授予將應用程序名稱添加到數組$ appnames的權限(腳本中的其他位置)以及對HTML進行相同的操作的原因。輸入到$行?
另一方面,我的$ RegistryEntries對象(即哈希表的哈希表)有點偏旁(甚至可能是題外話),由於某種原因,由於無法在以下兩行中進行訪問,因此無法訪問它:
Write-Debug $RegistryEntries[$pc].Value[$app]
$currentApp = $RegistryEntries[$pc].Value[$app]
誰能告訴我為什么?
總結/ TL; DR:
為什么在嘗試向腳本內創建的數組添加項目時我的函數提示我在外殼中執行此操作的權限?
上面已經描述過的自定義對象保存了要在HTML表中顯示的數據,在上面的代碼摘錄中嘗試訪問它時我做錯了什么?
PS:從某種意義上說,腳本起作用的原因是,如果我一直坐在shell中的所有提示中,一直打着A + Return鍵,那么我將得到所需類型的HTML表,但是會得到應用程序所在的所有單元格名稱與計算機名之間會出現“ Application N / A”。
我想您將$DebugPreference
和/或VerbosePreference
設置為Inquire
,這將在每次調用Write-Debug
或Write-Verbose
分別提示:
Confirm
Continue with this operation?
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"):
您可能希望將它們設置為Continue
。 另一個來源可能是-Debug
開關。
關於第二個問題,要解釋的時間有點長,但是對於命令的參數,您必須將此類表達式放在括號中:
Write-Debug ($RegistryEntries[$pc].Value[$app])
$currentApp = $RegistryEntries[$pc].Value[$app]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.