获得运行权限:
请确保在运行脚本文件之前,设置了适当的执行策略。如果需要,可以使用 Set-ExecutionPolicy
命令更改执行策略。例如,可以使用以下命令将执行策略设置为 RemoteSigned
:
Set-ExecutionPolicy RemoteSigned
这将允许运行本地脚本文件,但要求远程脚本文件必须由受信任的发布者签名才能运行。
注意:使用管理员身份运行powershell程序。
脚本编写:
# 吸引人的 PowerShell 脚本名称:SuperInfo.ps1
# 获取操作系统信息
$os = Get-WmiObject -Class Win32_OperatingSystem
$osName = $os.Caption
$osVersion = $os.Version
# 获取处理器信息
$processor = Get-WmiObject -Class Win32_Processor
$processorName = $processor.Name
$numberOfCores = $processor.NumberOfCores
# 获取内存信息
$memory = Get-WmiObject -Class Win32_PhysicalMemory
$totalMemory = ($memory | Measure-Object -Property Capacity -Sum).Sum
# 获取磁盘信息
$disk = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3"
$diskFreeSpace = ($disk | Measure-Object -Property FreeSpace -Sum).Sum
$diskTotalSize = ($disk | Measure-Object -Property Size -Sum).Sum
# 显示计算机信息
Write-Host "=== Super Info ==="
Write-Host "Operating System: $osName"
Write-Host "Version: $osVersion"
Write-Host "Processor: $processorName"
Write-Host "Number of Cores: $numberOfCores"
Write-Host "Total Memory: $totalMemory bytes"
Write-Host "Free Disk Space: $diskFreeSpace bytes"
Write-Host "Total Disk Size: $diskTotalSize bytes"
Write-Host "=================="
运行:
C:\delphisource\mytools\mynewtools\Win64\Debug\test.ps1