首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell调用-命令严重的性能问题

PowerShell调用-命令严重的性能问题
EN

Stack Overflow用户
提问于 2017-09-19 20:11:21
回答 4查看 1.3K关注 0票数 3

我正忙着在远程系统上高效地运行脚本。当在本地运行时,命令需要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-lget-childitem -attributes !ReparsePoint都不能阻止它递归到一个修复点。相反,它似乎只过滤实际条目本身。

谢谢一堆人!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-09-19 21:35:56

问题似乎是对问题的回答。由于某些原因,当命令在本地运行时,将拒绝对解析点(如Documents and Settings)进行访问。一旦该命令远程运行,DIRGet-ChildItem都将恢复为修复点。

使用-Attributes !ReparsePoint for get-childitem/a-l开关用于DIR并不能阻止这一点。相反,这些开关只会阻止分析点出现在文件列表输出中,但不会阻止命令递归到这些文件夹中。

相反,我必须编写一个递归脚本并自己执行目录递归。在我的机器上慢了一点。而不是大约20秒的局部,它花了大约1分钟。它用了近2分钟的时间。

下面是我使用的代码:编辑:由于PowerShell 2.0、PowerShell远程处理和原始代码内存使用的所有问题,我不得不更新代码如下所示。

代码语言:javascript
复制
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
}
票数 3
EN

Stack Overflow用户

发布于 2017-09-19 20:26:11

如果它是一个大型目录结构,并且您只需要文件的完整路径名,那么通过使用遗留的dir命令而不是Get-ChildItem,您应该能够大大加快速度

代码语言:javascript
复制
invoke-command -ComputerName MACHINE -ScriptBlock {cmd /c dir c:\*.pst /s /b /a-d /a-l}
票数 0
EN

Stack Overflow用户

发布于 2017-09-20 12:27:56

尝试使用远程会话:

代码语言:javascript
复制
$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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46309006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档