Powershell-帮助解决CMDB问题-意见



最近我自学了Powershell。这是艰难的两周,我读了很多书,但我越来越好了。我在工作中有一些压力,需要帮助纠正CMDB。我们离真正的撤资/资产管理系统到位还有大约7个月的时间。我们现在有很多依赖Powershell的原因,我们正在努力在进入管理系统之前清理混乱。无论如何,我创建了一个脚本,为我们获取了很多信息。我们有大约3000个对象/个,我们需要尽可能多的信息。不管怎样,我创作了一个剧本。到目前为止,它运行良好,但我想听听专家的意见或任何建议。我觉得我在这方面做得很好,只有两周的经验,但我真的想知道别人怎么想。

我注意到一件事:带有IE9及以上版本的Windows7盒子不会返回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 “rootCIMV2ApplicationsMicrosoftIE” -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 "\$ComputerC$Users" -Exclude "*Service*","*admin*","*Public*","*ffodero*","*jgalli*","*jwalters*","*frochet*" | Sort-Object LastAccessTime -Descending | Select-Object Name -First 1).Name
                $loggedintime     = (Get-ChildItem -Path "\$ComputerC$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 "\$ComputerC$Documents and Settings" -Exclude "*Service*","*admin*","*Public*" | Sort-Object LastAccessTime -Descending | Select-Object Name -First 1).Name
                      $loggedintime     = (Get-ChildItem -Path "\$ComputerC$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:logsmother.csv -NoTypeInformation -Append
    } 

从Windows Vista开始,命名空间rootCIMV2ApplicationsMicrosoftIE已被删除(请参阅博客文章末尾的注释)。不过,您应该能够从注册表中读取版本号:

$hive = [UInt32]'0x80000002'
$key  = 'SOFTWAREMicrosoftInternet Explorer'
$reg = [WMIClass]"\$computerrootdefault:StdRegProv"
$ieVersion = $reg.GetStringValue($hive, $key, 'Version').sValue

最新更新