Powershell检查远程电脑上的存储



你好,我正在构建一个菜单,根据所选选项运行脚本,我想要的一个选项是检查远程电脑的存储,但经过研究,我破坏了脚本,希望能得到一个有超过一个月使用PS经验的人的帮助。

Invoke-Command $Computer = Read-Host Please Enter Host name  -ScriptBlock{Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'" | Select SystemName, DeviceID, @{n='Size(GB)';e={$_.size / 1gb -as [int]}},@{n='Free(GB)';e={$_.Freespace / 1gb -as [int]}}} > C:DiskInfo_output.txt

您需要将$Computer = Read-Host ...语句从Invoke-Command语句中移出:

# Ask for computer name
$Computer = Read-Host "Please Enter Host name"
# Invoke command on remote computer
Invoke-Command -ComputerName $Computer -ScriptBlock {
Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'" | Select SystemName, DeviceID, @{n='Size(GB)';e={$_.size / 1gb -as [int]}},@{n='Free(GB)';e={$_.Freespace / 1gb -as [int]}}
} > C:DiskInfo_output.txt

您不需要使用Invoke-Command,因为WMI cmdlet接受-ComputerName值:

$ComputerName = Read-Host -Prompt "Please Enter Host name"
Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'" -ComputerName $ComputerName | 
Select-Object -Property SystemName, DeviceID, @{
Name ='Size(GB)';
Expression = {
$_.size / 1gb -as [int]
}
}, @{
Name ='Free(GB)';
Expression = {
$_.Freespace / 1gb -as [int]
}
}

或者,您可以先使用分组运算符提示输入"计算机名称"(,Santiago在注释中指出(:

Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'" -ComputerName (Read-Host -Prompt "Please Enter Host name")
  • 子表达式运算符也是如此,它只是告诉PowerShell先请求它-,而不详细说明

旁注:

WMI Cmdlet(如Get-WMIObject(已被弃用,并已被较新的CIM Cmdlet所取代。

  • 在v3中引入,它使用了一个独立的远程处理协议,而不是DCOM。
    • 这也可以显式地使用DCOM,但不是默认情况
  • 的强调取代了,因为自PowerShell Core起,它们不再是PowerShell部署的一部分
  • 如果我可以补充的话,大多数cmdlet的翻译都相当容易:
    • Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'"
    • Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='C:'"

相关内容

最新更新