我和这个有一点关系。我正在尝试使用 powershell 导入一个 CSV 文件,该文件是稍后在脚本中处理的 XML 的日志文件。
目标是在脚本顶部有一个 if 语句,以排除$_.FullName
已经以这种方式处理过的每个文件,如果脚本多次运行,它将读取此列而不是第二次处理该文件。
注意:我对powershell很陌生,抱歉语法草率
以下是我当前的代码和尝试
$sourceDir = "C:\test" #Production Folder "D:\Vodassets\_PreImport_Success"
$tempDir = "C:\test-temp"
$targetDir = "C:\test-copyback"
$Date = Get-Date -format MM-dd-yyyy
$Time = "{0:h:mm:ss tt zzz}" -f (get-date)
$LogFile = "Disney-Log.csv"
### Copy Disney Metadata.xml's to a temporary folder for the editting process
if (Test-Path $LogFile){
$ImportLog = Import-CSV $LogFile
$GetXML = @(Get-ChildItem $sourceDir -recurse -filter "Metadata.xml" -exclude {$LogFile.Fullname} | Where-Object {$_.FullName -like "*disney*"})
} else {
$GetXML = @(Get-ChildItem $sourceDir -recurse -filter "Metadata.xml" | Where-Object {$_.FullName -like "*disney*"})
}
$OutXML = ForEach-Object { $GetXML } | Select LastWriteTime,Fullname,DirectoryName
if ($GetXML.count -eq 0){
write-host "No files to copy. Ending Process....." -foregroundcolor yellow -backgroundcolor black
} else {
ForEach ($File in $GetXML)
{ $Path = $File.DirectoryName.Replace($sourceDir,$tempDir)
if (-not (Test-Path $Path)) {
Write-Host "Destination $Path doesn't exist, creating it." -foregroundcolor yellow -backgroundcolor black
New-Item -Path $Path -ItemType Directory
Copy-Item -Path $File.FullName -Destination $Path -ErrorAction silentlyContinue }
elseif (-not $?) { write-warning "Failed to copy $($File.Fullname)" }
else { write-host "Succesfully copied $($File.Fullname) to $($tempDir)" -foregroundcolor green -backgroundcolor black }
}
### Edit XML Process
ForEach ($File in $GetXML)
{ $Path = $File.DirectoryName.Replace($sourceDir,$tempDir)
if (Test-Path $Path) {
$xmlData = [xml](Get-Content $File.FullName)
foreach ($group in $xmlData){
$xmlData.assetpackages.assetpackage.'Type' = 'SVOD'
$xmlData.assetpackages.assetpackage.'Product' = 'SVOD'
$xmlData.assetpackages.assetpackage.'Name' = 'Disney Family Movies'
}
}
$xmlData.Save($File.Fullname)
}
$OutXML | Export-Csv $LogFile -NoTypeInformation -Force -Append
### Copy Files to VOD Import Server
$import = (Get-ChildItem $tempDir -recurse -filter "Metadata.xml" | Where-Object {$_.FullName -like "*disney*"})
if ($import.count -eq 0) {
write-host "No files to import. Ending Process....." -foregroundcolor yellow -backgroundcolor black
} else {
ForEach ($File in $import)
{ $Path = $File.DirectoryName.Replace($tempDir,$targetDir)
if (-not (Test-Path $Path)) {
Write-Host "Destination $Path doesn't exist, creating it." -foregroundcolor yellow -backgroundcolor black
New-Item -Path $Path -ItemType Directory
Copy-Item -Path $File.FullName -Destination $Path -ErrorAction silentlyContinue }
elseif (-not $?) { write-warning "Failed to copy $($File.Fullname)" }
else { write-host "Succesfully copied $($File.Fullname) to $($targetDir)" -foregroundcolor green -backgroundcolor black }
}
}
### Cleanup temporary directory
if (-not (Test-Path $tempDir | Where-Object {$_.FullName -like "*disney*"})){
Get-ChildItem $tempDir -recurse | % { Remove-Item $_.FullName -recurse } #Remove the -whatif to actual clean out the directory
}
}