【发布时间】:2014-09-19 08:12:38
【问题描述】:
当我在机器上有 3 个分区时,我创建了这个启动脚本来向我发送系统信息并获取所有属性 3 次:
$hostname = $env:computername
$file1 = "\\**UNC***\$hostname.1.csv"
$file2 = "\\*****UNC*******\$hostname.2.csv"
$compinfo = @()
$computerSystem = get-wmiobject Win32_ComputerSystem
$computerBIOS = get-wmiobject Win32_BIOS
$computerOS = get-wmiobject Win32_OperatingSystem
$computerCPU = get-wmiobject Win32_Processor
$computerHDD = Get-WmiObject Win32_LogicalDisk -Filter drivetype=3
$colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IpEnabled = TRUE"
# Build objects
ForEach($HDD in $computerHDD){
$compinfo += New-Object PSObject -property @{
PCName = $computerSystem.Name
Manufacturer = $computerSystem.Manufacturer
Model = $computerSystem.Model
SerialNumber = $computerBIOS.SerialNumber
RAM = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)
HDDSize = "{0:N2}" -f ($HDD.Size/1GB)
HDDFree = "{0:P2}" -f ($HDD.FreeSpace/$HDD.Size)
CPU = $computerCPU.Name
OS = $computerOS.caption
SP = $computerOS.ServicePackMajorVersion
User = $computerSystem.UserName
BootTime = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
IP_Address = [string]$colItems.IpAddress
MAC_Address = [string]$colItems.MacAddress
Default_Gateway = [string]$colItems.DefaultIpGateway
DNS_Domain = $colItems.DNSDomain
DHCP_Enabled = $colItems.DHCPEnabled
}
}
#wmi for windows updates
$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$Searcher.QueryHistory(0, $historyCount) | Select-Object Title, Description, Date | Export-Csv $file2 -NoTypeInformation
$hotfix1 = Import-Csv $file1
$hotfix2 = Import-Csv $file2
#Styles:
#Updates Style
$style = @"
<h3>Installed Updates:</h3>
<style>
"TABLE{border: 1px solid black; border-collapse: collapse;}
TH {border: 1px solid black; background: #B0E0E0; padding: 5px; }
TD {border: 1px solid black; padding: 5px; }
</style>
"@
#services Style
$style2 = @"
<h3>Services Report:</h3>
<style>
"TABLE{border: 1px solid black; border-collapse: collapse;}
TH {border: 1px solid black; background: #B0E0E0; padding: 5px; }
TD {border: 1px solid black; padding: 5px; }
</style>
"@
# style for system info
$style4 = @"
<h3>System info:</h3>
<style>
"TABLE{border: 1px solid black; border-collapse: collapse;}
TH {border: 1px solid black; background: #B0E0E0; padding: 5px; }
TD {border: 1px solid black; padding: 5px; }
</style>
"@
#check to see if first time running script by the old file.
$oldfile = "\\******UNC****\$hostname.old.csv"
$TestPath = Test-Path $oldfile
If (!$TestPath) {
$text = "First time script on this machine.. cant get updates status"
}
Else
{
$text = "No updates are installed!"
}
#this style is when no updates found or first time script run.
$style3 = @"
<h3>Installed Updates:</h3>
<p>$text</p>
</style>
"@
$Compare = Compare-Object -ReferenceObject $hotfix2 -DifferenceObject $hotfix1 -Property Title, Description, Date | Select-Object Title, Description, Date
$Service = Get-Service | Select-Object Name, DisplayName, Status | Sort-Object status -Descending
$compinfo | select -Property HDDFree ,HDDSize ,Ram ,OS ,CPU ,SP ,IP_Address,Mac_Address ,BootTime ,DHCP_Enabled
#Send Mail:
$smtpServer = "*****"
$smtpFrom = "**********"
$smtpTo = "**************"
$messageSubject = "$hostname has been started!"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
If ($compare -eq $null) {
$Compare = ConvertTo-Html -Head $style3
$message.Body = $Compare
$message.Body += $Service | ConvertTo-Html -Head $style2
$message.Body += $cominfovar | ConvertTo-Html -As Table -Head $style4
}
Else
{
$message.Body = $Compare | ConvertTo-Html -Head $style
$message.Body += $Service | ConvertTo-Html -Head $style2
$message.Body += $cominfovar | ConvertTo-Html -As Table -Head $style4
}
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
#Remove Old Files
del "\\*****UNC******\$hostname.old.csv"
Rename-Item $file1 "\\**********UNC*********\$hostname.old.csv"
Rename-Item $file2 $file1
我发布了所有脚本(我确定我在 powershell 中有很多新的错误) 表输出中重复属性的解决方案是什么? 你能帮我在系统信息中添加驱动器号吗? 脚本的任何升级都会有所帮助,就像其他有用的系统信息一样...
【问题讨论】:
标签: html powershell wmi