我正忙着在远程系统上高效地运行脚本。当在本地运行时,命令需要20秒。当使用Invoke-Command运行时,命令需要10或15分钟--即使“远程”计算机是我的本地计算机。
有人能向我解释一下这两个命令之间的区别吗?为什么调用命令花费这么长时间?
在机器上本地运行:get-childitem C:\ -Filter *.pst -Recurse -Force -ErrorAction SilentlyContinue
在\MACHINE上远程运行(行为与我的本地计算机或远程计算机的行为相同:invoke-command -ComputerName MACHINE -ScriptBlock {get-childitem C:\ -Filter *.pst -Recurse -Force -ErrorAction SilentlyContinue}
注意:该命令返回5个文件对象
编辑:我认为问题的一部分可能是修复点。在本地运行时,get-childitem (和DIR /a-l)不要跟随连接点。当远程运行时,即使我使用-attributes !ReparsePoint交换机,也是如此)
EDIT2:但是,如果我运行命令invoke-command -ComputerName MACHINE -ScriptBlock {get-childitem C:\ -Attributes !ReparsePoint -Force -ErrorAction SilentlyContinue},我就看不到连接点(即文档和设置)。因此,很明显,DIR /a-l和get-childitem -attributes !ReparsePoint都不能阻止它递归到一个修复点。相反,它似乎只过滤实际条目本身。
谢谢一堆人!
发布于 2017-09-19 21:35:56
问题似乎是对问题的回答。由于某些原因,当命令在本地运行时,将拒绝对解析点(如Documents and Settings)进行访问。一旦该命令远程运行,DIR和Get-ChildItem都将恢复为修复点。
使用-Attributes !ReparsePoint for get-childitem和/a-l开关用于DIR并不能阻止这一点。相反,这些开关只会阻止分析点出现在文件列表输出中,但不会阻止命令递归到这些文件夹中。
相反,我必须编写一个递归脚本并自己执行目录递归。在我的机器上慢了一点。而不是大约20秒的局部,它花了大约1分钟。它用了近2分钟的时间。
下面是我使用的代码:编辑:由于PowerShell 2.0、PowerShell远程处理和原始代码内存使用的所有问题,我不得不更新代码如下所示。
function RecurseFolder($path) {
$files=@()
$directory = @(get-childitem $path -Force -ErrorAction SilentlyContinue | Select FullName,Attributes | Where-Object {$_.Attributes -like "*directory*" -and $_.Attributes -notlike "*reparsepoint*"})
foreach ($folder in $directory) { $files+=@(RecurseFolder($folder.FullName)) }
$files+=@(get-childitem $path -Filter "*.pst" -Force -ErrorAction SilentlyContinue | Where-Object {$_.Attributes -notlike "*directory*" -and $_.Attributes -notlike "*reparsepoint*"})
$files
}发布于 2017-09-19 20:26:11
如果它是一个大型目录结构,并且您只需要文件的完整路径名,那么通过使用遗留的dir命令而不是Get-ChildItem,您应该能够大大加快速度
invoke-command -ComputerName MACHINE -ScriptBlock {cmd /c dir c:\*.pst /s /b /a-d /a-l}发布于 2017-09-20 12:27:56
尝试使用远程会话:
$yourLoginName = 'loginname'
$server = 'tagetserver'
$t = New-PSSession $server -Authentication CredSSP -Credential (Get-Credential $yourLoginName)
cls
"$(get-date) - Start"
$r = Invoke-Command -Session $t -ScriptBlock{[System.IO.Directory]::EnumerateFiles('c:\','*.pst','AllDirectories')}
"$(get-date) - Finish"https://stackoverflow.com/questions/46309006
复制相似问题