繁体   English   中英

用于远程计算机的 Powershell Get-Volumn

[英]Powershell Get-Volumn for remote computer

以下是我为本地服务器执行以获取磁盘详细信息的代码

$props = @(
    'DriveLetter'
    'FileSystemLabel'
    'FileSystem'
    'DriveType'
    'HealthStatus'
    'OperationalStatus'
    @{n='SizeRemaining';e={"{0:N2}" -f ($_.SizeRemaining/ 1Gb)}}
    @{n='Size';e={"{0:N2}" -f ($_.Size / 1Gb)}}
    @{n='% Free';e={"{0:P}" -f ($_.SizeRemaining / $_.Size)}}
)

$Diskmgmt = Get-Volume | Select-Object $props 


foreach($dsk in $Diskmgmt)
{

    $dl = $dsk.DriveLetter
    $fsl = $dsk.FileSystemLabel
    $fs = $dsk.FileSystem
    $dt = $dsk.DriveType
    $hs = $dsk.HealthStatus
    $os = $dsk.OperationalStatus
    $sizer = $dsk.SizeRemaining
    $siz = $dsk.Size
    $PercentFree = $dsk.'% Free'
}

我需要连接到远程服务器并获取数据。

请让我知道如何为此命令连接远程计算机。

Get-Volume还有一个参数CimSession ,您可以在其中给它一个计算机名或一组计算机名。 在远程会话或远程计算机上运行 cmdlet。输入计算机名称或会话对象

这使您可以将代码简化为:

$props = @{Name = 'ComputerName'; Expression = {$_.PSComputerName}},
         'DriveLetter','FileSystemLabel','FileSystem','DriveType','HealthStatus','OperationalStatus',
         @{Name = 'SizeRemaining_GB'; Expression = {"{0:N2}" -f ($_.SizeRemaining/ 1Gb)}},
         @{Name = 'Size_GB'; Expression = {"{0:N2}" -f ($_.Size / 1Gb)}},
         @{Name = '% Free'; Expression = {"{0:P}" -f ($_.SizeRemaining / $_.Size)}}

$Diskmgmt = Get-Volume -CimSession server01, server02 | Select-Object $props | Sort-Object ComputerName, DriveLetter

这些是有关运行远程命令的 Microsoft 文档:

https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7.2

看起来它可能就像确保在目标计算机上启用远程执行然后运行这样的事情一样简单:

Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {Get-UICulture}

或者,如果您想以交互方式执行此操作,您可以使用Enter-PSSession ServerNameExit-PSSession连接/断开连接,上面的文档中也有更详细的说明。

编辑:

您可能还想查看远程变量:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-7.2

基本上,当您执行该脚本块时,它无法访问在该块之外定义的任何局部变量,除非您使用$using:myVariable sytanx 显式访问它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM