trx
Published on 2025-01-15 / 20 Visits
0

powershell常用命令

# 获取本机IP地址,以网卡+IP的方式显示
Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -notlike "*Loopback*" } | Select-Object InterfaceAlias, IPAddress | Format-Table -HideTableHeaders

# 获取主机名
$env:COMPUTERNAME

# 获取BIOS序列号
(Get-WmiObject -Class Win32_BIOS).SerialNumber

# 查询网卡mac地址
(Get-NetAdapter | Where-Object { $_.Status -eq 'Up' })

# 查询硬盘信息,将存储单位自动转换成GB和TB
Get-WmiObject -Class Win32_DiskDrive | Select-Object Model, @{
    Name = "Size"; Expression = {
        if ($_.Size -ge 1TB) {
            [math]::Round($_.Size / 1TB, 2).ToString() + " TB"
        } else {
            [math]::Round($_.Size / 1GB, 2).ToString() + " GB"
        }
    }
}, SerialNumber

# 查询内存信息,将内存单位转换为GB
Get-WmiObject -Class Win32_PhysicalMemory | Select-Object @{
    Name = "CapacityGB"; Expression = { [math]::Round($_.Capacity / 1GB, 2) }
}, Manufacturer, PartNumber

# 查询CPU信息
Get-WmiObject -Class Win32_Processor | Select-Object Name, Manufacturer, NumberOfCores, NumberOfLogicalProcessors

# 获取操作系统信息
Get-WmiObject -Class Win32_OperatingSystem | Select-Object Caption, Version, OSArchitecture

# 查询CPU使用率最高的10个进程
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10
# 获取当前资源使用情况
# 定义单位换算函数
function Format-Bytes {
    param (
        [double]$Bytes
    )
    if ($Bytes -ge 1TB) {
        return "$([math]::Round($Bytes / 1TB, 2)) TB"
    } elseif ($Bytes -ge 1GB) {
        return "$([math]::Round($Bytes / 1GB, 2)) GB"
    } elseif ($Bytes -ge 1MB) {
        return "$([math]::Round($Bytes / 1MB, 2)) MB"
    } elseif ($Bytes -ge 1KB) {
        return "$([math]::Round($Bytes / 1KB, 2)) KB"
    } else {
        return "$Bytes Bytes"
    }
}

# 获取 CPU 使用率(百分比)
$cpuUsage = Get-Counter '\Processor(_Total)\% Processor Time'
$cpuUsageValue = [math]::Round($cpuUsage.CounterSamples.CookedValue, 2)
Write-Host "CPU 使用率: $cpuUsageValue %"

# 获取可用内存(转换为动态单位)
$availableMemory = Get-Counter '\Memory\Available MBytes'
$availableMemoryBytes = $availableMemory.CounterSamples.CookedValue * 1MB
$availableMemoryFormatted = Format-Bytes -Bytes $availableMemoryBytes
Write-Host "可用内存: $availableMemoryFormatted"

# 获取磁盘读写速度(转换为动态单位)
$diskRead = Get-Counter '\PhysicalDisk(_Total)\Disk Read Bytes/sec'
$diskWrite = Get-Counter '\PhysicalDisk(_Total)\Disk Write Bytes/sec'
$diskReadFormatted = Format-Bytes -Bytes $diskRead.CounterSamples.CookedValue
$diskWriteFormatted = Format-Bytes -Bytes $diskWrite.CounterSamples.CookedValue
Write-Host "磁盘读取速度: $diskReadFormatted/s"
Write-Host "磁盘写入速度: $diskWriteFormatted/s"

# 获取网络收发速度(转换为动态单位)
$networkReceived = Get-Counter '\Network Interface(*)\Bytes Received/sec'
$networkSent = Get-Counter '\Network Interface(*)\Bytes Sent/sec'
$networkReceivedFormatted = Format-Bytes -Bytes $networkReceived.CounterSamples.CookedValue
$networkSentFormatted = Format-Bytes -Bytes $networkSent.CounterSamples.CookedValue
Write-Host "网络接收速度: $networkReceivedFormatted/s"
Write-Host "网络发送速度: $networkSentFormatted/s"

# 获取磁盘使用情况(总容量、已用空间、可用空间)
$disk = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID, Size, FreeSpace
foreach ($d in $disk) {
    $totalSize = Format-Bytes -Bytes $d.Size
    $freeSpace = Format-Bytes -Bytes $d.FreeSpace
    $usedSpace = Format-Bytes -Bytes ($d.Size - $d.FreeSpace)
    Write-Host "磁盘 $($d.DeviceID) 使用情况:"
    Write-Host "  总容量: $totalSize"
    Write-Host "  已用空间: $usedSpace"
    Write-Host "  可用空间: $freeSpace"
}