https://technet.microsoft.com/en-us/library/dd367892.aspx
首推使用
Get-Counter
该函数下可以把现有的电脑监控统计数据 直接提取出来 ,
用法参考http://www.colorconsole.de/PS_Windows/de/Get-Counter.htm
如:$data = get-counter "\\SERVER01\Process(Idle)\% Processor Time"
如果实在不行 再可以考虑 一下内容 ,请继续狂拽酷炫的阅读下去
1.GB为单位查询内存
gwmi Win32_PhysicalMemory | %{$sum = 0} { $sum += $_.Capacity } {Write-Host ($sum / 1GB) "GB"}
--查询空闲内存
Get-WmiObject win32_OperatingSystem FreePhysicalMemory
--查询内存状态
gwmi Win32_PerfFormattedData_PerfOS_Memory
gwmi Win32_PerfFormattedData_PerfOS_Memory | %{$sum = 0} { $sum += $_.AvailableBytes} {Write-Host ($sum / 1GB) "GB"}
获取系统的信息: Get-WmiObject –class win32_OperatingSystem
获系统的信息: Get-WmiObject –class win32_ComputerSystem
获取磁盘信息: Get-WmiObject –class Win32_LogicalDisk
获取网络信息: Get-WmiObject –class Win32_NetworkAdapterConfiguration
获取网卡及带宽:gwmi Win32_PerfFormattedData_Tcpip_NetworkInterface
2.GB为单位查询硬盘
gwmi Win32_DiskDrive | %{$sum = 0} { $sum += $_.Size } {Write-Host ($sum / 1GB) "GB"}
--查询硬盘剩余空间
使用Windows PowerShell来
BIOS: PS C:/> Get-WmiObject -Class Win32_BIOS
内存: PS C:/> Get-WmiObject -Class Win32_PhysicalMemory
处理器: PS C:/> Get-WmiObject -Class Win32_Processor
显卡: PS C:/> Get-WmiObject -Class Win32_VideoController
硬盘: PS C:/> Get-WmiObject -Class Win32_DiskDrive
3.链接数据库操作
PowerShell 连接 SQL SERVER 数据库进行操作
- #配置信息
- $Database = 'DemoDB'
- $Server = '"WIN-AHAU9NO5R6U\DOG"'
- $UserName = 'kk'
- $Password = '123456'
- #创建连接对象
- $SqlConn = New-Object System.Data.SqlClient.SqlConnection
- #使用账号连接MSSQL
- $SqlConn.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;user id=$UserName;pwd=$Password"
- #或者以 windows 认证连接 MSSQL
- #$SqlConn.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;Integrated Security=SSPI;"
- #打开数据库连接
- $SqlConn.open()
- #执行语句方法一
- $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
- $SqlCmd.connection = $SqlConn
- $SqlCmd.commandtext = 'delete top(1) from dbo.B'
- $SqlCmd.executenonquery()
- #执行语句方法二
- $SqlCmd = $SqlConn.CreateCommand()
- $SqlCmd.commandtext = 'delete top(1) from dbo.B'
- $SqlCmd.ExecuteScalar()
- #方法三,查询显示
- $SqlCmd.commandtext = 'select name,recovery_model_desc,log_reuse_wait_desc from sys.databases'
- $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
- $SqlAdapter.SelectCommand = $SqlCmd
- $set = New-Object data.dataset
- $SqlAdapter.Fill($set)
- $set.Tables[0] | Format-Table -Auto
- #关闭数据库连接
- $SqlConn.close()
感谢 黄泽成先生 blog:http://my.csdn.net/kk185800961