2013-08-30 22 views
1

最近我自學了Powershell。這是一個艱難的2周和大量的閱讀,但我越來越好。我在工作上有一些壓力來幫助我們糾正我們的CMDB。我們距離擁有真正的解剖/資產管理系統約7個月。我們現在有很多依靠Powershell的理由,並且在我們獲得管理系統之前,我們試圖清理一團糟。無論如何,我創建了一個腳本,爲我們提供了大量信息。我們有大約3000件物品/件,我們需要儘可能多的信息。無論如何,我創建了一個腳本。到目前爲止,它運作良好,但我想從專家的意見或任何建議。我覺得我做了一個體面的工作,只用了兩週的時間,但我真的想知道別人的想法。Powershell - 幫助我們的CMDB問題 - 意見?

我注意到的一件事:帶有IE9和Windows的Windows 7盒子不會返回IE版本的值。有人知道爲什麼

請參閱下面我的代碼:

Set-QADPSSnapinSettings -defaultSizeLimit 0 


    $FullPCList = (Get-QADComputer -SearchRoot $ou | Sort Name | select -expand name) 

    foreach ($computer in $FullPCList) { 
    ping -n 2 $computer >$null 
    if($lastexitcode -eq 0) { $Online = "Yes" } else { $Online = "No" } 
    $PCInfo = (Get-WmiObject -ComputerName $computer -Class Win32_ComputerSystem -ErrorAction SilentlyContinue) 
    $WinInfo = (Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem -ErrorAction SilentlyContinue) 
    $ram = ((Get-WmiObject -ComputerName $computer -Class Win32_PhysicalMemory -ErrorAction SilentlyContinue | Measure-Object Capacity -Sum).Sum/1MB) 
    $bios = (Get-WmiObject -ComputerName $computer -Class Win32_Bios -ErrorAction SilentlyContinue) 
    $ie = (Get-Wmiobject -ComputerName $computer -namespace 「root\CIMV2\Applications\MicrosoftIE」 -query 「select version from MicrosoftIE_Summary」 -ErrorAction SilentlyContinue) 
    $freespace = ((Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk | Select Freespace | Measure-object Freespace -Sum).Sum/1GB) 


    #Start uptime check 
    $LastBootUpTime = $WinInfo.ConvertToDateTime($WinInfo.LastBootUpTime) 
    $Time = (Get-Date) - $LastBootUpTime 
    $formattime = '{0:00}:{1:00}:{2:00}' -f $Time.Days, $Time.Hours, $Time.Minutes 
    #End Uptime Check 

      if ($WinInfo.Caption -match "Windows 7") { 
       $name = (Get-ChildItem -Path "\\$Computer\C$\Users" -Exclude "*Service*","*admin*","*Public*","*ffodero*","*jgalli*","*jwalters*","*frochet*" | Sort-Object LastAccessTime -Descending | Select-Object Name -First 1).Name 
       $loggedintime  = (Get-ChildItem -Path "\\$Computer\C$\Users" -Exclude "*Service*","*admin*","*Public*","*ffodero*","*jgalli*" | Sort-Object LastAccessTime -Descending | Select-Object LastAccessTime -First 1).LastAccessTime 
      } 
      if ($WinInfo.Caption -match "Windows XP") { 
         $name     = (Get-ChildItem -Path "\\$Computer\C$\Documents and Settings" -Exclude "*Service*","*admin*","*Public*" | Sort-Object LastAccessTime -Descending | Select-Object Name -First 1).Name 
         $loggedintime  = (Get-ChildItem -Path "\\$Computer\C$\Documents and Settings" -Exclude "*Service*","*admin*","*Public*" | Sort-Object LastAccessTime -Descending | Select-Object LastAccessTime -First 1).LastAccessTime 
      } 

    $table = @{ 
      Model = $PCInfo.Model 
      IEVersion = $ie.Version 
      Serial = $Bios.SerialNumber 
      Memory = $ram 
      DriveFreeSpaceGB = $freespace 
      Manufacturer = $PCInfo.Manufacturer 
      OSName = $WinInfo.Caption 
      Computer = $computer 
      Uptime = $formattime 
      LastloggedinUser = $name 
      LastLoggedinDate = $loggedintime 
      LoggedOnDuringScan = $PCInfo.Username 
      ServicePack = $WinInfo.ServicePackMajorVersion 
      Online = $Online 
       } 
      New-Object PSObject -Property $table | Export-Csv C:\logs\mother.csv -NoTypeInformation -Append 
    } 

回答

1

命名空間root\CIMV2\Applications\MicrosoftIE一直removed starting with Windows Vista(見注在博客文章的末尾)。您應該能夠從註冊表中讀取版本號:

$hive = [UInt32]'0x80000002' 
$key = 'SOFTWARE\Microsoft\Internet Explorer' 

$reg = [WMIClass]"\\$computer\root\default:StdRegProv" 
$ieVersion = $reg.GetStringValue($hive, $key, 'Version').sValue 
+0

謝謝!我也遇到了另一個問題。 $ name和$ logggedintime都返回值,當沒有東西可以返回時。我假設它將上一個變量發佈到表中。我如何防止它做到這一點?只有當PC處於脫機狀態或RPC服務不可用時,纔會出現這種情況,因爲某些原因導致訪問被拒絕。 – Patrick

+0

變量可能會保留先前迭代中的值。在使用之前取消設置變量以防止該變量(例如'$ var = $ null'或'Remove-Variable var -EA SilentlyContinue')。 –