Powershell Dir recursive with Depth

####################################################
####     Siva Mulpuru                        ########
####    <scipt_file.ps1> input.txt            ########
####    input.txt – list of fileshares/dir    ########
####################################################

cls
New-Item -ItemType directory “c:\tmp” 2>> $null
$folder_depth = 6
#Dynamic LogFile name
$log = “c:\tmp\$(get-date -Format yyymmdd-HHmmss).log”
#File Extensions to look for
$virus_extensions = @(“*.scr,*.exe”)

function DOWork([string]$path)
{
if ($path -ne $null){
Write-Output “$((get-date).ToString()) Inside $($path)” >> $log
$virus_files = (dir  $path -force -include *.scr)

foreach($virus_file in $virus_files)
{
if($virus_file -ne $NULL){
Write-Output “Virus File ” $virus_file.FULLNAME
#Extra Condition – FileSize < 70Kb
if($virus_file.Length -lt 70000)
{
Write-Output “$((get-date).ToString()) Red Alert:  $($virus_file.FULLNAME) ” >>  $log

}
}
}

}
}

if($args.count -eq 0)
{
Write-Output “Need the FileShare Input file”
exit

}
elseif(Test-Path $args[0]){
$input_file_contents = Get-Content $args[0]
foreach($line in $input_file_contents)
{
if(($line -ne $NULL) -and  (Test-Path $line))
{
$enum_root_dir = (dir $line | where { $_.mode -like “*d*”})
foreach ($subdir in $enum_root_dir)
{
$curr_dir=$subdir
#Folder Depth Condition
for($i=1;$i -le $folder_depth;$i++)
{
foreach ($subdir2 in $curr_dir)
{
if(($subdir2 -ne $NULL) -and  (Test-Path $subdir2.FULLNAME)){
DoWork($subdir2.FULLNAME+”\*”)
$curr_dir= (dir $subdir2.FULLNAME -force | where { $_.mode -like “*d*”})
}
else
{break}
}
}
}
}
}
}

else
{
Write-Output “$args[0] does not exit”
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s