标签:powershell管理系列(三十六)powershell操作之统计域内计算机硬件资产
-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包,QQ:185426445.电话18666943750
客户端需设置防火墙,[注意要先设置管理模版防火墙设置,否则将会覆盖默认组策略的高级防火墙安全设置]
1、允许远程管理,设置如下,启用windows防火墙:允许入站管理程序
参考链接:http://908174.blog.51cto.com/898174/1175525
2、 允许远程桌面
3、允许ping,打开组策略,高级安全防火墙设置,入站规则
4、选择ICMPv4和ICMPv6
5、点击下一步完成
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
输出结果为:
修改后的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)本文出自 “周平的微软技术交流平台” 博客,请务必保留此出处http://yuntcloud.blog.51cto.com/1173839/1873729
Powershell管理系列(三十六)PowerShell操作之统计域内计算机硬件资产
标签:powershell管理系列(三十六)powershell操作之统计域内计算机硬件资产
原文地址:http://yuntcloud.blog.51cto.com/1173839/1873729