Drive变量在Powershell中不起作用

时间:2014-07-14 05:14:29

标签: powershell

我有以下脚本列出服务器列表中的大文件。不幸的是它没有列出任何东西。但是,如果我用字符串D:\替换$ _。Name,它可以正常工作。

$servers = Get-Content "servers1.txt" | Select-String -pattern `
    "^[^#]"
foreach ($line in $servers) {
    $svr = echo $line | %{$_.Line.split(':')[2]}
    Get-WmiObject Win32_Volume -ComputerName $svr -ErrorAction SilentlyContinue |
    Select-Object __SERVER,Name |
    foreach {
        Invoke-command {Get-ChildItem -path $_.Name -rec | Where-Object `
        -FilterScript {($_.Length -ge 3GB) -and ($_.Name -notlike "*.mdf")}} -computername $svr
    }
} 

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这是一个范围问题:在远程命令$ _中。名称不存在。试试这个:

Invoke-command {
    param ($Path)
    Get-ChildItem -path $Path -rec | Where-Object {($_.Length -ge 3GB) -and ($_.Name -notlike "*.mdf")}
} -ComputerName $svr -ArgumentList $_.Name
相关问题