Search for text in all files and if matched move files to different folder

  • Gary Thanks again !

    I modified what you have mentioned as below:

    # Deal with each text file in turn

    Foreach ($file in get-ChildItem \\servername\OutputLogs\ -include *.txt -recurse)

    {

    # Assuming it is an array returned

    $messages = $file | select-String -pattern "Msg","Level" |get-ChildItem -name

    if (0 -lt $messages.Length )

    {

    Move-Item -path $file.FullName -destination "\\servername\OutputLogs\Emailed"

    }

    }

    #send mail

    $smtpServer = "SMTPServerName"

    $msg = new-object Net.Mail.MailMessage

    $smtp = new-object Net.Mail.SmtpClient($smtpServer)

    $msg.From = "test@domain.com"

    $msg.To.Add("test@domain.com")

    $msg.Subject = "Test email from server"

    $msg.Body = "This email is send uinsg powershell"

    $attachments = @()

    Foreach ($message in $messages)

    {

    $attachment = new-object Net.Mail.Attachment($message)

    $attachments = $attachments + $attachment

    $msg.Attachments.Add($attachment)

    # Can do following after loop? $msg.Attachments.Add($attachments)

    }

    $smtp.Send($msg)

    Foreach ($attachment in $attachments)

    {

    $attachment.Dispose() #to close the attached file correctly

    }

    Now it is throwing below error:

    New-Object : Exception calling ".ctor" with "1" argument(s): "Could not find file 'C:\update.sql.txt'."

    At \\servername\filetest.ps1:25 char:28

    + $attachment = new-object <<<< Net.Mail.Attachment($message)

    Cannot convert argument "0", with value: "test.sql.txt", for "Add" to type "System.Net.Mail.Attachment": "Cannot conver

    t value "test.sql.txt" to type "System.Net.Mail.Attachment". Error: "Could not find file 'C:\test.sql.txt'

    .""

    At \\servername\filetest.ps1:27 char:24

    + $msg.Attachments.Add( <<<< $attachment)

    Method invocation failed because [System.String] doesn't contain a method named 'Dispose'.

    At \\servername\filetest.ps1:34 char:23

    + $attachment.Dispose( <<<< ) #to close the attached file correctly

    I can understand that the file it is trying to email is not found as the script moves the file to another folder.

    Any ideas 🙂

    Thanks again,

    \\K

    ______________________________________________________________________________________________________________________________________________________________________________________
    HTH !
    Kin
    MCTS : 2005, 2008
    Active SQL Server Community Contributor 🙂

  • The file not found is the simplest.

    The following line uses only the file name:

    $attachment = new-object Net.Mail.Attachment($message)

    I can assume from the error message that you are running your script from the root of the C drive i.e. C:\. What you need to do is add the location to the filename i.e.

    $attachment = new-object Net.Mail.Attachment("\\servername\OutputLogs\Emailed\" + $message)

    So you have ended up with an array of nulls (I think) and null references do not implement the Dispose method 😉

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Hi Gary,

    I am still facing problems:

    I am using following final script.

    Foreach ($file in get-ChildItem \\serverName\NewFolder\SSIS_Script_Runner_Test\OutputLogs\ -include *.txt -recurse)

    {

    # Assuming it is an array returned

    $messages = $file | select-String -pattern "Msg","Level" |get-ChildItem -name

    if (0 -lt $messages.Length )

    {

    Move-Item -path $file.FullName -destination "\\serverName\NewFolder\SSIS_Script_Runner_Test\OutputLogs\Emailed"

    }

    }

    #send mail

    $smtpServer = "SMTPServerName"

    $msg = new-object Net.Mail.MailMessage

    $smtp = new-object Net.Mail.SmtpClient($smtpServer)

    $msg.From = "test@domain.com"

    $msg.To.Add("test@domain.com")

    $msg.Subject = "Test email from database"

    $msg.Body = "This email is send uinsg powershell"

    $attachments = @()

    Foreach ($message in $messages)

    {

    $attachment = new-object Net.Mail.Attachment("\\serverName\NewFolder\SSIS_Script_Runner_Test\OutputLogs\Emailed" + $message)

    $attachments = $attachments + $attachment

    $msg.Attachments.Add($attachment)

    # Can do following after loop? $msg.Attachments.Add($attachments)

    }

    $smtp.Send($msg)

    Foreach ($attachment in $attachments)

    {

    $attachment.Dispose() #to close the attached file correctly

    }

    If I use this with $attachment = new-object Net.Mail.Attachment("\\serverName\NewFolder\SSIS_Script_Runner_Test\OutputLogs\Emailed" + $message)

    Then I get below error message:

    PS Microsoft.PowerShell.Core\FileSystem::\\serverName\NewFolder\SSIS_Script_Runner_Test\powershellScripts> ./mov

    efiletest.ps1

    New-Object : Exception calling ".ctor" with "1" argument(s): "Access to the path '\\serverName\NewFolder\SSIS_S

    cript_Runner_Test\OutputLogs\Emailed' is denied."

    At \\serverName\NewFolder\SSIS_Script_Runner_Test\powershellScripts\movefiletest.ps1:25 char:28

    + $attachment = new-object <<<< Net.Mail.Attachment("\\serverName\NewFolder\SSIS_Script_Runner_Test\OutputL

    ogs\Emailed" + $message)

    Exception calling "Add" with "1" argument(s): "Value cannot be null.

    Parameter name: item"

    At \\serverName\NewFolder\SSIS_Script_Runner_Test\powershellScripts\movefiletest.ps1:27 char:24

    + $msg.Attachments.Add( <<<< $attachment)

    You cannot call a method on a null-valued expression.

    At \\serverName\NewFolder\SSIS_Script_Runner_Test\powershellScripts\movefiletest.ps1:34 char:23

    + $attachment.Dispose( <<<< ) #to close the attached file correctly

    and when I use with $attachment = new-object Net.Mail.Attachment("\\serverName\NewFolder\SSIS_Script_Runner_Test\OutputLogs\Emailed\" + $message)

    I get :

    New-Object : Exception calling ".ctor" with "1" argument(s): "The filename, directory name, or volume label syntax is i

    ncorrect.

    "

    At \\serverName\NewFolder\SSIS_Script_Runner_Test\powershellScripts\movefiletest.ps1:25 char:28

    + $attachment = new-object <<<< Net.Mail.Attachment("\\serverName\NewFolder\SSIS_Script_Runner_Test\OutputL

    ogs\Emailed\ " + $message)

    Exception calling "Add" with "1" argument(s): "Value cannot be null.

    Parameter name: item"

    At \\serverName\NewFolder\SSIS_Script_Runner_Test\powershellScripts\movefiletest.ps1:27 char:24

    + $msg.Attachments.Add( <<<< $attachment)

    You cannot call a method on a null-valued expression.

    At \\serverName\NewFolder\SSIS_Script_Runner_Test\powershellScripts\movefiletest.ps1:34 char:23

    + $attachment.Dispose( <<<< ) #to close the attached file correctly

    In both case I am getting emails but WITHOUT attachment. I want the attachment too. :hehe:

    I am not sure why it is giving me access denied error along with other errors that prevent the attachment to be emailed ! :unsure:

    Please help me out !

    Thanks,

    \\K

    ______________________________________________________________________________________________________________________________________________________________________________________
    HTH !
    Kin
    MCTS : 2005, 2008
    Active SQL Server Community Contributor 🙂

  • Hi \\K,

    I think this is down to debugging now. At this point you have all that you need to know. Try adding the constant string and the filename together and outputting it before using it - you can then copy and paste it into an explorer address bar to check it. Or try File.Exists method.

    Good luck.

    As they say "You can lead a horse to water but a pencil must be lead, mate!!!"

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Thanks Gary !:-)

    ______________________________________________________________________________________________________________________________________________________________________________________
    HTH !
    Kin
    MCTS : 2005, 2008
    Active SQL Server Community Contributor 🙂

Viewing 5 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply