Powershell检查远程电脑上的存储

发布于 2025-01-14 10:32:52 字数 410 浏览 4 评论 0原文

你好,我正在构建一个菜单来运行每个所选选项的脚本,我想要的一个选项是检查远程电脑的存储,但通过研究,我已经破坏了脚本,希望能够从拥有超过一个月使用 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

Hello I am building a menu to run scripts per option selected and an option I want is to check storage of a remote pc but from researching I have broken the script and hopefully can get assistance from someone that has more than my month experience using 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再让梦枯萎 2025-01-21 10:32:52

您需要将 $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

You need to move the $Computer = Read-Host ... statement out of the Invoke-Command statement:

# 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
酒浓于脸红 2025-01-21 10:32:52

您不需要使用 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 首先请求它 - 而不需要详细说明

旁注

Get-WMIObject 等 WMI Cmdlet 已弃用,并已被较新的 CIM Cmdlet

  • 在 v3 中引入,它使用 DCOM 之外的单独远程协议。
    • 也可以明确地使用 DCOM,但默认情况下不行。
  • 强调替换,因为从 PowerShell Core 开始,它们不再是 PowerShell 部署的一部分。
  • 如果我可以补充一下,大多数 cmdlet 翻译起来相当容易:
    • Get-WmiObject -Class Win32_ologicalDisk -Filter "DeviceID='C:'"
    • Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='C:'"

You don't need to use Invoke-Command seeing as the WMI cmdlets accept a -ComputerName value:

$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]
        }
    }

Alternatively, you can have the Computer Name input be prompted first using the grouping operator (as Santiago points out in the comments):

Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'" -ComputerName (Read-Host -Prompt "Please Enter Host name")
  • Same goes for the subexpression operator which just tells PowerShell to ask for it first - without going into more detail.

Side Note:

The WMI Cmdlets such as Get-WMIObject are deprecated and have been replaced by the newer CIM Cmdlets.

  • Introduced in v3, it uses a separate remoting protocol other than DCOM.
    • This can be explicitly made to use DCOM as well, but not by default.
  • Emphasis on replaced as they are no longer part of PowerShell's deployment as of PowerShell Core.
  • Most cmdlets translate fairly easy if I may add:
    • Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'"
    • Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='C:'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文