【问题标题】:powershell wmi to html duplicate objects- and how i add drive letters with wmi?powershell wmi to html重复对象-以及如何使用wmi添加驱动器号?
【发布时间】: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


    【解决方案1】:

    您的foreach 循环针对您计算机中的每个逻辑磁盘(即分区/卷)运行:

    $computerHDD = Get-WmiObject Win32_LogicalDisk -Filter drivetype=3 
    ...
    ForEach($HDD in $computerHDD){
        $compinfo += ...
    }
    

    不过,输出应该报告每个分区的大小(和可用空间)。属性DeviceID 将为您提供分配给逻辑磁盘的驱动器号。

    如果您想要物理磁盘的大小而不是分区的大小,则需要查询 Win32_DiskDrive 类。但是请注意,该类不提供可用磁盘空间信息,因为它不知道有关文件或它们使用的空间的任何信息。

    【讨论】:

    • 谢谢,我会把它添加到脚本中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多