none
Help writing script to identify files bigger than X and then send them via e-mail RRS feed

  • Question

  • Hey Scripting Guys can you help me trouble shoot or point me to where I can fix the following script. 

    The script that I have is as follows

    $files = @(Get-ChildItem -Path d:\Logs -Filter RPTG_SA_AllcBU*.Log) |Select-Object Name
    for($i=0; $i -lt $files.length; $i++){
    $file = get-childitem $files[$i]"
    if($file.length -gt 0){
    $files[$i] +" is greater than zero"
    Send-mailmessage -to "me@A.com" -from "you@a.com" -SmtpServer "smtp.com" -subject "hello" -Attachments $files[$i]
    }
    }

    When I ran in PS i get the script stuck in >>

    If I Echo $Files I see the list of files that I need but trying to Echo the $File[1] yields just the number. Appreciate any way you can help.

    Saturday, March 12, 2016 5:31 AM

Answers

  • Hint 2:

    $files=Get-ChildItem -Path d:\Logs -Filter RPTG_SA_AllcBU*.Log
    foreach($f -in $files){
         $file.length
    }


    \_(ツ)_/

    Saturday, March 12, 2016 11:31 AM
  • thank you very much for posting the answer. I was able to make everything work based on your hints. I am publishing the fix script based on the input provide and for the benefit of the community

    $files = Get-ChildItem -Path d:\Logs -Filter RPTG_SA_AllcBU*.Log |?{ $_.length -gt 0}
    foreach($f in $files){
         Send-mailmessage -to "someone@a.com" -from "sender@a.com" -SmtpServer "Smtp.server.com" -subject " -Attachments $f
    }

    Monday, March 14, 2016 12:35 PM

All replies

  • If you use "select name" you will not have any file objects to work with.

    The open >> means you are missing a closure or a quote.

    You want $files.Count not $files.length.


    \_(ツ)_/


    • Edited by jrv Saturday, March 12, 2016 11:28 AM
    Saturday, March 12, 2016 11:27 AM
  • Hint:

    Get-ChildItem -Path d:\Logs -Filter RPTG_SA_AllcBU*.Log |?{ $_.length -gt 0}


    \_(ツ)_/

    Saturday, March 12, 2016 11:30 AM
  • Hint 2:

    $files=Get-ChildItem -Path d:\Logs -Filter RPTG_SA_AllcBU*.Log
    foreach($f -in $files){
         $file.length
    }


    \_(ツ)_/

    Saturday, March 12, 2016 11:31 AM
  • thank you very much for posting the answer. I was able to make everything work based on your hints. I am publishing the fix script based on the input provide and for the benefit of the community

    $files = Get-ChildItem -Path d:\Logs -Filter RPTG_SA_AllcBU*.Log |?{ $_.length -gt 0}
    foreach($f in $files){
         Send-mailmessage -to "someone@a.com" -from "sender@a.com" -SmtpServer "Smtp.server.com" -subject " -Attachments $f
    }

    Monday, March 14, 2016 12:35 PM