I have a PowerShell script with the following excerpt:
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>"
}
}
So, to explain the wall of text above a bit;
- $RegistryEntries is a Hashtable of Hashtables, with the top-level keys being computer names, and the low-level hashtable keys being application names found in the Uninstall part of the registry. The corresponding values to the Application-name-keys are custom PSObjects with three general properties: .Displayname, .DisplayVersion, and .UninstallString. (Not all objects have all of the three properties, but each object has at least one).
- What I hope to achieve with this HTML-table is to get some kind of “pivot-table” (ref. Wikipedia entry for Pivot Tables, but not quite), where I can get Application Names on the Y-axis, and computer names on the X-axis, and the Version number of said application on said computer where they intersect.
So again, with that in mind, could someone help me understand why my script when run prompts me in the shell for permission to add application names to the array $appnames (elsewhere in the script), as well as doing the same with the HTML input that is being put into $rows?
Another thing which is a bit on the side (maybe even off-topic), my $RegistryEntries object, the hashtable of hashtables, is for some reason not possible to access in the way I do it on the two following lines:
Write-Debug $RegistryEntries[$pc].Value[$app]
$currentApp = $RegistryEntries[$pc].Value[$app]
Would anyone be able to tell me why?
To sum up/TL;DR:
-
Why does my function when trying to add items to an array created inside the script prompt me for permission to do just this in the shell?
-
With the custom object I’ve described above holding the data I want to display in my HTML table, what am I doing wrong in trying to access it in the above code excerpt?
PS: The script works in the sense that if I sit throughout all the prompts I get in the shell, hitting A + Return all the time, I will get a HTML table of the kind that I want, but all the cells where an application name interstices a computername will say “Application N/A”.
I guess you have your
$DebugPreferenceand/orVerbosePreferenceset toInquirewhich will prompt every timeWrite-DebugorWrite-Verboseare called, respectively:You probably want to set them to
Continueinstead. Another source might be the-Debugswitch.Regarding your second question, it’s a little long to explain, but for arguments to commands you have to put such expressions in parentheses: