简体   繁体   English

Powershell使用调用命令会话来获取远程计算机上的子代大小

[英]Powershell Use Invoke-Command Session to Get-Childitem sizes on a remote computer

I am trying to use powershell to remotely access a computer and get the size of each sub-directory of a folder. 我正在尝试使用Powershell远程访问计算机并获取文件夹的每个子目录的大小。

I am using the script to get each of the folder sizes and it works successfully: 我正在使用脚本获取每个文件夹的大小,并且可以成功运行:

$log = "F:\logfile.txt" 
$startFolder = "C:\"
$colItems = Get-ChildItem $startFolder  | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
foreach ($i in $colItems){
   $itemSum = Get-ChildItem ("$startFolder\" + $i.Name) -recurse | Measure-Object -property length -sum
   "$startFolder\$i -- " + "{0:N2}" -f ($itemSum.sum / 1MB) + " MB" >> $log
   }

This is how I tried to incorporate it using the Invoke-Command and it is yielding no results. 这就是我尝试使用Invoke-Command合并它的方式,但未产生任何结果。

#login info
$username = "domain\user"
$password = 'password'

$log = "C:\logfile.txt" 
$startFolder = "comp-name\e"

#setup login credentials
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr

$session = new-pssession "comp-name" -Credential $cred

Invoke-Command -Session $session -ScriptBlock {$colItems = Get-ChildItem $startFolder  | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
foreach ($i in $colItems){
   $itemSum = Get-ChildItem ("$startFolder\" + $i.Name) -recurse | Measure-Object -property length -sum
   "$startFolder\$i -- " + "{0:N2}" -f ($itemSum.sum / 1GB) + " GB" >> $log
   }
   }

I have already done the step of enabling ps-remoting on both computers. 我已经完成了在两台计算机上启用ps-remoting的步骤。

Thanks in advance for the help! 先谢谢您的帮助!

The variable $startFolder needs to be passed into the scriptblock eg: 需要将变量$startFolder传递到$startFolder ,例如:

Invoke-Command -Session $session -ScriptBlock {param($path,$log) 
    $colItems = Get-ChildItem $path | Where-Object {$_.PSIsContainer} | Sort-Object
    foreach ($i in $colItems) {
        $itemSum = Get-ChildItem "$path\$($i.Name)" -recurse | Measure-Object -property length -sum
        "$startFolder\$i -- " + "{0:N2}" -f ($itemSum.sum / 1GB) + " GB" >> $log
    }
} -Arg $startFolder,$logFilePath

I was able to get this working (no redirect to log file): 我能够使它正常工作(没有重定向到日志文件):

23# Invoke-Command acme -ScriptBlock {param($path)
>>>     $colItems = Get-ChildItem $path | Where-Object {$_.PSIsContainer} | Sort-Object
>>>     foreach ($i in $colItems) {
>>>         $itemSum = Get-ChildItem $i.FullName -recurse | Measure-Object -property length -sum
>>>         "$startFolder\$i -- " + "{0:N2}" -f ($itemSum.sum / 1MB) + " MB"
>>>     }
>>> } -Arg c:\bin
>>>
\Orca -- 3.63 MB
\Reg -- 0.06 MB
\Src -- 0.25 MB
\sym -- 19.09 MB
\x64 -- 0.71 MB

暂无
暂无

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

相关问题 使用Invoke-Command和Get-Childitem的Powershell无法传递内容 - Powershell using Invoke-Command and Get-Childitem not delivering content 获取子项 (gci) | 选择字符串(模式)故障转移远程调用命令(icm) - Get-ChildItem (gci) | select-string (pattern) fails over remote invoke-command (icm) 如何在Invoke-Command中测试Get-ChildItem是否没有文件? - How to test Get-ChildItem for no files as part of Invoke-Command? 在 PowerShell 中的调用命令 session 中将文件从远程计算机复制到本地计算机 - Copy file from remote computer to local computer within an Invoke-command session in PowerShell Powershell Get-ChildItem命令 - Powershell Get-ChildItem command 如何通过Invoke-Command在远程计算机上调用Powershell版本2 - How to invoke Powershell version 2 on remote computer via Invoke-Command Get-ChildItem Cert:\\(本地执行) vs. Invoke-Command 结果差异混淆? - Get-ChildItem Cert:\ (Locally executed) vs. Invoke-Command results difference confusion? 调用命令不从我的Get-ChildItem返回任何内容 - Invoke-Command doesn't return anything from my Get-ChildItem Powershell:使用带有自定义功能的invoke-command在远程计算机上执行并在主机中显示输出 - Powershell: use invoke-command with custom function to execute at remote computer and display output in the host computer 如何在远程计算机上通过 powershell invoke-command 使用 psexec? (无效句柄) - Howto use psexec via powershell invoke-command on a remote computer? (invalid handle)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM