AffectiveGnome
AffectiveGnome

Reputation: 25

Grabbing MetaData and outputting to XML

I'm trying to get the script to read the first line of files.txt, grab the data metadata requested and then output the .xml, move onto the next line and repeat.

I expect each line to have its own individual file with the meta data and then the next line to do the same.

Currently it creates all the individual files, but the data is combined and duplicated across them.

$logdir = "C:\Users\gnome\Documents"

$inputPath = Get-Content -Path "C:\Users\gnome\Documents\files.txt"
$inputSave = Get-Content -Path "C:\Users\gnome\Documents\filesv2.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 "$logdir\test1_$inputSave.xml" 
              
     }
             
}
'''

Upvotes: 0

Views: 104

Answers (1)

Theo
Theo

Reputation: 61188

From your comment, files.txt stores full path and filenames and filesv2.txt has new names for these files according to some naming convention to be used for the output xml filename.

Having both arrays separate from eachother in separate files is somewhat accident-prone, because all that links the file name to the convention name is the index in both arrays..

Below first creates a Hashtable from these arrays assuming the indices match and both arrays have the same number of elements

$logdir = "C:\Users\gnome\Documents"

$inputPath = @(Get-Content -Path "C:\Users\gnome\Documents\files.txt")   # full path and filenames 
$inputSave = @(Get-Content -Path "C:\Users\gnome\Documents\filesv2.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
}

Upvotes: 1

Related Questions