none
POWERSHELL Reading INFO FROM A TEXT FILE

    Question

  • hi i am a newbie to powershell and would be really greatfull for you all help.

    i have a log file that capture spam email info for every user in our company. i want to go through the log file and create single files with each user info.

    if the user name repeats the data should append to same file .  username == email address

     I am reading some data from a Text file (log file). i want to read the info line by line and save the info to back to a text file .

    this new text file should ave the name of the of  a VARIABLE. 

    INFORMATION STORED IN THE TEXT FILE(LOG)..

    "01/22/13 02:14:32","SpamRazer","bounce-2098686-20571980@m40.bm02.net","robert.masson@westwind.com","[SPAM-SpamRazer] - Download The 2013 
    "01/11/13 00:26:20","SpamRazer","service@peypal.com","kenroy.clarke@westwind.com","[SPAM-SpamRazer] - kenny update Your Account Has Been Limited 

    "01/27/13 02:14:32","SpamRazer","bounce-2098686-20571980@m40.bm02.net","robert.masson@westwind.com","[SPAM-SpamRazer] -test mail 

    i want the new text file to save as the email address "robert.masson@westwinds.txt"  that would be index 3

    if the name repeat, it should be append to the text file name should not be overwrite. 

    this my code ************

    #$secondline| Out-File C:\Users\admin.ittech\Desktop\test3.txt





    #$file= "C:\Users\admin.ittech\Desktop\mass.txt"
    $file= "C:\Users\admin.ittech\Desktop\output.txt"

    (Get-Content $File) | Foreach-Object {$_ -replace "`"", ""} | Set-Content $File

    #(gc C:\Users\admin.ittech\Desktop\mass.txt) -replace ","  , "`r`n" | sc C:\Users\admin.ittech\Desktop\hot17.txt
    (gc C:\Users\admin.ittech\Desktop\output.txt) -replace ","  , "`r`n" | sc C:\Users\admin.ittech\Desktop\output.txt
    #(gc C:\Users\admin.ittech\Desktop\mass.txt) -replace ","  , "`r`n" | Set-Content $File

    $h=Get-Content -Path C:\Users\admin.ittech\Desktop\output.txt | Select-Object -Index 3
    $h
    Write-Host ('"{0}"' -f $h)

    $a= Get-Content -Path C:\Users\admin.ittech\Desktop\output.txt | Select-Object -Index 0
    $b= Get-Content -Path C:\Users\admin.ittech\Desktop\output.txt | Select-Object -Index 2
    $c=Get-Content -Path C:\Users\admin.ittech\Desktop\output.txt | Select-Object -Index 4



    $a| Out-File C:\Users\admin.ittech\Desktop\loo\ $h.txt 
    $b| Out-File C:\Users\admin.ittech\Desktop\loo\ $h.txt 
    $c| Out-File C:\Users\admin.ittech\Desktop\loo\ $h.txt 

    Get-Process |C:\Users\admin.ittech\Desktop\remove.ps1 

    help needed how do i get the output to save as the user name ????

    Monday, January 21, 2013 4:12 PM

Answers

  • Get-Content mass.txt | Foreach {$n=$_.split(",")[3].Trim('"');$n | out-file "$n.txt" -Append}
    • Marked as answer by Dontez Monday, January 21, 2013 4:54 PM
    Monday, January 21, 2013 4:46 PM
  • For new files:

    Import-CSV mass.txt -Header 0,1,2,3,4 | Group {$_."3".split("@")[0]} | Foreach {
    	$n = $_.Name + ".txt"
    	$m = $_."3"
    	$_.Group | Foreach {"{0} {1} {2}" -f $_."0",$_."4",$_."3" | out-file $n -append}
    	Send-MailMessage -From admin@westmed.com -To $m -Attachments $n -Subject "Spam" -Body "Spam"	
    }

    If you have files in a folder:

    Get-ChildItem *.txt | Foreach {Send-MailMessage -From admin@westmed.com -To $_.basename -Attachments $_.fullname -Subject "Spam" -Body "Spam"}


    • Edited by KazunMVP Monday, January 21, 2013 5:45 PM
    • Marked as answer by Dontez Monday, January 21, 2013 5:55 PM
    Monday, January 21, 2013 5:45 PM

All replies

  • Get-Content mass.txt | Foreach {$_.split(",")[3].Trim('"')} | Out-File result.txt
    Monday, January 21, 2013 4:35 PM
  • thanks for your help , but that is not exactly what i want

    i want to create single files that save as Index 3.txt

    example ****

    "01/22/13 02:14:32","SpamRazer","bounce-2098686-20571980@m40.bm02.net","robert.masson@westwind.com","[SPAM-SpamRazer] - Download The 2013 

    "01/11/13 00:26:20","SpamRazer","service@peypal.com","kenroy.clarke@westwind.com","[SPAM-SpamRazer] - kenny update Your Account Has Been Limited

     

    file name should be  :  robert.masson.txt   .. file should be name of index 3

    file name should be  :  kenroy.clarke@westwind.txt   .. file should be name of index 3


     help will be greatly appreciated.
    Monday, January 21, 2013 4:42 PM
  • Get-Content mass.txt | Foreach {$n=$_.split(",")[3].Trim('"');$n | out-file "$n.txt" -Append}
    • Marked as answer by Dontez Monday, January 21, 2013 4:54 PM
    Monday, January 21, 2013 4:46 PM
  • Thank you so much .. this helps alot..

     just a quick question .. i want to capture index 0 and index 4 in the individual text file .. how do i modify the script to do that .?

    example 

    "01/22/13 02:14:32","SpamRazer","bounce-2098686-20571980@m40.bm02.net","robert.masson@westwind.com","[SPAM-SpamRazer] - Download The 2013 

    robert.masson.txt should contain 

    01/22/13 02:14:32  "[SPAM-SpamRazer] - Download The 2013  robert.masson@westwind.com

    Monday, January 21, 2013 4:57 PM
  • Import-CSV mass.txt -Header 0,1,2,3,4 | Foreach {
    	$n = $_."3".split("@")[0]
    	"{0} {1} {2}" -f $_."0",$_."4",$_."3" | out-file "$n.txt" -append
    }

    Monday, January 21, 2013 5:03 PM
  •  i would like to email each file to each user

    eg : file name

    robert.masson@westmed.txt ... i want to email that file to robert.masson@westmed.com

    kev.smith@westmed.txt ... i want to email that file to kev.smith@westmed@westmed.com

    within the text file the email address is index 3.. 

    is there a way to accomplish that 

    Monday, January 21, 2013 5:27 PM
  • the folder consist of 3 file 

    robert.masson@westmed.txt

    kev.smith@westmed.txt

    ander.wison@westmed.txt

    within the text file the email address is index 3 or file name ..  i would like to email each file to each user


     Thank you for your assistance so far 


    Monday, January 21, 2013 5:35 PM
  • For new files:

    Import-CSV mass.txt -Header 0,1,2,3,4 | Group {$_."3".split("@")[0]} | Foreach {
    	$n = $_.Name + ".txt"
    	$m = $_."3"
    	$_.Group | Foreach {"{0} {1} {2}" -f $_."0",$_."4",$_."3" | out-file $n -append}
    	Send-MailMessage -From admin@westmed.com -To $m -Attachments $n -Subject "Spam" -Body "Spam"	
    }

    If you have files in a folder:

    Get-ChildItem *.txt | Foreach {Send-MailMessage -From admin@westmed.com -To $_.basename -Attachments $_.fullname -Subject "Spam" -Body "Spam"}


    • Edited by KazunMVP Monday, January 21, 2013 5:45 PM
    • Marked as answer by Dontez Monday, January 21, 2013 5:55 PM
    Monday, January 21, 2013 5:45 PM
  • this will only give me the output for the first username "robert.masson@westmed.com"

    i would like to get the same info for david.smith@westmed.com

    index [3] contain two email address sepearated by a comma .

    only the first email address it is reading 



    Import-CSV  I:\Script\SpamScript\output\output.txt -Header 0,1,2,3,4 | Foreach {


    $n = $_."3".split(",")[0]

    "{0} {1} {2}" -f $_."0",$_."4",$_."3" | out-file I:\Script\SpamScript\store\"$n.txt" -append

    ********output.txt contains*********

    "01/22/13 02:14:32","SpamRazer","bounce-2098686-20571980@m40.bm02.net","robert.masson@westwind.com,david.smith@westmed.com","[SPAM-SpamRazer] - Download The 2013 


    Tuesday, January 22, 2013 8:02 PM