【发布时间】:2014-12-19 17:10:04
【问题描述】:
如何循环我的 powershel 脚本?它将本地 NTUSER.DAT 复制到文件服务器,我知道本地 NTUSER.DAT 文件可能被操作系统锁定,因此我的 COPY 可能会失败。我需要它尝试运行脚本 5 次,直到它最终可能失败。我希望到第 5 次尝试 NTUSER.DAT 文件不再被操作系统使用。
start-sleep -s 50
$time = Get-Date -Format "hh:mm"
Add-Content -Path "C:\Temp\time.txt" -Value "Script ran at $time"
$FQDN = "$Env:UserDomain"
$User = Get-WmiObject -Class Win32_NetworkLoginProfile | Sort-Object -Property LastLogon -Descending | Select-Object -Property * -First 1 | Where-Object {$_.LastLogon -match "(\d{14})"} | Foreach-Object { New-Object PSObject -Property @{ Name=$_.Name.TrimStart("$FQDN\") }} | Select -ExpandProperty Name
copy-item -path C:\Users\$User\NTUSER.DAT -destination \\10.10.10.10\Profiles$\$user.V2\ -force
copy-item -path C:\Users\$User\NTUSER.DAT -destination \\10.10.10.10\Profiles$\$user.V3\ -force
【问题讨论】:
-
没有研究? google 搜索的第一个结果:powershell loop
-
除了缺乏研究问题外,这还存在“错误的方法”和“无论如何都是坏主意”的问题。顺便说一句,访问锁定文件的正确方法是使用卷影服务。
-
@austinian 我不知道有严格的谷歌政策。我已经使用 google 进行了研究,并最终进行了一些练习。不幸的是,我对 Powershell 的专业知识只带我到此为止,这也是我提出这个问题的原因。我将继续使用谷歌找到我的问题的答案。谢谢。
-
@user3591436
I don't think the VSS will have access to it as it's opened by the OS in exclusive mode. I may be wrong and open to your further advice.是的,你不太对劲。查看探索 VSS 副本/卷的技术和工具,您应该能够找到提取ntuser.dat副本所需的内容。不过,这完全是另一个问题,而且时间太长,无法在这些 cmets 中填入字符数限制。 -
简单的解决方案是使用
robocopy,因为你可以告诉它重试多少次。如果您愿意,它将永远尝试。
标签: windows-server-2008 powershell scripting