我遇到了一些奇怪的事情,我不明白。我的场景是:
我在C:\Functions中有多个.ps1文件。我想将文件的内容复制到一个文件(AllFunctions.ps1)中。文件CopyFunctions2AllFunctions.ps1是执行我的命令的文件。
$path="C:\Functions\*.ps1"
$destination="C:\Functions\AllFunctions.ps1"
Clear-Content -Path C:\Functions\AllFunctions.ps1
Get-Content -Path $path -Exclude "C:\Functions\CopyFunctions2AllFunctions.ps1" | Add-Content -Path $destination错误消息是德语的,但是它指出无法访问AllFunctions.ps1,因为它正被另一个进程使用。
如果替换代码,则代码可以正常工作
$path="C:\Functions\*.ps1"使用特定的文件名,如
$path="C:\Functions\Read-Date.ps1"-Force没有帮助
此外,代码在Add-Content -Path $destination之前一直有效。当我执行Get-Content...时,终端不仅没有显示.ps1文件中的内容,还显示了终端的内容,以及我在尝试时遇到的所有错误……
有没有人有主意?
发布于 2020-09-09 05:24:03
这段代码中有两件事需要修复,首先是新代码:
$path="C:\Functions"
$destination="C:\Functions\AllFunctions.ps1"
Clear-Content -Path C:\Functions\AllFunctions.ps1
$functions=Get-ChildItem -Path $path -Exclude CopyFunctions2Profile.ps1 | Get-Content
Add-Content -Path $destination -Value $functions问题#1
$path="C:\Functions\*.ps1"不工作,PS也在复制终端的内容,我不知道why...Therefore,我们在$path中不使用通配符。
因此,我们需要在代码中使用Get-Childitem,如下所示:
$functions=Get-ChildItem -Path $path -Exclude CopyFunctions2Profile.ps1 | Get-Content问题#2
使用管道,PS处理一个项目并通过管道发送它,然后发送第二个项目,依此类推。因此,当项目2被发送到Add-Content时,"AllFunctions.ps1“仍然用于项目1。
因此,我们需要将Get-Content保存在一个变量($functions)中,然后在Add-Content中使用它。
https://stackoverflow.com/questions/63801523
复制相似问题