Instead of
$newest = $newhash.values -join ' '
try
$newest = $newhash.values
PowerShell will automatically expand an array of values into individual parameters.
Question
I have a PowerShell (v1.0 on Windows 7) script that compares the 7-Zip archive timestamp with the source file timestamps and re-creates the archive if any of the source files are newer. The sequence that logs what the script is doing (if it needs to re-create the archive) is:
$logFile = "$env:temp\$zipFile.log" # the backup log file
write-host " Compressing ${newest}${otherDocDirList}"
write-host " The log file is $logFile."
del $zipFile -erroraction:silentlycontinue
echo " $(pwd)"
echo " C:\progra~1\7-zip\7z.exe a $zipFile ${newest} ${otherDocDirs}"
C:\progra~1\7-zip\7z.exe a $zipFile ${newest} ${otherDocDirs} | set-content $logFile -encoding ascii
In some cases, this works file, specifically,
Compressing UserGuide_2303.docm, Graphics, and gettoolbar.
The log file is C:\Users\jim\AppData\Local\Temp\ST_SUG.7z.log.
C:\users\jim\desktop\ST\userguide\working
C:\progra~1\7-zip\7z.exe a ST_SUG.7z UserGuide_2303.docm Graphics gettoolbar
and
Compressing AdminGuide_0300.docm and Graphics.
The log file is C:\Users\jim\AppData\Local\Temp\ST_AUG.7z.log.
C:\users\jim\desktop\ST\adminguide\working
C:\progra~1\7-zip\7z.exe a ST_AUG.7z AdminGuide_0300.docm Graphics
but I get an error "SystemOverview_TrngMan_0103.pptx SystemOverview_TrngMan_0103.docm: WARNING: The system cannot find the file specified." in the log file when I try:
Compressing SystemOverview_TrngMan_0103.pptx SystemOverview_TrngMan_0103.docm and Graphics.
The log file is C:\Users\jim\AppData\Local\Temp\ST_SOTG.7z.log.
C:\users\jim\desktop\ST\Training\SystemOverview\working
C:\progra~1\7-zip\7z.exe a ST_SOTG.7z SystemOverview_TrngMan_0103.pptx SystemOverview_TrngMan_0103.docm Graphics
When I copy the command as echoed and run it in the command line, 7-Zip creates the archive without any error.
One difference between the first two examples and the one that fails is that $newest has 2 file names, separated by a space. The code that finds the most recent files uses a hash table and $newest is assigned a value with:
$newest = $newhash.Values -join ' '
I suspect there might be an extra character in the generated command line that 7-Zip does not expect when it parses the command line. I previously had the same error when I put 2 spaces between the file names. Any suggestions? Thanks in advance!!
Solution
Instead of
$newest = $newhash.values -join ' '
try
$newest = $newhash.values
PowerShell will automatically expand an array of values into individual parameters.