簡體   English   中英

為什么我的PowerShell腳本提示您允許將項目添加到數組的權限?

[英]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>"
  }
}

因此,在上面稍微解釋一下文字牆;

  • $ RegistryEntries是哈希表的哈希表,頂級密鑰是計算機名稱,低級哈希表密鑰是在注冊表的“卸載”部分中找到的應用程序名稱。 “應用程序名稱”鍵的對應值是具有三個常規屬性的自定義PSObject,即:.Displayname,.DisplayVersion和.UninstallString。 (並非所有對象都具有這三個屬性,但是每個對象至少具有一個)。
  • 我希望通過此HTML表實現的是獲取某種“數據透視表”(參考數據透視表的Wikipedia條目,但並非完全如此),在這里我可以獲得Y軸上的應用程序名稱和計算機名稱。在X軸上以及在相交的所述計算機上的所述應用程序的版本號。

再次考慮到這一點,有人可以幫助我理解為什么我的腳本在運行時會提示我在外殼中向我授予將應用程序名稱添加到數組$ appnames的權限(腳本中的其他位置)以及對HTML進行相同的操作的原因。輸入到$行?

另一方面,我的$ RegistryEntries對象(即哈希表的哈希表)有點偏旁(甚至可能是題外話),由於某種原因,由於無法在以下兩行中進行訪問,因此無法訪問它:

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

誰能告訴我為什么?

總結/ TL; DR:

  1. 為什么在嘗試向腳本內創建的數組添加項目時我的函數提示我在外殼中執行此操作的權限?

  2. 上面已經描述過的自定義對象保存了要在HTML表中顯示的數據,在上面的代碼摘錄中嘗試訪問它時我做錯了什么?

PS:從某種意義上說,腳本起作用的原因是,如果我一直坐在shell中的所有提示中,一直打着A + Return鍵,那么我將得到所需類型的HTML表,但是會得到應用程序所在的所有單元格名稱與計算機名之間會出現“ Application N / A”。

我想您將$DebugPreference和/或VerbosePreference設置為Inquire ,這將在每次調用Write-DebugWrite-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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM