0

I'm trying to generate a report for all WIM files in my MDT Deployment Share. Basically, I think need to do a ForEach loop on all the WIM files found. I have what I think should work but, obviously, it doesn't. How far off am I?

$WimPath = "G:\DeploymentShare\Operating Systems"

Get-ChildItem -Path $WimPath -Filter *.wim -Recurse -File | Select-Object -ExpandProperty VersionInfo | Select-Object FileName | ForEach-Object { Get-WindowsImage -ImagePath $_ }

The error I'm seeing is nagging about the Parameter being incorrect for the Get-WindowsImage command.

Get-WindowsImage : The parameter is incorrect.
At line:3 char:147
+ ... t-Object FileName | ForEach-Object { Get-WindowsImage -ImagePath $_ }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm thinking my Select-Object isn't working like I think it should or I'm not using the pipeline correctly in my Get-WindowsImage command.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
RKillcrazy
  • 125
  • 8
  • 1
    try `Get-ChildItem -Path $WimPath -Filter *.wim -Recurse -File | ForEach-Object { Get-WindowsImage -ImagePath $_.fullname }` – brendan62269 Oct 02 '17 at 14:48
  • 1
    @Brendand62269, well done! I was wondering if I even needed the `Select-Object` at all. I guess I didn't... How do I mark you as the one who answered it correctly? – RKillcrazy Oct 02 '17 at 14:50

1 Answers1

1

I'm a PowerShell noob and don't fully understand this, but I think what is going on can be explained by first focusing on this part of the command:

Get-ChildItem -Path $WimPath -Filter *.wim -Recurse -File | Select-Object -ExpandProperty VersionInfo | Select-Object FileName 

To get this far, we first get all the *.wim files in your path, we expand the VersionInfo property, and then select the FileName. In the console, that will show you results like this:

FileName
--------
[files here]

The trick is in understanding what PowerShell is telling you with this output. The fact you see a FileName header means the pipeline has a stream of Objects with one property named FileName. Then we send that stream of Objects to ForEach-Object and look at the $_ special variable.

Hopefully it is clearer now what is going on. Get-WindowsImage -ImagePath $_ wants to see a string value holding the path of a *.wim file. But we sent it an object with one property.

You can fix this a few ways... adding ExpandProperty to the second Select-Object would probably do it. But really there's no reason for two Select-Objects in there at all. I think you could just do this:

Get-ChildItem -Path $WimPath -Filter *.wim -Recurse -File | Select-Object -ExpandProperty FullName | ForEach-Object { Get-WindowsImage -ImagePath $_ }

And the trick here is the string representation you see in the shell from Get-ChildItem doesn't necessarily show every property in the object. There wouldn't be space. The FileName was always there, and you can see it by checking the Get-Member cmdlet, like so:

Get-ChildItem -Path $WimPath -Filter *.wim -Recurse -File | Get-Member
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Like you, I'm a bit of a _noob_. Good catch on the **string** value needed for the `Get-WindowsImage` command. Now that I look at it, the documentation states that's exactly what's needed. I was messing with `Get-ChildItem -Path $WimPath -Filter *.wim -Recurse -File | Select-Object -ExpandProperty VersionInfo | Select-Object FileName` and _thought_ it was giving me what I needed. I didn't fully understand what type of data it was giving me and I still don't fully understand how to figure it out. – RKillcrazy Oct 02 '17 at 15:16