PowerShell中 用户登录控制系统计算机仿真中怎么控制?

使用Powershell来管理服务器_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
使用Powershell来管理服务器
你可能喜欢用户名:zhou_ping
文章数:159
评论数:165
访问量:385201
注册日期:
阅读量:1297
阅读量:3317
阅读量:581571
阅读量:466128
51CTO推荐博文
-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包,QQ:.电话客户端需设置防火墙,[注意要先设置管理模版防火墙设置,否则将会覆盖默认组策略的高级防火墙安全设置]1、允许远程管理,设置如下,启用windows防火墙:允许入站管理程序参考链接:http://2、 允许远程桌面开启远程桌面。域组策略,“计算机配置”―&“策略”―&”管理模板“―&”Windows组件”―&”远程桌面服务“―&“远程桌面会话主机”―&”连接“,允许用户通过使用远程桌面服务进行远程连接,状态改为“已启用”。3、允许ping,打开组策略,高级安全防火墙设置,入站规则4、选择ICMPv4和ICMPv65、点击下一步完成6、允许Powershell远程管理具体设置参考链接:脚本如下:#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平&QQ:&联系方式:&欢迎技术交流和提出修改意见
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&=&""&&&&&&#定义发送账户名称
&&&&&&&&$Password&=&ConvertTo-SecureString&"123456"&-AsPlainText&CForce
&&&&&&&&$cred&=&New-Object&System.Management.Automation.PSCredential($UserName,$Password)&
&&&&&&&&Send-MailMessage&-From&""&-To&""&-Subject&"计算机硬件信息汇总"&-Credential&$cred&-SmtpServer&""&-Attachments&$tmplogfile&-Encoding&([System.Text.Encoding]::UTF8)思路之一:$a=""|select "计算机类型","计算机名"$a.计算机类型+="a"$a.计算机名+="b"$a输出结果为:修改后的powershell如下:#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平&QQ:&联系方式:&欢迎技术交流和提出修改意见
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&=&""&&&&&&#定义发送账户名称
&&&&&&&&$Password&=&ConvertTo-SecureString&"123456"&-AsPlainText&CForce
&&&&&&&&$cred&=&New-Object&System.Management.Automation.PSCredential($UserName,$Password)&
&&&&&&&&Send-MailMessage&-From&""&-To&""&-Subject&"计算机硬件信息汇总"&-Credential&$cred&-SmtpServer&""&-Attachments&$tmplogfile&-Encoding&([System.Text.Encoding]::UTF8)思路之二:还可以考虑使用-append参数代替数组递加#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平&QQ:&联系方式:&欢迎技术交流和提出修改意见
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&=&""&&&&&&#定义发送账户名称
&&&&&&&&$Password&=&ConvertTo-SecureString&"123456"&-AsPlainText&CForce
&&&&&&&&$cred&=&New-Object&System.Management.Automation.PSCredential($UserName,$Password)&
&&&&&&&&Send-MailMessage&-From&""&-To&""&-Subject&"计算机硬件信息汇总"&-Credential&$cred&-SmtpServer&""&-Attachments&$tmplogfile&-Encoding&([System.Text.Encoding]::UTF8)思路之三:以上只会显示在线的,不在线的计算机则不会显示,其实我们可以同时分别统计在线的和不在线的,命令如下:#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平&QQ:&联系方式:&欢迎技术交流和提出修改意见
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&=&$_
&&&&&&&&&&&&$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=$CLoggedOn=$_}&}&|&
&&&&&&&&&&&&&&&&&&&&Select-Object&Computer,LoggedOn
&&&&&&&&&&&&&&&&}#If
&&&&&&&&&&&&"找不到指定进程在计算机&$computer"&|&Out-Host
&&&&&}#Forech-object(ComputerName)&&&&&&&
&&&&&&&&&&&&
}#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&CForce
&&&&&&&&$cred&=&New-Object&System.Management.Automation.PSCredential($UserName,$Password)&
&&&&&&&&Send-MailMessage&-From&&-To&&-Subject&"计算机硬件信息汇总"&-Credential&$cred&-SmtpServer&""&-Attachments&$tmplogfile,$tmplogfileoffline&-Encoding&([System.Text.Encoding]::UTF8)思路之四:以上虽然我们可以同时分别统计在线的和不在线的,但是我们最希望的还是不管在线的还是不在线的都能全部显示出来,命令如下:#统计ip、MAC地址、计算机名、登录用户、计算机配置、主机序列号、硬盘序列号、计算机型号、windows及SP的版本、C盘可用空间
#防火墙开启windows远程管理、windows防火墙允许入站远程管理
#编写:周平&QQ:&联系方式:&欢迎技术交流和提出修改意见
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&=&""&&&&&&#定义发送账户名称
&&&&&&&&$Password&=&ConvertTo-SecureString&"123456"&-AsPlainText&CForce
&&&&&&&&$cred&=&New-Object&System.Management.Automation.PSCredential($UserName,$Password)&
&&&&&&&&Send-MailMessage&-From&""&-To&""&-Subject&"计算机硬件信息汇总"&-Credential&$cred&-SmtpServer&""&-Attachments&$tmplogfile&-Encoding&([System.Text.Encoding]::UTF8)本文出自 “” 博客,请务必保留此出处
了这篇文章
附件下载:          
类别:┆阅读(0)┆评论(0)
请输入验证码:

我要回帖

更多关于 session控制用户登录 的文章

 

随机推荐