wmic + powershell run a command to search and remove files

0

I want to clean a website before a deploy. In order to do that, I use WMIC to run remotely and Powershell to remove all the files except some specific ones:

WMIC /node:server /user:user /password:pass process call create "powershell -Command 'Get-ChildItem C:\Optima-QA2 -File -Name -Recurse -Force -Exclude .*\.7z, .*\.lic, [Ww]eb\.config | foreach($_) {remove-item $_}'"

but it seems to have problems with the pipe and the foreach, to parse and catch properly the command.

How can I do that? Is there other possibility to clean a website using regex for exclusions? PsExec is not working in the environment, so it's not an option to me, I'm afraid.

Luis

Posted 2018-03-23T22:29:39.300

Reputation: 111

Answers

0

Try eliminating the pipe by calling Remove-Item directly:

Remove-Item (Get-ChildItem C:\Optima-QA2 -File -Name -Recurse -Force -Exclude .*\.7z, .*\.lic, [Ww]eb\.config) -WhatIf

The WhatIf parameter will show you what would happen without actually performing the removal. This is a great way to test what would be deleted without the parameter.

root

Posted 2018-03-23T22:29:39.300

Reputation: 2 992