2

I'm figuring out how to loop trough a set of files and execute a program against these files, that outputs a log and tar file as it scans a hyper-v virtual hard disk.

What I already figured out is the following: gathering all VHDX on a volume. Putting them in a variable.

However I'm stuck at processing the foreach, and having a flexible output of the tar file and log name.

Example:

Get-ChildItem -Path $VDHXPath -Filter $Extension -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName }

Where $VHDXPath represents a customizable variable that represents the driveletter. This way I get an variable filled with one or multiple VHDX.

Than I need to execute the program against the queried/inventoried VHDX. The plane single file /straight command line is as following:

win.exe guid -o "tarfile.tar" -l "logfile.log" -p default --history "pathtovhdxfile.vhdx"

So I already tried many variants of how to do the foreach and call a command to talk against the VHDX. Example:

Foreach ($file in $files){
Start-Process -NoNewWindow -FilePath ".\win.exe" -ArgumentList "guid -o tarfile.tar -l logfile.tar -p default --history vhdxfile.vhdx"
}

Where the output of the tar file and log file, must match the name of the VHDX being processed.

I also tried:

Foreach ($file in $files){
& ".\win.exe" "guid -o tarfile.tar -l logfile.tar -p default --history vhdxfile.vhdx"
}

Unfortunately to no result. I either get the mention that the program isn't a valid powershell cmd-let or executable program. Or the program itself reacts that the tarfile, logfile and vhdxfiles are invalid, as the command line takes all the VHDX' at once.

The goal is to get a tar and logfile with the name of each VHDX it processes.

Any guidance would be highly appreciated

edit:

I'm getting a little bit furter:

$VDHXPath = "C:\Users\%USERNAME%"
$Extension = "*.VHDX"
Foreach ($file in Get-ChildItem -Path $VDHXPath -Filter $Extension -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName }) {
   
   & .\win.exe guid -o '.\$file.name'.tar -l '.\$file.name'.log -p default --history $file
}

So now the program walks through the files, but I'm only struggling with the per VHDX logfile name and tar file name.

2
  • "the output of the tar file and log file, must match the name of the VHDX being processed" - what do you mean by "must match"? Do you need to refer to tar files in the same directory as the vhdx file you've discovered with Get-ChildItem? Commented Oct 18, 2021 at 12:51
  • @MathiasR.Jessen I meant, that lets say I'm scanning a VDHX called Computer1 located in D:\Hyper-V\Computer1\Computer1.vhdx, the individual log and tar file name must be: computername1.tar and computername1.log, so for each vhdx I scan, I get a separate log and tar file. Commented Oct 18, 2021 at 13:24

1 Answer 1

2

Use $file.BaseName to get the file name without the extension. To expand a member expression (like $file.FullName) inside an expandable string, use the $() subexpression operator:

Foreach ($file in Get-ChildItem -Path $VDHXPath -Filter $Extension -Recurse -ErrorAction SilentlyContinue -Force) {
   $basename = $file.BaseName
   & .\win.exe guid -o ".\${basename}.tar" -l ".\${basename}.log" -p default --history "$($file.FullName)"
}
Sign up to request clarification or add additional context in comments.

4 Comments

Strangely enough I'm still presented with .log and tar.tar files, no filenames are generated. What am I missing?
@loewie1984 my bad, I forgot to remove | % { $_.FullName } from your existing script, try again
Your my hero! :) Kind regards from NL. See your also there. This works!
How did you find out about the $file.BaseName property?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.