Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to test Get-ChildItem for no files as part of Invoke-Command?

$
0
0

I have the following script:

try {Invoke-Command-Computer $Server -ScriptBlock{
    param ($dir, $name)Get-ChildItem-Path $dir |Where{$_.Name-Match"$name"}|Remove-Item-confirm:$false -Recurse-Verbose}-ArgumentList $Directory, $DBWrite-Host"`r`nsuccessfull! "-foregroundcolor yellow -backgroundcolor black}
catch {
    write-host "`r`nFAILED!"-foregroundcolor red -backgroundcolor blackWrite-Host"$($error[0])`r`n"-foregroundcolor magenta -backgroundcolor black}

if no files exist to delete...it currently still says "successful" as output...can i refine this to say "there was no files to delete" else "successfully deleted x number of files"?

also, the recurse on verbose is needed because otherwise i am forced to manually confirm even with confirm parameter. I think its because there are many sub items inside the target folder(s) i am looking to delete...

however, i get a TON of verbose messages for every single one of those items saying

VERBOSE: Performing the operation "Remove Directory" on target \name1\subitem

VERBOSE: Performing the operation "Remove Directory" on target \name1\subitem1

VERBOSE: Performing the operation "Remove Directory" on target \name1\subitem2

can i make it so that it just prints verbose on a folder level instead for every single item?

VERBOSE: Performing the operation "Remove Directory" on target \name1


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>