Powershell管理系列(三十六)PowerShell操作之统计域内计算机硬件资产

-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包,QQ:185426445.电话18666943750

客户端需设置防火墙,[注意要先设置管理模版防火墙设置,否则将会覆盖默认组策略的高级防火墙安全设置]

1、允许远程管理,设置如下,启用windows防火墙:允许入站管理程序

参考链接:http://908174.blog.51cto.com/898174/1175525

clip_image002

clip_image004

2、 允许远程桌面

clip_image006

clip_image008

开启远程桌面。域组策略,“计算机配置”—>“策略”—>”管理模板“—>”Windows组件”—>”远程桌面服务“—>“远程桌面会话主机”—>”连接“,允许用户通过使用远程桌面服务进行远程连接,状态改为“已启用”。

wKiom1g8K-jAv7SZAAF0eQ2ALhA142.png-wh_50

3、允许ping,打开组策略,高级安全防火墙设置,入站规则

clip_image010

4、选择ICMPv4和ICMPv6

clip_image012

5、点击下一步完成

clip_image014

6、允许Powershell远程管理

具体设置参考链接:

http://yuntcloud.blog.51cto.com/1173839/1790701

脚本如下:

#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平 QQ:185426445 联系方式:18666943750 欢迎技术交流和提出修改意见
Import-Module activedirectory                                                   
#导入其中的AD 模块
$computeraccount=Get-ADComputer -Filter * -Properties * |?{($_.OperatingSystem -ne $null) -and ($_.enabled) -and ($_.IPv4Address -ne $null) }|select -ExpandProperty name         
#获取当前AD 计算机中的所有机器NETBIOS名称,排除禁用的,无操作系统类型、没有IP的
$allcomputername=@()                                                            
#定义所有计算机的初始空值
foreach ($currentcomputename in $computeraccount)                               
#根据计算机对象进行轮询
      { 
        if (Test-NetConnection $currentcomputename|select -ExpandProperty PingSucceeded) `
        #测试计算机是否可以ping通,如果可以ping通,则继续执行
        {
        $currentcomputename+"正测试IP连通性..."   
        $currentname= Get-ADComputer -Identity $currentcomputename|select -ExpandProperty name         
        #获取机器的NETBIOS名称       
        $currentuser=(Get-WmiObject Win32_Process -Filter 'Name="explorer.exe"' -ComputerName $currentcomputename).getOwner() | Select -ExpandProperty User
        #获取机器的当前登录用户 
        $currentoperatingsystem= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystem          
        #获取机器的操作系统版本  
        $currentoperatingsystemsp= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystemServicePack          
        #获取机器的操作系统SP版本                                   
        $currentclass= Get-WmiObject -class Win32_BIOS -computername $currentcomputename -namespace "root\cimv2" |select -ExpandProperty SerialNumber        
        #通过获取WMI中的bios 类获取到机器相应的序列号,存放在BIOS的SN
        $currentIP=Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true |select -ExpandProperty IPAddress -First 1 |?{$_ -notlike "*:*" -and $_ -notlike "169*"}   
        #通过获取WMI中的IPV4地址      
        $currentMAC= Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true -Property * |?{$_.IPAddress -match $currentIP} |select -ExpandProperty macaddress -First 1 
        #通过获取WMI中的MAC地址 
        $currentdiskSN=  Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename |select -First 1 -ExpandProperty Model        
        #通过获取WMI中的硬盘BIOS序列号
        $currentpcmodel=  Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty Model        
        #通过获取WMI中的计算机类型       
        $currentmemory=  (Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty TotalPhysicalMemory)/1gb -as [int]       
        #通过获取WMI中的计算机内存   
        $currentharddisk=  (Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename|select -First 1 -ExpandProperty size)/1gb  -as [int]      
        #通过获取WMI中的硬盘大小   
        $currentdiskcfreesize=  ((Get-WMIObject Win32_LogicalDisk -ComputerName $currentcomputename | ? { $_.deviceid -match "c" }).freespace)/1GB -as [int]      
        #通过获取WMI中的C盘可用空间大小                       
        $computerproperty=New-Object  psobject                                                                                                               
        #定义一个新PS 对象
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机名" -Value  $currentname                                                        
        #为新的对象定义计算机名称属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "登录用户名" -Value  $currentuser                                                        
        #为新的对象定义计算机当前登录用户名属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "主机序列号" -Value $currentclass                                                          
        #为计算机对象定义序列号属性   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "IP地址" -Value $currentip                                                          
        #为计算机对象定义IP地址属性  
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "MAC地址" -Value $currentMAC                                                          
        #为计算机对象定义MAC地址属性 
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "硬盘序列号" -Value $currentdisksn                                                          
        #为计算机对象定义硬盘序列号属性  
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "操作系统版本" -Value $currentoperatingsystem                                                          
        #为计算机对象定义操作系统版本   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "操作系统SP版本" -Value $currentoperatingsystemsp                                                          
        #为计算机对象定义操作系统SP版本   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机类型" -Value $currentpcmodel                                                          
        #为计算机对象定义计算机类型 
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机内存大小(GB)" -Value $currentmemory                                                         
        #为计算机对象定义计算机内存属性                                                               
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机硬盘大小(GB)" -Value $currentharddisk                                                         
        #为计算机对象定义计算机硬盘属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "C盘可用空间大小(GB)" -Value $currentdiskcfreesize                                                         
        #为计算机对象定义计算机C盘可用空间属性                                                                       
        $allcomputername=$allcomputername+$computerproperty                                                                                                  
        #根据对象的轮询将当前对象的属性加入到哈希数组中             
        }      
      }  
        if(!(Test-Path C:\统计计算机资产 -pathType container))
        {
        New-Item c:\统计计算机资产 -type directory
        }
        else {"C:\统计计算机资产文件夹已存在,不需要在创建"}
        #C盘下创建文件夹统计计算机资产,用于统一存放每天的生成的CSV文件            
        $tmplogfile="c:"+"\统计计算机资产\"+$(get-date -Format "yyyy-MM-dd")+".csv"                                                                                         
        #定义输出文件的路径和文件格式
        $allcomputername|Sort-Object IP地址 -Descending | Export-Csv -Encoding default -NoTypeInformation -Path $tmplogfile                                                                  
        #将数据导出为csv 文件,我们直接通过CSV 文件来获取希望拿到的信息        
        $UserName = "test@yuntcloud.com"      #定义发送账户名称
        $Password = ConvertTo-SecureString "123456" -AsPlainText –Force
        $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
        Send-MailMessage -From "test@yuntcloud.com" -To "zhouping@yuntcloud.com" -Subject "计算机硬件信息汇总" -Credential $cred -SmtpServer "mail.yuntcloud.com" -Attachments $tmplogfile -Encoding ([System.Text.Encoding]::UTF8)

思路之一:

$a=""|select "计算机类型","计算机名"
$a.计算机类型+="a"
$a.计算机名+="b"
$a

输出结果为:

wKioL1gqbEaRp9nRAACRAlw6A_8050.png-wh_50

修改后的powershell如下:

#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平 QQ:185426445 联系方式:18666943750 欢迎技术交流和提出修改意见
Import-Module activedirectory                                                   
#导入其中的AD 模块
$computeraccount=Get-ADComputer -Filter * -Properties * |?{($_.OperatingSystem -ne $null) -and ($_.enabled) -and ($_.IPv4Address -ne $null) }|select -ExpandProperty name         
#获取当前AD 计算机中的所有机器NETBIOS名称,排除禁用的,无操作系统类型、没有IP的
$allcomputername=@()                                                            
#定义所有计算机的初始空值
foreach ($currentcomputename in $computeraccount)                               
#根据计算机对象进行轮询
      { 
        if (Test-NetConnection $currentcomputename|select -ExpandProperty PingSucceeded) `
        #测试计算机是否可以ping通,如果可以ping通,则继续执行
        {
        $currentcomputename+"正测试IP连通性..."   
        $currentname= Get-ADComputer -Identity $currentcomputename|select -ExpandProperty name         
        #获取机器的NETBIOS名称       
        $currentuser=(Get-WmiObject Win32_Process -Filter 'Name="explorer.exe"' -ComputerName $currentcomputename).getOwner() | Select -ExpandProperty User
        #获取机器的当前登录用户 
        $currentoperatingsystem= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystem          
        #获取机器的操作系统版本  
        $currentoperatingsystemsp= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystemServicePack          
        #获取机器的操作系统SP版本                                   
        $currentclass= Get-WmiObject -class Win32_BIOS -computername $currentcomputename -namespace "root\cimv2" |select -ExpandProperty SerialNumber        
        #通过获取WMI中的bios 类获取到机器相应的序列号,存放在BIOS的SN
        $currentIP= Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true |select -ExpandProperty IPAddress -First 1 |?{$_ -notlike "*:*" -and $_ -notlike "169*"}   
        #通过获取WMI中的IPV4地址
        $currentMAC= Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true -Property * |?{$_.IPAddress -match $currentIP} |select -ExpandProperty macaddress -First 1 
        #通过获取WMI中的MAC地址       
        $currentdiskSN=  Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename |select -First 1 -ExpandProperty Model        
        #通过获取WMI中的硬盘BIOS序列号
        $currentpcmodel=  Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty Model        
        #通过获取WMI中的计算机类型       
        $currentmemory=  (Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty TotalPhysicalMemory)/1gb -as [int]       
        #通过获取WMI中的计算机内存   
        $currentharddisk=  (Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename|select -First 1 -ExpandProperty size)/1gb  -as [int]      
        #通过获取WMI中的硬盘大小   
        $currentdiskcfreesize=  ((Get-WMIObject Win32_LogicalDisk -ComputerName $currentcomputename | ? { $_.deviceid -match "c" }).freespace)/1GB -as [int]      
        #通过获取WMI中的C盘可用空间大小                 
        $allcomputername1=""|select "计算机名","登录用户名","主机序列号","IP地址","MAC地址","硬盘序列号","操作系统版本","操作系统SP版本","计算机类型","计算机内存大小","计算机硬盘大小","C盘可用空间大小"
        $allcomputername1.计算机名+=$currentname
        $allcomputername1.登录用户名+=$currentuser
        $allcomputername1.主机序列号+=$currentclass
        $allcomputername1.IP地址+=$currentip
        $allcomputername1.MAC地址+=$currentmac
        $allcomputername1.硬盘序列号+=$currentdisksn
        $allcomputername1.操作系统版本+=$currentoperatingsystem
        $allcomputername1.操作系统SP版本+=$currentoperatingsystemsp
        $allcomputername1.计算机类型+=$currentpcmodel
        $allcomputername1.计算机内存大小+=$currentmemory
        $allcomputername1.计算机硬盘大小+=$currentharddisk
        $allcomputername1.C盘可用空间大小+=$currentdiskcfreesize
        $allcomputername+=$allcomputername1                                       
        }      
      }  
        if(!(Test-Path C:\统计计算机资产 -pathType container))
        {
        New-Item c:\统计计算机资产 -type directory
        }
        else {"C:\统计计算机资产文件夹已存在,不需要在创建"}
        #C盘下创建文件夹统计计算机资产,用于统一存放每天的生成的CSV文件             
        $tmplogfile="c:"+"\统计计算机资产\"+$(get-date -Format "yyyy-MM-dd")+".csv"                                                                                         
        #定义输出文件的路径和文件格式
        $allcomputername|Sort-Object IP地址 -Descending | Export-Csv -Encoding default -NoTypeInformation -Path $tmplogfile                                                                  
        #将数据导出为csv 文件,我们直接通过CSV 文件来获取希望拿到的信息        
        $UserName = "test@yuntcloud.com"      #定义发送账户名称
        $Password = ConvertTo-SecureString "123456" -AsPlainText –Force
        $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
        Send-MailMessage -From "test@yuntcloud.com" -To "zhouping@yuntcloud.com" -Subject "计算机硬件信息汇总" -Credential $cred -SmtpServer "mail.yuntcloud.com" -Attachments $tmplogfile -Encoding ([System.Text.Encoding]::UTF8)

思路之二:

还可以考虑使用-append参数代替数组递加

#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平 QQ:185426445 联系方式:18666943750 欢迎技术交流和提出修改意见
Import-Module activedirectory                                                   
#导入其中的AD 模块
$computeraccount=Get-ADComputer -Filter * -Properties * |?{($_.OperatingSystem -ne $null) -and ($_.enabled) -and ($_.IPv4Address -ne $null) }|select -ExpandProperty name         
#获取当前AD 计算机中的所有机器NETBIOS名称,排除禁用的,无操作系统类型、没有IP的
$allcomputername=@()                                                            
#定义所有计算机的初始空值
        if(!(Test-Path C:\统计计算机资产 -pathType container))
        {
        New-Item c:\统计计算机资产 -type directory
        }
        else {"C:\统计计算机资产文件夹已存在,不需要在创建"}
        #C盘下创建文件夹统计计算机资产,用于统一存放每天的生成的CSV文件            
        $tmplogfile="c:"+"\统计计算机资产\"+$(get-date -Format "yyyy-MM-dd")+".csv"                                                                                         
        #定义输出文件的路径和文件格式
foreach ($currentcomputename in $computeraccount)                               
#根据计算机对象进行轮询
      { 
        if (Test-NetConnection $currentcomputename|select -ExpandProperty PingSucceeded) `
        #测试计算机是否可以ping通,如果可以ping通,则继续执行
        {
        $currentcomputename+"正测试IP连通性..."   
        $currentname= Get-ADComputer -Identity $currentcomputename|select -ExpandProperty name         
        #获取机器的NETBIOS名称       
        $currentuser=(Get-WmiObject Win32_Process -Filter 'Name="explorer.exe"' -ComputerName $currentcomputename).getOwner() | Select -ExpandProperty User
        #获取机器的当前登录用户 
        $currentoperatingsystem= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystem          
        #获取机器的操作系统版本  
        $currentoperatingsystemsp= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystemServicePack          
        #获取机器的操作系统SP版本                                   
        $currentclass= Get-WmiObject -class Win32_BIOS -computername $currentcomputename -namespace "root\cimv2" |select -ExpandProperty SerialNumber        
        #通过获取WMI中的bios 类获取到机器相应的序列号,存放在BIOS的SN
        $currentIP= Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true |select -ExpandProperty IPAddress -First 1 |?{$_ -notlike "*:*" -and $_ -notlike "169*"}   
        #通过获取WMI中的IPV4地址
        $currentMAC= Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true -Property * |?{$_.IPAddress -match $currentIP} |select -ExpandProperty macaddress -First 1 
        #通过获取WMI中的MAC地址       
        $currentdiskSN=  Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename |select -First 1 -ExpandProperty Model        
        #通过获取WMI中的硬盘BIOS序列号
        $currentpcmodel=  Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty Model        
        #通过获取WMI中的计算机类型       
        $currentmemory=  (Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty TotalPhysicalMemory)/1gb -as [int]       
        #通过获取WMI中的计算机内存   
        $currentharddisk=  (Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename|select -First 1 -ExpandProperty size)/1gb  -as [int]      
        #通过获取WMI中的硬盘大小   
        $currentdiskcfreesize=  ((Get-WMIObject Win32_LogicalDisk -ComputerName $currentcomputename | ? { $_.deviceid -match "c" }).freespace)/1GB -as [int]      
        #通过获取WMI中的C盘可用空间大小                       
        $computerproperty=New-Object  psobject                                                                                                               
        #定义一个新PS 对象
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机名" -Value  $currentname                                                        
        #为新的对象定义计算机名称属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "登录用户名" -Value  $currentuser                                                        
        #为新的对象定义计算机当前登录用户名属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "主机序列号" -Value $currentclass                                                          
        #为计算机对象定义序列号属性   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "IP地址" -Value $currentip                                                          
        #为计算机对象定义IP地址属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "MAC地址" -Value $currentMAC                                                          
        #为计算机对象定义MAC地址属性   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "硬盘序列号" -Value $currentdisksn                                                          
        #为计算机对象定义硬盘序列号属性  
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "操作系统版本" -Value $currentoperatingsystem                                                          
        #为计算机对象定义操作系统版本   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "操作系统SP版本" -Value $currentoperatingsystemsp                                                          
        #为计算机对象定义操作系统SP版本   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机类型" -Value $currentpcmodel                                                          
        #为计算机对象定义计算机类型 
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机内存大小(GB)" -Value $currentmemory                                                         
        #为计算机对象定义计算机内存属性                                                               
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机硬盘大小(GB)" -Value $currentharddisk                                                         
        #为计算机对象定义计算机硬盘属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "C盘可用空间大小(GB)" -Value $currentdiskcfreesize                                                         
        #为计算机对象定义计算机C盘可用空间属性                                                                      

        $computerproperty|Sort-Object IP地址 -Descending | Export-Csv -Encoding default -NoTypeInformation -Path $tmplogfile -append                                                               
        #将数据导出为csv 文件,我们直接通过CSV 文件来获取希望拿到的信息   
             
        }      
      }  
     
        $UserName = "test@yuntcloud.com"      #定义发送账户名称
        $Password = ConvertTo-SecureString "123456" -AsPlainText –Force
        $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
        Send-MailMessage -From "test@yuntcloud.com" -To "zhouping@yuntcloud.com" -Subject "计算机硬件信息汇总" -Credential $cred -SmtpServer "mail.yuntcloud.com" -Attachments $tmplogfile -Encoding ([System.Text.Encoding]::UTF8)

思路之三:以上只会显示在线的,不在线的计算机则不会显示,其实我们可以同时分别统计在线的和不在线的,命令如下:

#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平 QQ:185426445 联系方式:18666943750 欢迎技术交流和提出修改意见
Import-Module activedirectory                                                   
#导入其中的AD 模块
$computeraccount=Get-ADComputer -Filter * -Properties * |?{($_.OperatingSystem -ne $null) -and ($_.enabled) -and ($_.IPv4Address -ne $null) }|select -ExpandProperty name         
#获取当前AD 计算机中的所有机器NETBIOS名称,排除禁用的,无操作系统类型、没有IP的
$allcomputername=@()                                                            
#定义所有计算机的初始空值
$allcomputernameoffline=@()                                                            
#定义所有离线计算机的初始空值

function Get-LoggedOnUser {
#Requires -Version 2.0            
[CmdletBinding()]            
 Param             
   (                       
    [Parameter(Mandatory=$true,
               Position=0,                          
               ValueFromPipeline=$true,            
               ValueFromPipelineByPropertyName=$true)]            
    [String[]]$ComputerName
   )#End Param
Begin            
{            
 Write-Host "`n 正查找用户 . . . "
 $i = 0            
}#Begin          
Process            
{
    $ComputerName | Foreach-object {
    $Computer = $_
    try
        {
            $processinfo = @(Get-WmiObject -class win32_process -ComputerName $Computer -EA "Stop")
                if ($processinfo)
                {    
                    $processinfo | Foreach-Object {$_.GetOwner().User} | 
                    Where-Object {$_ -ne "NETWORK SERVICE" -and $_ -ne "LOCAL SERVICE" -and $_ -ne "SYSTEM"} |
                    Sort-Object -Unique |
                    ForEach-Object { New-Object psobject -Property @{Computer=$Computer;LoggedOn=$_} } | 
                    Select-Object Computer,LoggedOn
                }#If
        }
    catch
        {
            "找不到指定进程在计算机 $computer" | Out-Host
        }
     }#Forech-object(ComputerName)       
            
}#Process
End
{
}#End
}#Get-LoggedOnUser
#查找当前登录用户
foreach ($currentcomputename in $computeraccount)                               
#根据计算机对象进行轮询
      { 
        if (Test-Connection $currentcomputename -Quiet) `
        #测试计算机是否可以ping通,如果可以ping通,则继续执行
        {
        $currentcomputename+"正测试IP连通性..."   
        $currentname= Get-ADComputer -Identity $currentcomputename|select -ExpandProperty name         
        #获取机器的NETBIOS名称       
        $currentuser=Get-LoggedOnUser -ComputerName $currentcomputename|select -ExpandProperty LoggedOn -First 1
        #$currentuser=(Get-WmiObject Win32_Process -Filter 'Name="explorer.exe"' -ComputerName $currentcomputename).getOwner() | Select -ExpandProperty User
        #获取机器的当前登录用户 
        $currentoperatingsystem= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystem          
        #获取机器的操作系统版本  
        $currentoperatingsystemsp= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystemServicePack          
        #获取机器的操作系统SP版本                                   
        $currentclass= Get-WmiObject -class Win32_BIOS -computername $currentcomputename -namespace "root\cimv2" |select -ExpandProperty SerialNumber        
        #通过获取WMI中的bios 类获取到机器相应的序列号,存放在BIOS的SN
        $currentIP=Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true |select -ExpandProperty IPAddress -First 1 |?{$_ -notlike "*:*" -and $_ -notlike "169*"}   
        #通过获取WMI中的IPV4地址      
        $currentMAC= Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true -Property * |?{$_.IPAddress -match $currentIP} |select -ExpandProperty macaddress -First 1 
        #通过获取WMI中的MAC地址 
        $currentdiskSN=  Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename |select -First 1 -ExpandProperty Model        
        #通过获取WMI中的硬盘BIOS序列号
        $currentpcmodel=  Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty Model        
        #通过获取WMI中的计算机类型       
        $currentmemory=  (Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty TotalPhysicalMemory)/1gb -as [int]       
        #通过获取WMI中的计算机内存   
        $currentharddisk=  (Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename|select -First 1 -ExpandProperty size)/1gb  -as [int]      
        #通过获取WMI中的硬盘大小   
        $currentdiskcfreesize=  ((Get-WMIObject Win32_LogicalDisk -ComputerName $currentcomputename | ? { $_.deviceid -match "c" }).freespace)/1GB -as [int]      
        #通过获取WMI中的C盘可用空间大小                       
        $computerproperty=New-Object  psobject                                                                                                               
        #定义一个新PS 对象
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机名" -Value  $currentname                                                        
        #为新的对象定义计算机名称属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "登录用户名" -Value  $currentuser                                                        
        #为新的对象定义计算机当前登录用户名属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "主机序列号" -Value $currentclass                                                          
        #为计算机对象定义序列号属性   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "IP地址" -Value $currentip                                                          
        #为计算机对象定义IP地址属性  
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "MAC地址" -Value $currentMAC                                                          
        #为计算机对象定义MAC地址属性 
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "硬盘序列号" -Value $currentdisksn                                                          
        #为计算机对象定义硬盘序列号属性  
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "操作系统版本" -Value $currentoperatingsystem                                                          
        #为计算机对象定义操作系统版本   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "操作系统SP版本" -Value $currentoperatingsystemsp                                                          
        #为计算机对象定义操作系统SP版本   
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机类型" -Value $currentpcmodel                                                          
        #为计算机对象定义计算机类型 
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机内存大小(GB)" -Value $currentmemory                                                         
        #为计算机对象定义计算机内存属性                                                               
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "计算机硬盘大小(GB)" -Value $currentharddisk                                                         
        #为计算机对象定义计算机硬盘属性
        $computerproperty|  Add-Member -MemberType NoteProperty -Name "C盘可用空间大小(GB)" -Value $currentdiskcfreesize                                                         
        #为计算机对象定义计算机C盘可用空间属性                                                                       
        $allcomputername=$allcomputername+$computerproperty                                                                                                  
        #根据对象的轮询将当前对象的属性加入到哈希数组中             
        } 
        else `
        {
        $currentnameoffline= Get-ADComputer -Identity $currentcomputename|select -ExpandProperty name         
        #获取机器的NETBIOS名称       
        $currentuseroffline=Get-LoggedOnUser -ComputerName $currentcomputename|select -ExpandProperty LoggedOn -First 1
        #$currentuser=(Get-WmiObject Win32_Process -Filter 'Name="explorer.exe"' -ComputerName $currentcomputename).getOwner() | Select -ExpandProperty User
        #获取机器的当前登录用户 
        $currentoperatingsystemoffline= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystem          
        #获取机器的操作系统版本  
        $computerpropertyoffline=New-Object  psobject                                                                                                               
        #定义一个新PS 对象
        $computerpropertyoffline|  Add-Member -MemberType NoteProperty -Name "计算机名" -Value  $currentnameoffline                                                        
        #为新的对象定义计算机名称属性
        $computerpropertyoffline|  Add-Member -MemberType NoteProperty -Name "登录用户名" -Value  $currentuseroffline                                                        
        #为新的对象定义计算机当前登录用户名属性
        $computerpropertyoffline|  Add-Member -MemberType NoteProperty -Name "操作系统版本" -Value $currentoperatingsystemoffline                                                          
        #为计算机对象定义操作系统版本 
        $allcomputernameoffline=$allcomputernameoffline+$computerpropertyoffline                                                                                                  
        #根据对象的轮询将当前对象的属性加入到哈希数组中     
        }     
      }  
        if(!(Test-Path C:\统计计算机资产 -pathType container))
        {
        New-Item c:\统计计算机资产 -type directory
        }
        else {"C:\统计计算机资产文件夹已存在,不需要在创建"}
        #C盘下创建文件夹统计计算机资产,用于统一存放每天的生成的CSV文件            
        $tmplogfile="c:"+"\统计计算机资产\"+$(get-date -Format "yyyy-MM-dd")+"-online"+".csv"                                                                                         
        #定义输出文件的路径和文件格式
        $allcomputername|Sort-Object IP地址 -Descending | Export-Csv -Encoding default -NoTypeInformation -Path $tmplogfile                                                                  
        #将数据导出为csv 文件,我们直接通过CSV 文件来获取希望拿到的信息
        $tmplogfileoffline="c:"+"\统计计算机资产\"+$(get-date -Format "yyyy-MM-dd")+"-offline"+".csv"                                                                                         
        #定义输出文件的路径和文件格式
        $allcomputernameoffline|Sort-Object 计算机名 -Descending | Export-Csv -Encoding default -NoTypeInformation -Path $tmplogfileoffline                                                                  
        #将数据导出为csv 文件,我们直接通过CSV 文件来获取希望拿到的信息        
                
        $UserName =       #定义发送账户名称
        $Password = ConvertTo-SecureString "123456" -AsPlainText –Force
        $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
        Send-MailMessage -From  -To  -Subject "计算机硬件信息汇总" -Credential $cred -SmtpServer "mail.yuntcloud.com" -Attachments $tmplogfile,$tmplogfileoffline -Encoding ([System.Text.Encoding]::UTF8)

思路之四:以上虽然我们可以同时分别统计在线的和不在线的,但是我们最希望的还是不管在线的还是不在线的都能全部显示出来,命令如下:

#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平 QQ:185426445 联系方式:18666943750 欢迎技术交流和提出修改意见
Import-Module activedirectory                                                   
#导入其中的AD 模块
$computeraccount=Get-ADComputer -Filter * -Properties * |?{($_.OperatingSystem -ne $null) -and ($_.enabled) -and ($_.IPv4Address -ne $null) }|select -ExpandProperty name         
#获取当前AD 计算机中的所有机器NETBIOS名称,排除禁用的,无操作系统类型、没有IP的
$allcomputername=@()                                                            
#定义所有计算机的初始空值
foreach ($currentcomputename in $computeraccount)                               
#根据计算机对象进行轮询
      { 
        $allcomputername1=""|select "计算机名","登录用户名","主机序列号","IP地址","MAC地址","硬盘序列号","操作系统版本","操作系统SP版本","计算机类型","计算机内存大小","计算机硬盘大小","C盘可用空间大小","计算机是否在线"     
        if (Test-Connection $currentcomputename -Quiet) `
        #测试计算机是否可以ping通,如果可以ping通,则继续执行
        #2012R2后续版本采用更高效的命令 if (Test-NetConnection $currentcomputename|select -ExpandProperty PingSucceeded) `
        {
        $currentcomputename+"正测试IP连通性..."   
        $currentname= Get-ADComputer -Identity $currentcomputename|select -ExpandProperty name         
        #获取机器的NETBIOS名称       
        $currentuser=(Get-WmiObject Win32_Process -Filter 'Name="explorer.exe"' -ComputerName $currentcomputename).getOwner() | Select -ExpandProperty User
        #获取机器的当前登录用户 
        $currentoperatingsystem= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystem          
        #获取机器的操作系统版本  
        $currentoperatingsystemsp= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystemServicePack          
        #获取机器的操作系统SP版本                                   
        $currentclass= Get-WmiObject -class Win32_BIOS -computername $currentcomputename -namespace "root\cimv2" |select -ExpandProperty SerialNumber        
        #通过获取WMI中的bios 类获取到机器相应的序列号,存放在BIOS的SN
        $currentIP= Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true |select -ExpandProperty IPAddress -First 1 |?{$_ -notlike "*:*" -and $_ -notlike "169*"}   
        #通过获取WMI中的IPV4地址
        $currentMAC= Get-WmiObject -class Win32_NetworkAdapterConfiguration -computername $currentcomputename -Filter IPEnabled=true -Property * |?{$_.IPAddress -match $currentIP} |select -ExpandProperty macaddress -First 1 
        #通过获取WMI中的MAC地址       
        $currentdiskSN=  Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename |select -First 1 -ExpandProperty Model        
        #通过获取WMI中的硬盘BIOS序列号
        $currentpcmodel=  Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty Model        
        #通过获取WMI中的计算机类型       
        $currentmemory=  (Get-WmiObject -Class Win32_ComputerSystem -computername $currentcomputename -Property * |select -ExpandProperty TotalPhysicalMemory)/1gb -as [int]       
        #通过获取WMI中的计算机内存   
        $currentharddisk=  (Get-WmiObject -Class Win32_DiskDrive -computername $currentcomputename|select -First 1 -ExpandProperty size)/1gb  -as [int]      
        #通过获取WMI中的硬盘大小   
        $currentdiskcfreesize=  ((Get-WMIObject Win32_LogicalDisk -ComputerName $currentcomputename | ? { $_.deviceid -match "c" }).freespace)/1GB -as [int]      
        #通过获取WMI中的C盘可用空间大小                                
        $allcomputername1.计算机名=$currentname
        $allcomputername1.登录用户名=$currentuser
        $allcomputername1.主机序列号=$currentclass
        $allcomputername1.IP地址=$currentip
        $allcomputername1.MAC地址=$currentmac
        $allcomputername1.硬盘序列号=$currentdisksn
        $allcomputername1.操作系统版本=$currentoperatingsystem
        $allcomputername1.操作系统SP版本=$currentoperatingsystemsp
        $allcomputername1.计算机类型=$currentpcmodel
        $allcomputername1.计算机内存大小=$currentmemory
        $allcomputername1.计算机硬盘大小=$currentharddisk
        $allcomputername1.C盘可用空间大小=$currentdiskcfreesize
        $allcomputername1.计算机是否在线="Online"                                              
        }
        else `
        {
        $currentname= Get-ADComputer -Identity $currentcomputename|select -ExpandProperty name         
        #获取机器的NETBIOS名称  
        $currentoperatingsystem= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystem          
        #获取机器的操作系统版本  
        $currentoperatingsystemsp= Get-ADComputer -Identity $currentcomputename -Properties * |select -ExpandProperty OperatingSystemServicePack          
        #获取机器的操作系统SP版本  
        $allcomputername1.计算机名=$currentname
        $allcomputername1.操作系统版本=$currentoperatingsystem
        $allcomputername1.操作系统SP版本=$currentoperatingsystemsp
        $allcomputername1.计算机是否在线="Offline"      
        }
        $allcomputername+=$allcomputername1      
      }  
        if(!(Test-Path C:\统计计算机资产 -pathType container))
        {
        New-Item c:\统计计算机资产 -type directory
        }
        else {"C:\统计计算机资产文件夹已存在,不需要在创建"}
        #C盘下创建文件夹统计计算机资产,用于统一存放每天的生成的CSV文件             
        $tmplogfile="c:"+"\统计计算机资产\"+$(get-date -Format "yyyy-MM-dd")+".csv"                                                                                         
        #定义输出文件的路径和文件格式
        $allcomputername|Sort-Object IP地址 -Descending | Export-Csv -Encoding default -NoTypeInformation -Path $tmplogfile                                                                  
        #将数据导出为csv 文件,我们直接通过CSV 文件来获取希望拿到的信息        
        $UserName = "test@yuntcloud.com"      #定义发送账户名称
        $Password = ConvertTo-SecureString "123456" -AsPlainText –Force
        $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
        Send-MailMessage -From "test@yuntcloud.com" -To "zhouping@yuntcloud.com" -Subject "计算机硬件信息汇总" -Credential $cred -SmtpServer "mail.yuntcloud.com" -Attachments $tmplogfile -Encoding ([System.Text.Encoding]::UTF8)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值