2014-09-19 37 views
0

我創建這個啓動腳本給我的系統信息和現在得到所有屬性的3倍時,我有機器上的3個分區:powershell wmi html複製對象 - 以及我如何添加與wmi驅動器號?

$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 

我發佈的所有腳本(的我確定我有很多mstakes的即時通訊新的PowerShell) 什麼解決方案的重複屬性在表輸出? 你能幫我在系統信息中添加驅動器號嗎? 爲腳本的任何升級將是有益的,像其他有用的系統信息...

回答

0

foreach循環運行在您的計算機中的每個邏輯磁盤(即分區/卷):

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

輸出應但是報告每個分區的大小(和可用空間)。屬性DeviceID將爲您提供分配給邏輯磁盤的驅動器盤符。

如果您想要物理磁盤的大小而不是分區的大小,您需要改爲查詢Win32_DiskDrive類。但請注意,此類不提供可用磁盤空間信息,因爲它不知道有關文件或其使用空間的任何信息。

+0

謝謝我將這個添加到腳本中 – Zman 2014-09-19 16:13:56