Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为什么我的PowerShell脚本会提示向阵列添加项目的权限?_Arrays_Powershell_Scripting - Fatal编程技术网

Arrays 为什么我的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..." $

我有一个包含以下摘录的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>"
  }
}
有人能告诉我为什么吗

总结/TL;博士:

  • 为什么我的函数在尝试向脚本中创建的数组添加项时会提示我在shell中执行此操作的权限

  • 对于我上面描述的自定义对象,它保存了我想在HTML表中显示的数据,在上面的代码摘录中,我试图访问它的错误是什么


  • 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]