好的,所以我有我的第一个PowerShell脚本,它的工作原理。输出仅到一个txt文件,很平淡。
完全对其进行了重新设计,将所有结果放入一个数组中。
请注意,您可能是一种更好的方法来做到这一点,或者我可能提出了太多的代码,因此受到了建议。...
我的最终目标是只有2行的HTML ....项目和结果
这是在将获得注册表设置,服务启动类型和本地ACCT状态的机器上运行的。
我只是无法弄清楚如何做一个桌子并循环在阵列中。
感谢您的帮助,因为它非常感谢!!!
# Static array of registry keys
$RegKeys = @("DisableNotificationCenter","AutoConfiURL","HibernateEnabled","HideSCAHealth","NoDriveTypeAutoRun","TurnOffSidebar","EnableBaloonTips","UseDomainNameDevolution","DomainNameDevolutionlevel","*.one.ads","*","SearchOrderConfig","NoAutoRebootWithLoggedOnUsers","DisabledComponents","fAllowToGetHelp","fDenyTSConnections","EnableLUA","dontdisplaylastusername")
#Static array of service names
$Services = @("LanmanServer","MPSSVC","WinDefend","WSCSVC","TRKWKS","NAPAGENT","WUAUSERV")
#Static array of users
$Users = @("Admin","Guest")
#Registry Keys
$dnc = 'HKCU:SoftwarePoliciesMicrosoftWindowsExplorer'
if (Test-Path $dnc) {$dnc = (Get-ItemProperty -Path "HKCU:SoftwarePoliciesMicrosoftWindowsExplorer").DisableNotificationCenter}
else {$dnc = "Key not Found"}
$acu = 'HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings'
if (Test-Path $acu) {$acu = (Get-ItemProperty -Path "HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings").AutoConfigURL}
else {$acu = "Key not Found"}
$he = 'HKLM:SYSTEMCurrentControlSetControlPower'
if (Test-Path $he) {$he = (Get-ItemProperty -Path "HKLM:SYSTEMCurrentControlSetControlPower").HibernateEnabled}
else {$he = "Key not Found"}
$hscah = 'HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesExplorer'
if (Test-Path $hscah) {$hscah = (Get-ItemProperty -Path "HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesExplorer").HideSCAHealth}
else {$hscah = "Key not Found"}
$ndtar = 'HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesExplorer'
if (Test-Path $ndtar) {$ndtar = (Get-ItemProperty -Path "HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesExplorer").NoDriveTypeAutoRun}
else {$ndtar = "Key not Found"}
$tos = 'HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesWindowsSidebar'
if (Test-Path $tos) {$tos = (Get-ItemProperty -Path "HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesWindowsSidebar").TurnOffSidebar}
else {$tos = "Key not Found"}
$ebt = 'HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesExplorerAdvanced'
if (Test-Path $ebt) {$ebt = (Get-ItemProperty -Path "HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesExplorerAdvanced").EnableBaloonTips}
else {$ebt = "Key not Found"}
$udnd = 'HKLM:SOFTWAREPoliciesMicrosoftWindows NTDNSClient'
if (Test-Path $udnd) {$udnd = (Get-ItemProperty -Path "HKLM:SOFTWAREPoliciesMicrosoftWindows NTDNSClient").UseDomainNameDevolution}
else {$udnd = "Key not Found"}
$dndl = 'HKLM:SYSTEMCURRENTCONTROLSETSERVICESDnscacheParameters'
if (Test-Path $dndl) {$dndl = (Get-ItemProperty -Path "HKLM:SYSTEMCURRENTCONTROLSETSERVICESDnscacheParameters").DomainNameDevolutionlevel}
else {$dndl = "Key not Found"}
$oads = 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionInternet SettingsZoneMapDomainsbms.com*.one.ads'
if (Test-Path $oads) {$oads = (Get-ItemProperty -Path "HKLM:SOFTWAREMicrosoftWindowsCurrentVersionInternet SettingsZoneMapDomainsbms.com*.one.ads")."*"}
else {$oads = "Key not Found"}
$ads = 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionInternet SettingsZoneMapDomainsbms.com*.one.ads'
if (Test-Path $ads) {$ads = (Get-ItemProperty -Path "HKLM:SOFTWAREMicrosoftWindowsCurrentVersionInternet SettingsZoneMapDomainsbms.com")."*"}
else {$ads = "Key not Found"}
$soc = 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionDriverSearching'
if (Test-Path $soc) {$soc = (Get-ItemProperty -Path "HKLM:SOFTWAREMicrosoftWindowsCurrentVersionDriverSearching").SearchOrderConfig}
else {$soc = "Key not Found"}
$narwlou = 'HKLM:SOFTWAREPoliciesMicrosoftWindowsWindowsUpdateAU'
if (Test-Path $narwlou) {$narwlou = (Get-ItemProperty -Path "HKLM:SOFTWAREPoliciesMicrosoftWindowsWindowsUpdateAU").NoAutoRebootWithLoggedOnUsers}
else {$narwlou = "Key not Found"}
$dc = 'HKLM:SYSTEMCurrentControlSetservicesTCPIP6Parameters'
if (Test-Path $dc) {$dc = (Get-ItemProperty -Path "HKLM:SYSTEMCurrentControlSetservicesTCPIP6Parameters").DisabledComponents}
else {$dc = "Key not Found"}
$atgh = 'HKLM:SYSTEMCurrentControlSetControlRemote Assistance'
if (Test-Path $atgh) {$atgh = (Get-ItemProperty -Path "HKLM:SYSTEMCurrentControlSetControlRemote Assistance").fAllowToGetHelp}
else {$atgh = "Key not Found"}
$dtsc = 'HKLM:SOFTWAREPoliciesMicrosoftWindows NTTerminal Services'
if (Test-Path $dtsc) {$dtsc = (Get-ItemProperty -Path "HKLM:SOFTWAREPoliciesMicrosoftWindows NTTerminal Services").fDenyTSConnections}
else {$dtsc = "Key not Found"}
$elua = 'HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesSystem'
if (Test-Path $elua) {$elua = (Get-ItemProperty -Path "HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesSystem").EnableLUA}
else {$elua = "Key not Found"}
$ddlun = 'HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesSystem'
if (Test-Path $ddlun) {$ddlun = (Get-ItemProperty -Path "HKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesSystem").dontdisplaylastusername}
else {$ddlun = "Key not Found"}
#Services
$ls = (Get-WmiObject Win32_Service -filter "Name='LanmanServer'").StartMode
$mpssvc = (Get-WmiObject Win32_Service -filter "Name='MPSSVC'").StartMode
$wd = (Get-WmiObject Win32_Service -filter "Name='WinDefend'").StartMode
$wscsvc = (Get-WmiObject Win32_Service -filter "Name='WSCSVC'").StartMode
$trkwks = (Get-WmiObject Win32_Service -filter "Name='TRKWKS'").StartMode
$napagent = (Get-WmiObject Win32_Service -filter "Name='NAPAGENT'").StartMode
$wuauserv = (Get-WmiObject Win32_Service -filter "Name='WUAUSERV'").StartMode
#Local Accounts
$Adm = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True' AND Name='Administrator'"
$Admin = $Adm.Disabled
$Gu = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True' AND Name='Guest'"
$Guest = $Gu.Disabled
#Make individual arrays from each queried information
$RegValues = @($dnc,$acu,$he,$hscah,$ndtar,$tos,$ebt,$udnd,$dndl,$oads,$ads,$soc,$narwlou,$dc,$atgh,$dtsc,$elua,$ddlun)
$ServiceValues = @($ls,$mpssvc,$wd,$wscsvc,$trkwks,$napagent,$wuauserv)
$UsersValues = @($Admin,$Guest)
#Make array of all keys
$RegAll = @($RegKeys[0], $RegValues[0],$RegKeys[1], $RegValues[1],$RegKeys[2], $RegValues[2],$RegKeys[3], $RegValues[3], $RegKeys[4], $RegValues[4], $RegKeys[5], $RegValues[5]
$RegKeys[6], $RegValues[6], $RegKeys[7], $RegValues[7], $RegKeys[8], $RegValues[8], $RegKeys[9], $RegValues[9], $RegKeys[10], $RegValues[10]
$RegKeys[11], $RegValues[11], $RegKeys[12], $RegValues[12], $RegKeys[13], $RegValues[13], $RegKeys[14], $RegValues[14], $RegKeys[15], $RegValues[15]
$RegKeys[16], $RegValues[16], $RegKeys[17], $RegValues[17], $RegKeys[18], $RegValues[18], $Services[0], $ServiceValues[0], $Services[1], $ServiceValues[1]
, $Services[2], $ServiceValues[2], $Services[3], $ServiceValues[3], $Services[4], $ServiceValues[4], $Services[5], $ServiceValues[5], $Services[6], $ServiceValues[6],
$Users[0], $UsersValues[0], $Users[1], $UsersValues[1])
#output to html
$RegAll # | Select @{label='Item';expression={$_}} | ConvertTo-HTML -Fragment -Property 'Item' |Out-File c:Scripts.html
这是我在脚本中使用的示例。您可以在脚本上贴上它。询问您何时需要帮助。
$YourArray = @()
#Object one for the array
$test1 = New-Object –TypeName PSObject
$test1 | Add-Member –MemberType NoteProperty –Name Propert_1 –Value "Example1"
$test1 | Add-Member –MemberType NoteProperty –Name Propert_2 –Value "Example2"
$test1 | Add-Member -MemberType NoteProperty -Name Propert_3 -Value "Example3"
#Object two for the array
$test2 = New-Object –TypeName PSObject
$test2 | Add-Member –MemberType NoteProperty –Name Propert_1 –Value "Example_2_1"
$test2 | Add-Member –MemberType NoteProperty –Name Propert_2 –Value "Example_2_2"
$test2 | Add-Member -MemberType NoteProperty -Name Propert_3 -Value "Example_2_3"
$YourArray = $test1,$test2
$beginning = {
#html code the format of the table
@'
<html>
<head>
<title>Report</title>
<STYLE type="text/css">
BODY{background-color:#b0c4de;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{font-family:SegoeUI, sans-serif; font-size:15; border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#778899}
TD{font-family:Consolas, sans-serif; font-size:12; border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
tr:nth-child(odd) { background-color:#d3d3d3;}
tr:nth-child(even) { background-color:white;}
</STYLE>
</head>
<h1>Stackoverflow example</h1>
<table>
<tr><th>Propert_1</th><th>Propert_2</th><th>Propert_3</th></tr>
'@
}
#Mapping between Property and table
$process = {
$Propert_1 = $_.Propert_1
$Propert_2 = $_.Propert_2
$Propert_3 = $_.Propert_3
'<tr>'
'<td bgcolor="#33CC33">{0}</td>' -f $Propert_1
'<td bgcolor="#FFFFFF">{0}</td>' -f $Propert_2
'<td bgcolor="#FFFFFF">{0}</td>' -f $Propert_3
'</tr>'
}
$end = {
@'
</table>
</html>
</body>
'@
}
#Export the array in a html sheet
$YourArray | ForEach-Object -Begin $beginning -Process $process -End $end | Out-File -FilePath "U:Export_Report.html" -Encoding utf8