Trouble with Get-ChildItem in PowerShell for MOF and MFL Files

0
5
Asked By TechyNinja92 On

I'm trying to convert a command line script for an SCCM SMS program uninstallation process into PowerShell. The command that works in CMD is: `dir /b *.mof *.mfl | findstr /v /i uninstall > moflist.txt & for /F %%s in (moflist.txt) do mofcomp %%s`.

I've managed to create this PowerShell command:
```powershell
Pushd "C:WindowsSystem32wbem"
Get-ChildItem -Filter {Name -like "*.mof" -or Name -like "*.mfl"}).FullName | Where-Object {(Get-Content $_) -notcontains "uninstall"} | ForEach-Object {mofcomp $_}
```

But I can't get this to work:
```powershell
Get-ChildItem -Path "C:WindowsSystem32wbem" -Filter {Name -like "*.mof" -or Name -like "*.mfl"}).FullName | Where-Object {(Get-Content $_) -notcontains "uninstall"} | ForEach-Object {mofcomp $_}
```

I'm getting an error message saying `Get-Content : cannot find path x: file because it does not exist`. It seems like it's not looking in the specified path at all. I would like to avoid changing directories in my script, and while I haven't tested as an admin yet, I need to make sure it throws an 'access denied' error as a normal user during testing. Any insights on what might be going wrong here?

1 Answer

Answered By CodeCrafter88 On

You might want to try this command instead:
```powershell
Get-ChildItem C:WindowsSystem32wbem* -Include *.mof, *.mfl | Select-String uninstall -NotMatch -List | ForEach-Object { mofcomp $_.Filename }
```
This version should work better. Also, for a shorter version, you could use aliases like this:
```powershell
gci C:WindowsSystem32wbem* -i *.mof, *.mfl | sls uninstall -NotMatch -List | % { mofcomp $_.Filename }
```

QueryWizard99 -

Here's another option you might consider:
```powershell
Select-String -Path C:WindowsSystem32wbem* -Include *.mof, *.mfl -Pattern uninstall -NotMatch -List |
ForEach-Object { mofcomp $_.Path }
```
Just keep in mind that these scripts, including what you have, aren't directly equivalent to your CMD code. The CMD command checks file names for the absence of "uninstall", while the PowerShell versions you provided check file content instead.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.