Powershell - 帮助解决我们的CMDB问题 - 意见?

时间:2013-08-30 01:49:09

标签: powershell cmdb

最近我自己学习了Powershell。这是一个艰难的2周和很多阅读,但我越来越好。我在工作上有一些压力来帮助纠正我们的CMDB。我们距离真正的Depolyment / Asset Management系统还有7个月的时间。我们现在有很多理由依赖Powershell,我们正试图在我们获得管理系统之前清理一团糟。无论如何,我创建了一个脚本,为我们提供了大量信息。我们有大约3000个对象/个人,我们需要尽可能多的信息。无论如何,我创建了一个脚本。到目前为止它运作良好,但我想要专家的意见或任何建议。我觉得我做了一个体面的工作,只用了两周的时间,但我真的很想知道其他人的想法。

我注意到的一件事:带有IE9和Up的Windows 7 Box不会返回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 个答案:

答案 0 :(得分:1)

命名空间root\CIMV2\Applications\MicrosoftIEremoved 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