Arrays 为什么我的PowerShell脚本会提示向阵列添加项目的权限?
我有一个包含以下摘录的PowerShell脚本:Arrays 为什么我的PowerShell脚本会提示向阵列添加项目的权限?,arrays,powershell,scripting,Arrays,Powershell,Scripting,我有一个包含以下摘录的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..." $
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>"
}
}
有人能告诉我为什么吗
总结/TL;博士:
PS:这个脚本的工作原理是,如果我一直坐在shell中的所有提示中,一直按+回车键,我将得到一个我想要的HTML表,但是应用程序名与计算机名之间的所有单元格都会显示“应用程序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]
它还在继续。我想这是标准,我没有碰过它。嗯,我想知道,因为你提到了提示,
Write Debug
和Write Verbose
可以根据$*首选项来执行。我只是设法在shell中重新创建了提示,这是在我得到下一行的Debug:
AKA之后发生的。这实际上指向$DebugPreference
或-Debug
参数。在调试过程中,我有时使用-Debug开关。我不知道每次代码通过调试语句时都会提示我。我们每天都会学到一些东西^^我在哪里可以了解到-Debug
开关的效果?正如在获取帮助*
中写什么来代替*
一样,我想我想要的答案是被告知/发现在cmdlet中添加-Debug
开关会使整个函数停止,并在每次遇到write Debug
语句时等待我的执行!但既然是你把我带到这里来的,我感谢你提供的答案=)。(我添加到数组中的每个地方都有一个Write Debug
,原因很明显,为什么我认为数组操作是罪魁祸首^^^)如果不能像您那样尝试代码,这些事情真的很难发现;)是的,当我8小时前第一次写这个问题的时候,这是我第一次尝试写一个问题,所以我想你可能对阅读我那200行的丑陋代码不感兴趣^^如果我在晚些时候看到你,一定要打声招呼,你会得到一个pastebin链接=停下来,200行肯定太多了,但是您看到问题所在的代码肯定会被欣赏;)哦,你的意思是来自shell的提示,不是吗,不是代码?我被你说的“你看到问题的代码”弄糊涂了,因为问题显然来自shell,而它起源于我的代码(好吧,200有点夸张,因为它包括帮助文档和更多,如果你感兴趣,这里是链接:这里是另一个我认为你可能会感兴趣的链接:)
Write-Debug ($RegistryEntries[$pc].Value[$app])
$currentApp = $RegistryEntries[$pc].Value[$app]