抓取元数据并输出为XML



我试图让脚本读取第一行files.txt,获取所请求的数据元数据,然后输出.xml,移动到下一行并重复。

我希望每一行都有自己的元数据文件,然后下一行也这样做。

当前它创建所有单独的文件,但数据是合并和复制的。

  • files.txt包含完整路径和正在使用的文件
    D:datatestscript。ps1
    D:dataworkingfile.doc
    C:Windowstempoutput.txt

  • Filesv2.txt包含xml输出的文件名,并且是一致的例如
    D_data_testscript。ps1
    D_data_workingfile.doc
    c_windows_temp .txt

$logdir = "C:UsersgnomeDocuments"
$inputPath = Get-Content -Path "C:UsersgnomeDocumentsfiles.txt"
$inputSave = Get-Content -Path "C:UsersgnomeDocumentsfilesv2.txt"

#Get-*
$hash = Get-FileHash -Path $inputPath 
$acl = Get-Acl -Path $inputPath | Select-Object *
$metadata = Get-ChildItem -Path $inputPath | Select-Object *
#Loop each directory in $inputPath
#ForEach ($path in $inputPath){
$output = ForEach ($path in $inputPath){ 
Write-host Checking $path
ForEach($inputSave in $inputSave){
@{

#$log  = "$logdir$inputSave.xml"

sha256Hash = $hash
acl = $acl
metadata =$metadata

}
$output | Export-Clixml "$logdirtest1_$inputSave.xml" 

}

}
'''

根据您的评论,files.txt存储完整路径和文件名,filesv2.txt根据用于输出xml文件名的命名约定为这些文件提供新名称。

让两个数组在不同的文件中彼此分开是有点容易发生事故的,因为所有将文件名与约定名称连接起来的都是两个数组中的索引。

下面的首先从这些数组创建一个哈希表,假设索引匹配并且两个数组具有相同数量的元素

$logdir = "C:UsersgnomeDocuments"
$inputPath = @(Get-Content -Path "C:UsersgnomeDocumentsfiles.txt")   # full path and filenames 
$inputSave = @(Get-Content -Path "C:UsersgnomeDocumentsfilesv2.txt") # naming convention for the output
# create a Hashtable where the input from files.txt is key and the naming convention for the output xml is value
$filesHash = @{}
for ($i = 0; $i -lt $inputPath.Count; $i++) {
$filesHash[$inputPath[$i]] = $inputSave[$i]
}
# now iterate
$filesHash.GetEnumerator() | ForEach-Object {
Write-host Checking $_.Key 
$output = [PsCustomObject]@{
sha256Hash = Get-FileHash -Path $_.Key -Algorithm SHA256
acl        = Get-Acl -Path $_.Key
metadata   = Get-Item -Path $_.Key
}
$outFile = Join-Path -Path $logdir -ChildPath ('{0}.xml' -f $_.Value)
$output | Export-Clixml -Path $outFile
}

最新更新