为什么我的PowerShell脚本会提示向项目添加项目的权限?

时间:2012-07-05 10:40:28

标签: 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..."
    $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>"
  }
}

所以,要解释一下上面的文字墙;

  • $ RegistryEntries是Hashtables的Hashtable,顶级键是计算机名,低级哈希表键是在注册表的Uninstall部分中找到的应用程序名称。 Application-name-keys的相应值是具有三个常规属性的自定义PSObject:。Displayname,.DisplayVersion和.UninstallString。 (并非所有对象都具有所有三个属性,但每个对象至少有一个)。
  • 我希望通过这个HTML表获得某种“数据透视表”(参考数据表的维基百科条目,但不完全),我可以在Y轴上获取应用程序名称,和X轴上的计算机名称,以及所述应用程序在它们相交的所述计算机上的版本号。

再说一次,考虑到这一点,有人可以帮助我理解为什么我的脚本在运行时会提示我在shell中获取将应用程序名称添加到数组$ appnames(脚本中的其他位置)的权限,以及执行相同的操作将HTML输入放入$ rows?

另一个有点偏在一边的东西(甚至可能是偏离主题的),我的$ RegistryEntries对象,哈希表的哈希表,由于某种原因不能以我在以下两行中的方式访问:

Write-Debug $RegistryEntries[$pc].Value[$app]
$currentApp = $RegistryEntries[$pc].Value[$app]

有人能告诉我原因吗?

总结/ TL; DR:

  1. 为什么我的功能在尝试将项目添加到脚本中创建的数组时提示我允许在shell中执行此操作?

  2. 使用我上面描述的自定义对象来保存我想要在HTML表格中显示的数据,在上面的代码摘录中尝试访问它时我做错了什么?

  3. PS:脚本的工作原理是,如果我坐在所有提示中,我进入shell,一直按A + Return,我会得到一个我想要的HTML表,但是所有的单元格如果应用程序名称填入计算机名称,则会说“应用程序N / A”。

1 个答案:

答案 0 :(得分:1)

我猜您将$DebugPreference和/或VerbosePreference设置为Inquire,每次调用Write-DebugWrite-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]