虽然已经测试过多种Windows系统能用,但不能保证适用各种情况,毕竟系统环境千差万别,建议先打快照,快照转镜像买台按量计费的临时测试机的机器来验证,验证没问题再用#获取网卡名$NICName=(Get-WmiObject 0.0.0.0 $gwnetsh interface ipv4 add route prefix=0.0.0.0/0 interface=$NICName nexthop=$gw#getgw$gw=(Get-WmiObject findstr /i DhcpServer$gw = $string -split '\s+' | Select-Object -Last 1}#set route metric$NICName=(Get-WmiObject netsh interface ip set interface interface="$interfacename" metric=1}#set defaultgw metric$interfaces = Get-WmiObject /d "$gw\0" /f}#以上代码执行完成后,检查网关是否为空,为空则重启机器write-host > C:\checkgw.ps1"if( [string]::IsNullOrEmpty( (Get-WmiObject
通过下面的例子告诉大家如何获取设备里面包含的类 获取 WMI 类 在使用 WMI 之前需要知道 WMI 是能做什么的,这个方法能做的就是描述系统能被管理的资源,在系统里面包含了几百个类,一个类里面包含很多属性 通过 Get-WmiObject 可以找到设备里面所有可以被找到的 WMI 类 Get-WmiObject -List 在 Windows 10 设备,右击开始菜单,打开 PowerShell 输入上面代码,就可以看到输出 在 Get-WmiObject 的参数可以加上计算机是哪个,支持访问局域网可以访问的计算机的信息 Get-WmiObject -List -ComputerName 192.168.1.29 尝试在自己的系统输入一下,可以看到很多代码 -Class 某个类 具体的类可以通过 Get-WmiObject -List 找到 获取某个类里面包含的所有属性,通过这个格式 Get-WmiObject -Class 某个类 | Get-Member -MemberType Property 如果需要获取某个类的某一些属性的值,可以通过下面的代码 Get-WmiObject -Class 某个类 | Format-Table -Property 属性
Aliyun|Alibaba"}| select -property Name,StartMode,State,PathName Get-Process -Name "aliyun*","vminit*" get-wmiobject Aliyun|Alibaba"} | ForEach-Object { Stop-Service -name $_.Name -Force -Confirm:$false} -EA 0 2>&1 >$null get-wmiobject aliyun*","vminit*" | ForEach-Object { Stop-Process -Id $_.Id -Force -Confirm:$false} -EA 0 2>&1 >$null get-wmiobject Aliyun|Alibaba"}| select -property Name,StartMode,State,PathName Get-Process -Name "aliyun*","vminit*" get-wmiobject where{$_.PathName -match "Aliyun|Alibaba"} | ForEach-Object { sc.exe delete $_.Name} -EA 0 2>&1 >$null get-wmiobject
2.0.查找 WMI 实例 我们可以使用 Get-WMIObject 和由root\Subscription组成的 –Class 参数,然后指定我们希望查看的适当类 #List Event Filters Get-WMIObject -Namespace root\Subscription -Class __EventFilter 可以通过 Filter 实例的 Query 属性判断正在使用哪种Consumer #List Event Consumers Get-WMIObject -Namespace root\Subscription -Class __EventConsumer #List 事件绑定 Get-WMIObject -Namespace root\Subscription -Class __FilterToConsumerBinding 可以从 __PATH 属性中看到哪个 Filter 定位实例并通过管道传输到 Remove-##Removing WMI Subscriptions using Remove-WMIObject #Filter Get-WMIObject -Namespace
与物理位置 (例如磁盘上) 不同,它们本质上更具有逻辑 WMI 中的所有命名空间都是 __Namespace 系统类的实例,要获取 root 命名空间下所有命名空间的列表,可使用以下命令查询同一个类: Get-WmiObject -Namespace root -Class __Namespace 输出的内容包含了许多信息,为了过滤掉 “无用” 信息,可使用 PowerShell 中的 select: Get-WmiObject cmdlet 列出可用的类: Get-WmiObject -Class * -List 上面这条命令将会列出所有类,但为了举例,假设我们对系统上的用户感兴趣。 可以使用以下命令来缩小范围,该命令列出了用于获取或操作用户信息的所有可用类: Get-WmiObject -Class *user* -List 同样也可以使用 Get-CimClass 命令也能实现同样的效果 为了避免这种情况,我们可以使用 -Filter 参数来获取我们正在寻找的特定进程 (这里选择了 lsass.exe): Get-WmiObject -Class Win32_Process -Filter
Get-WmiObject -Class win32_computersystem -Property bootupstate,username,totalphysicalmemory,systemtype 另一个类 Win32_OperatingSystem 也为我们提供了很多有用的信息: Get-WmiObject -Class win32_operatingsystem | fl * 3 目录列表 Get-WmiObject -Class win32_directory 通常使用通配符搜索文件模式很有帮助。可以使用 cmdlet 的 -Filter 参数来实现类似的功能。 可以使用以下命令来查询: Get-WmiObject -Class win32_ntlogevent 每个日志条目都包含详细信息,例如时间、生成事件的来源、严重性和消息。 Get-WmiObject -Class win32_ip4routetable 在谈论网络内容时,我想提到另一个名为 Win32_NetworkAdapter 的有用类。
Tips: Get-Alias gwmi 可以看到 gwmi 是 Get-WmiObject 的别名 语法参数: Get-WmiObject [[-Class] <System.String>] [[ 简单示例: # 1) 计算机相关信息获取、操作 Get-WmiObject Win32_UserAccount # 获取计算机中的账号 (可省略Class) Get-WmiObject Win32 Get-WmiObject -Class Win32_Bios | Format-List -Property * # 本地计算机获取 BIOS 信息 Get-WmiObject _ComputerSystem # 获取计算机系统的信息 Get-WmiObject -class Win32_LogicalDisk # 获取计算机磁盘信息 Get-WMIObject -Class Get-WmiObject Win32_LogicalDisk -filter "DeviceID = 'c:' " # 指定逻辑磁盘信息 Get-WmiObject win32_service -
在PowerShell中,我们使用Get-WMIObject命令来查看事件过滤器绑定的WMI事件过滤器,事件使用者和使用者过滤器。 # Reviewing WMI Subscriptions using Get-WMIObject # Event Filter Get-WMIObject -Namespace root\Subscription -Class __EventFilter -Filter “Name=’Updater’” # Event Consumer Get-WMIObject -Namespace root\Subscription # Removing WMI Subscriptions using Remove-WMIObject # Event Filter Get-WMIObject -Namespace root\Subscription -Class __EventFilter -Filter “Name=’Updater’” | Remove-WmiObject -Verbose # Event Consumer Get-WMIObject
通过下面的例子告诉大家如何获取设备里面包含的类 获取 WMI 类 在使用 WMI 之前需要知道 WMI 是能做什么的,这个方法能做的就是描述系统能被管理的资源,在系统里面包含了几百个类,一个类里面包含很多属性 通过 Get-WmiObject 可以找到设备里面所有可以被找到的 WMI 类 Get-WmiObject -List 在 Windows 10 设备,右击开始菜单,打开 PowerShell 输入上面代码,就可以看到输出 在 Get-WmiObject 的参数可以加上计算机是哪个,支持访问局域网可以访问的计算机的信息 Get-WmiObject -List -ComputerName 192.168.1.29 尝试在自己的系统输入一下,可以看到很多代码 -Class 某个类 具体的类可以通过 Get-WmiObject -List 找到 获取某个类里面包含的所有属性,通过这个格式 Get-WmiObject -Class 某个类 | Get-Member -MemberType Property 如果需要获取某个类的某一些属性的值,可以通过下面的代码 Get-WmiObject -Class 某个类 | Format-Table -Property 属性
windows通过命令获取mtu 一、支持>2008R2的系统,不支持≤2008R2 #快 $NICName=(Get-WmiObject Win32_NetworkAdapter -Filter ' NetEnabled=True').NetConnectionID Get-NetAdapter $NICName| Format-List *|findstr /i mtu #快 $NICName=(Get-WmiObject NetworkAdapter -Filter 'NetEnabled=True').NetConnectionID (Get-NetAdapter $NICName).MtuSize #有点慢 $NICName=(Get-WmiObject hl=zh-cn $NICName=(Get-WmiObject Win32_NetworkAdapter -Filter 'NetEnabled=True').NetConnectionID $idx =(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").InterfaceIndex 图片 图片
\CurrentControlSet\Services\Tcpip6\Parameters" /v DisabledComponents /t REG_DWORD /d 0x11 /f $gw=(Get-WmiObject true'"|where {$_.Description -match "Tencent VirtIO Ethernet Adapter"}).defaultipgateway $gw $idx=(Get-WmiObject match "Tencent VirtIO Ethernet Adapter"}).InterfaceIndex $idx #如果想获取本地连接网卡名称,执行这句powershell #$NICName=(Get-WmiObject
Aliyun|Alibaba"}| select -property Name,StartMode,State,PathName Get-Process -Name "aliyun*","vminit*" get-wmiobject Aliyun|Alibaba"} | ForEach-Object { Stop-Service -name $_.Name -Force -Confirm:$false} -EA 0 2>&1 >$null get-wmiobject \tat_agent_installer.exe start-sleep 5 #查询tat agent运行状态 get-wmiobject -class win32_service | where{$ \windows-stargate-installer.exe start-sleep 5 #查询云监控组件相关2个服务的运行状态 get-wmiobject -class win32_service 以上3大组件都有共同的父路径:C:\Program Files\QCloud\,可以用一句命令过滤 get-wmiobject -class win32_service | where{$_.PathName
\ -force #上面2行代码适用≥2012系统,不适用2008R2系统,为了提升代码兼容性,上面2行的功能用下面的黑体代码替代了 $str1="" $str2="" $str3="" if(((Get-WmiObject version) -eq "6.1.7601"){ $str1="certutil -urlcache -split -f " $str2=" " $str3="Win7" } if(((Get-WmiObject OperatingSystem).version) -ne "6.1.7601"){ $str1="Invoke-WebRequest -uri " $str2=" -OutFile " if(((Get-WmiObject Win32_OperatingSystem).version) -eq "6.3.9600"){$str3="Win8"} if(((Get-WmiObject Win32_OperatingSystem
以下代码仅限一块网卡的机器,多块网卡的代码需要调整以管理员身份powershell执行这几句命令$gw=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").defaultipgateway#$idx=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").InterfaceIndex$idx=(Get-WmiObject win32_networkadapterconfiguration -filter = 'true'" | Where-Object {$_.servicename -eq "netkvm"} ).InterfaceIndex下面这些开头带#的是备忘的,不用管,跳过即可#$idx=(Get-WmiObject -filter "ipenabled = 'true'" | Where-Object {$_.Description -notmatch "NGN"} ).InterfaceIndex#$idx=(Get-WmiObject
举个具体例子,有台机器执行msinfo32有2颗CPU,每颗是20个cores(每个core是双线程),即每颗是40个vCPUmsinfo32图片PS C:\Users\Administrator> (Get-WmiObject System.Environment]::ProcessorCount40PS C:\Users\Administrator> [System.Environment]::ProcessorCount * ((Get-WmiObject -Class Win32_Processor).NumberOfLogicalProcessors).count80PS C:\Users\Administrator> (Get-WmiObject
_operatingSystem).lastbootuptime图片get-date(get-date) - (gcim Win32_OperatingSystem).LastBootUpTime((Get-WmiObject Win32_OperatingSystem).ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime))$obj = Get-WmiObject Win32_OperatingSystem$obj.ConvertToDateTime($obj.LastBootUpTime)$operatingSystem = Get-WmiObject Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime)))"function Get-SystemUptime { $operatingSystem = Get-WmiObject
name}} | get-service -name bits | select -Property MachineName,status 例子2、以下命令获取了服务器名和C盘剩余空间(以GB为单位) Get-WmiObject Get-WmiObject -class win32_bios -computername (get-adcomputer -filter * | select -ExpandPropert y Name) 「Powershell 2.0] Get-WmiObject –class win32_bios –ComputerName (Get-ADComputer –filteer * ).name [Powershell3.0] get-CimInstance 支持管道输入,代替Get-WmiObject Get-ADComputer -Filter * | Get-WmiObject -class
powershell:#getgw$gw=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").defaultipgatewayif ([string]::IsNullOrEmpty($gw)){$gw=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").DHCPServer}if([string]::IsNullOrEmpty($gw)){$interface = (Get-WmiObject Win32_NetworkAdapter findstr /i DhcpServer$gw = $string -split '\s+' | Select-Object -Last 1}#set route metric$NICName=(Get-WmiObject netsh interface ip set interface interface="$interfacename" metric=1}#set defaultgw metric$interfaces = Get-WmiObject
查看硬盘总字节数 Get-PhysicalDisk | Select-Object DeviceID, MediaType, Model, Size 显示磁盘0每个volume的总字节数 Get-WmiObject -Class Win32_DiskPartition|Where-Object { $_.Name -match '#0,' } Get-WmiObject -Class Win32_DiskPartition |Where-Object { $_.Name -match '#0,' } #0后面的逗号一个是英文一个是中文 2句都执行下,肯定会有一个有结果 显示磁盘1每个volume的总字节数 Get-WmiObject -Class Win32_DiskPartition|Where-Object { $_.Name -match '#1,' } Get-WmiObject -Class Win32_DiskPartition
[TOC] PS > Get-WmiObject Win32_UserAccount #显示计算机中的账号利用class AccountType : 512 Caption : WEIYIGEEK : S-1-5-21-923396991-3399624210-3190802951-500 FullName : Name : Administrator Get-WmiObject WEIYIGEEK SID : S-1-5-21-923396991-3399624210-3190802951-501 FullName : Name : guest PS > Get-WmiObject