函数调用变量的问题


PowerShell交流中心函数调用变量的问题
0
humblejohn asked 3年 ago

function logicaldisk ($computername)
{
Get-WmiObject -Class win32_LogicalDisk -ComputerName $computername -Filter “drivetype=3” |
Sort-Object DeviceID |
Format-Table -Property $computername,DeviceID,
@{label=’FreeSpace(MB)’;expression={$_.freespace/1MB -as [int]}},
@{label=’Size(GB)’;expression={$_.Size/1GB -as [int]}},
@{label=’%Free’;expression={$_.freespace / $_.size * 100 -as [int]}}
}
$server=get-content “c:\servers.txt”
foreach($i in $server)
{
logicaldisk $i
}
 
# 在server.txt中写了2个服务器名字,一个服务器名字一行。 在运行这个脚本时报下面的错,请问这是什么意思? 难道logicaldisk $i 不能这么调用?
Get-WmiObject : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty.
Supply an argument that is not null or empty and then try the command again.
At D:\bt\Get-logicaldisk.ps1:10 char:54
+ Get-WmiObject -Class win32_LogicalDisk -ComputerName $computername -Filter “driv …
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObj
ectCommand

×用微信扫描并分享
1 Answers
0
Mooser Lee 管理员 answered 3年 ago

最大的可能是你的server.txt中可能有空行

humblejohn replied 3年 ago

确实是空行问题,但是去掉空行后,又爆出这个错误。如果我直接运行LogicalDisk DCSER01的话,这个函数是可以运行的。
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:3 char:1
+ Get-WmiObject -Class win32_LogicalDisk -ComputerName $computername -Filter “driv …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:3 char:1
+ Get-WmiObject -Class win32_LogicalDisk -ComputerName $computername -Filter “driv …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

humblejohn replied 3年 ago

我使用了for循环,可以了。

function logicaldisk ($computername)
{
Get-WmiObject -Class win32_LogicalDisk -ComputerName $computername -Filter “drivetype=3” |
Sort-Object DeviceID |
Format-Table -Property $computername,DeviceID,
@{label=’FreeSpace(MB)’;expression={$_.freespace/1MB -as [int]}},
@{label=’Size(GB)’;expression={$_.Size/1GB -as [int]}},
@{label=’%Free’;expression={$_.freespace / $_.size * 100 -as [int]}}
}

$server=get-content “c:\servers.txt”
$j=$server.count
for ($i=0;$i -le $j;$i++)
{
logicaldisk $server[$i]
}

但是老出现这个报错,

PowerCLI D:\bt> .\Get-logicaldisk1.ps1 | Export-csv c:\se.csv
Get-WmiObject : Cannot validate argument on parameter ‘ComputerName’. The
argument is null or empty. Supply an argument that is not null or empty and
then try the command again.
At D:\bt\Get-logicaldisk1.ps1:3 char:54
+ Get-WmiObject -Class win32_LogicalDisk -ComputerName $computername -Filter
“driv …
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindi
ngValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
Shell.Commands.GetWmiObjectCommand