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.
The files.txt contains the full path and files that is being collected with the metadata e.g.
D:\data\testscript.ps1
D:\data\workingfile.doc
C:\Windows\temp\output.txtFilesv2.txt contain the filename of the xml output and is consistent in array with files.txt e.g
D_data_testscript.ps1
D_data_workingfile.doc
C_Windows_temp_output.txt
$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"
}
}
'''
CodePudding user response:
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
}