2

Below PowerShell script should measure folder sizes on remote computers but apparently my $Desktop variable value is somehow ignored by Get-ChildItem and I get 0.00 MB. But when I replace $Desktop with explicit string i.e. "C:\Users\user1\Desktop" it works alright and I get e.g. 10.MB. Am I doing something wrong?

$file1="C:\computers_users.csv"
import-csv $file1 | ForEach-Object{
  $Desktop = "C:\Users\$($_.user)\Desktop"      
  Invoke-Command -ComputerName $_.computer -ScriptBlock {
    $FldSize =(Get-ChildItem $Desktop -recurse | Measure-Object -property length -sum)
    "{0:N2}" -f ($FldSize.sum / 1MB) + " MB"}
}
4

2 に答える 2

2

これを試して:

Invoke-Command -ComputerName $_.computer -ScriptBlock {
    $FldSize =(Get-ChildItem $args[0] -recurse | Measure-Object -property length -sum)
    "{0:N2}" -f ($FldSize.sum / 1MB) + " MB"} -argumentlist $desktop

引数を渡す必要が-argumentlist あるのは、invoke-command がセッション変数の呼び出しを認識しない新しい PowerShell セッションを作成するためです。

于 2013-06-10T07:42:53.200 に答える
2

ちなみに、PowerShell 3.0 では、$using を使用してローカル変数をリモート マシンに渡すことができます。

Invoke-Command ... -ScriptBlock { $FldSize =(Get-ChildItem $using:Desktop ...
于 2013-06-10T08:04:51.743 に答える