相关疑难解决方法(0)

空文件夹上的 Powershell 比较对象

我正在编写一个简单的 DLL 复制脚本来帮助我的开发团队设置他们的本地环境。我检查了 build devdrop 文件夹并获得了一个 DLL 文件列表。然后我查看本地文件夹并复制任何较新的 DLL。

我的问题是当本地文件夹为空时(即脚本第一次运行),它会分崩离析。

$newFiles = Get-ChildItem -recurse -path "$drop\$latest\ship" -name
$oldFiles = Get-ChildItem -recurse -path $bin -name

$files = Compare-Object $newFiles $oldFiles -IncludeEqual -PassThru
$files | Where-Object { $_.SideIndicator -eq '<=' -or $_.SideIndicator -eq '=='} | % {
    Copy-Item -Path "$drop\$latest\ship\$_" -Destination $bin -Force
}
Run Code Online (Sandbox Code Playgroud)

当 $bin 为空时,gci 调用将 $oldFiles 保留为空,给我一个很好的错误:

比较对象:无法将参数绑定到参数“DifferenceObject”,因为它为空。

我已经四处搜索了,比较对象的所有操作方法似乎都没有解决这个问题。我可以检查文件夹是否为空,然后复制所有文件,但我想知道是否有更好的方法。

干杯。

powershell scripting

1
推荐指数
1
解决办法
6400
查看次数

标签 统计

powershell ×1

scripting ×1