你能幫忙嗎?我有以下 PowerShell 代碼將檔案 (test.txt) 復制到每個用戶的漫游檔案夾中。有用。但如果test-old.txt(這是以前重命名的檔案)已經存在,我想跳過復制檔案。提前謝謝你的幫助。
僅供參考 - 如果可行,我很高興有一個新代碼。
$Source = '\\ser04\h\AppsData\test.txt'
$Destination = 'C:\users\*\AppData\Roaming\SAP\Common\'
Get-ChildItem $Destination | ForEach-Object {Copy-Item -Path $Source -Destination $_ -Force}
uj5u.com熱心網友回復:
假設路徑test-old.txt是每個用戶的..\SAP\Common\檔案夾,您只需要if在現有代碼中添加一個條件來檢查該檔案是否存在于所述檔案夾中。一個簡單的方法是使用Test-Path.
如果要定位和檔案夾,請記住添加-Forceswitch 。Get-ChildItemDefaultDefault User
$Source = '\\ser04\h\AppsData\test.xml'
$Destination = 'C:\users\*\AppData\Roaming\SAP\Common\'
Get-ChildItem $Destination | ForEach-Object {
$testOld = Join-Path $_.FullName -ChildPath 'test-old.txt'
if(-not(Test-Path $testOld))
{
Copy-Item -Path $Source -Destination $_ -Force
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/338517.html
