我有一个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>"
}
}
所以,要解释一下上面的文字墙;
再说一次,考虑到这一点,有人可以帮助我理解为什么我的脚本在运行时会提示我在shell中获取将应用程序名称添加到数组$ appnames(脚本中的其他位置)的权限,以及执行相同的操作将HTML输入放入$ rows?
另一个有点偏在一边的东西(甚至可能是偏离主题的),我的$ RegistryEntries对象,哈希表的哈希表,由于某种原因不能以我在以下两行中的方式访问:
Write-Debug $RegistryEntries[$pc].Value[$app]
$currentApp = $RegistryEntries[$pc].Value[$app]
有人能告诉我原因吗?
总结/ TL; DR:
为什么我的功能在尝试将项目添加到脚本中创建的数组时提示我允许在shell中执行此操作?
使用我上面描述的自定义对象来保存我想要在HTML表格中显示的数据,在上面的代码摘录中尝试访问它时我做错了什么?
答案 0 :(得分:1)
我猜您将$DebugPreference
和/或VerbosePreference
设置为Inquire
,每次调用Write-Debug
或Write-Verbose
时都会提示:< / p>
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]