Unzip Password Protected ZIP File And Send It’s Content By Email

Home Forums Scripting PowerShell Unzip Password Protected ZIP File And Send It’s Content By Email

This topic contains 2 replies, has 2 voices, and was last updated by  igor7 1 year, 3 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts

  • igor7
    Member
    #167208

    Hi, there!!
    Here is my goal, I’m trying accomplish with Powershell script.
    I have administrative user that need to be able extract csv file from password protected zip file and send this csv file by email.
    Given directory has many zip files named by username (e.g. dana.zip) and protected with the same password (123456). The administrative user (who know the password for zip files) needs to run powershell script which asking for input desired user name and then do it’s staff – extract file to the same directory and send it by email.
    So far I find and adopt for above needs following powershell scripts.

    Unzip Password protected file:

    $7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $zipFilePassword = “123456”
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command[/CODE]

    This script doing it’s job, but I’m trying to avoid usage of password as plain text in the script. Since this script will be run under same administrative user account I’ve tried to use encrypted password file in the script.

    First, I’ve run following command to create encrypted password file:

    [CODE]”123456″ | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File “W:AdminZipPassword.txt”[/CODE]

    Afterwards I’ve adopt my script to use encrypted password file:

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $cred = Get-Content “W:AdminZipPassword.txt” | ConvertTo-SecureString
    $zipFilePassword = new-object -typename System.Management.Automation.PSCredential -argumentlist ($cred)
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command[/CODE]

    When running this script I’m getting following error:

    [QUOTE]7-Zip [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04

    Scanning the drive for archives:

    ERROR: The system cannot find the file specified.
    .Management.Automation.PSCredential

    System ERROR:
    The system cannot find the file specified.[/QUOTE]

    If it possible to make this script use encrypted password file it will me very beneficial…

    The second script – sending extracted file by email.

    First, I’ve created encrypted password file (in this script it ‘s working perfectly):

    [CODE]”myPassword” | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File “W:AdminEmailPassword.txt”[/CODE]

    And here is the script itself:

    [CODE]$User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATIONdana.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    This script working as expected… The only problem is that administrative user need edit it each time and put proper filename (dana.csv, david.csc… etc). Of cource I can use user input method in this script as well, but I want to combine both scripts into the single one… So far I tried this one:

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $zipFilePassword = “123456”
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command

    $User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATION$User.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    But it’s failed to attach file to email. I think I have problem here (wrong syntax):

    [CODE]$filenameAndPath = “W:ADMINISTRATION$User.csv”[/CODE]

    So, if someone can help me to fix following issues, it will be much appreciated:

    1. In the first portion of script, use encrypted password file instead of plain text
    2. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was “dana” , $User.csv will be equal to dana.csv)
    3. Remove *.csv file after mail was sent.

    Thank you in advance,

    Igor.

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ”


    ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ”


    ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $zipFilePassword = “123456”
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command[/CODE]

    This script doing it’s job, but I’m trying to avoid usage of password as plain text in the script. Since this script will be run under same administrative user account I’ve tried to use encrypted password file in the script.

    First, I’ve run following command to create encrypted password file:

    “123456” | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File “W:AdminZipPassword.txt”[/CODE]

    Afterwards I’ve adopt my script to use encrypted password file:

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $cred = Get-Content “W:AdminZipPassword.txt” | ConvertTo-SecureString
    $zipFilePassword = new-object -typename System.Management.Automation.PSCredential -argumentlist ($cred)
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command[/CODE]

    When running this script I’m getting following error:

    [QUOTE]7-Zip [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04

    Scanning the drive for archives:

    ERROR: The system cannot find the file specified.
    .Management.Automation.PSCredential

    System ERROR:
    The system cannot find the file specified.[/QUOTE]

    If it possible to make this script use encrypted password file it will me very beneficial…

    The second script – sending extracted file by email.

    First, I’ve created encrypted password file (in this script it ‘s working perfectly):

    [CODE]”myPassword” | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File “W:AdminEmailPassword.txt”[/CODE]

    And here is the script itself:

    [CODE]$User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATIONdana.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    This script working as expected… The only problem is that administrative user need edit it each time and put proper filename (dana.csv, david.csc… etc). Of cource I can use user input method in this script as well, but I want to combine both scripts into the single one… So far I tried this one:

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $zipFilePassword = “123456”
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command

    $User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATION$User.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    But it’s failed to attach file to email. I think I have problem here (wrong syntax):

    [CODE]$filenameAndPath = “W:ADMINISTRATION$User.csv”[/CODE]

    So, if someone can help me to fix following issues, it will be much appreciated:

    1. In the first portion of script, use encrypted password file instead of plain text
    2. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was “dana” , $User.csv will be equal to dana.csv)
    3. Remove *.csv file after mail was sent.

    Thank you in advance,

    Igor.

    [CODE]”123456″ | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File “W:AdminZipPassword.txt”[/CODE]

    Afterwards I’ve adopt my script to use encrypted password file:

    $7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $cred = Get-Content “W:AdminZipPassword.txt” | ConvertTo-SecureString
    $zipFilePassword = new-object -typename System.Management.Automation.PSCredential -argumentlist ($cred)
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command[/CODE]

    When running this script I’m getting following error:

    [QUOTE]7-Zip [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04

    Scanning the drive for archives:

    ERROR: The system cannot find the file specified.
    .Management.Automation.PSCredential

    System ERROR:
    The system cannot find the file specified.[/QUOTE]

    If it possible to make this script use encrypted password file it will me very beneficial…

    The second script – sending extracted file by email.

    First, I’ve created encrypted password file (in this script it ‘s working perfectly):

    [CODE]”myPassword” | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File “W:AdminEmailPassword.txt”[/CODE]

    And here is the script itself:

    [CODE]$User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATIONdana.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    This script working as expected… The only problem is that administrative user need edit it each time and put proper filename (dana.csv, david.csc… etc). Of cource I can use user input method in this script as well, but I want to combine both scripts into the single one… So far I tried this one:

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $zipFilePassword = “123456”
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command

    $User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATION$User.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    But it’s failed to attach file to email. I think I have problem here (wrong syntax):

    [CODE]$filenameAndPath = “W:ADMINISTRATION$User.csv”[/CODE]

    So, if someone can help me to fix following issues, it will be much appreciated:

    1. In the first portion of script, use encrypted password file instead of plain text
    2. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was “dana” , $User.csv will be equal to dana.csv)
    3. Remove *.csv file after mail was sent.

    Thank you in advance,

    Igor.

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ”


    ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ”


    ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $cred = Get-Content “W:AdminZipPassword.txt” | ConvertTo-SecureString
    $zipFilePassword = new-object -typename System.Management.Automation.PSCredential -argumentlist ($cred)
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command[/CODE]

    When running this script I’m getting following error:

    Quote:
    7-Zip [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04

    Scanning the drive for archives:

    ERROR: The system cannot find the file specified.
    .Management.Automation.PSCredential

    System ERROR:
    The system cannot find the file specified.

    If it possible to make this script use encrypted password file it will me very beneficial…

    The second script – sending extracted file by email.

    First, I’ve created encrypted password file (in this script it ‘s working perfectly):

    “myPassword” | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File “W:AdminEmailPassword.txt”[/CODE]

    And here is the script itself:

    [CODE]$User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATIONdana.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    This script working as expected… The only problem is that administrative user need edit it each time and put proper filename (dana.csv, david.csc… etc). Of cource I can use user input method in this script as well, but I want to combine both scripts into the single one… So far I tried this one:

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $zipFilePassword = “123456”
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command

    $User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATION$User.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    But it’s failed to attach file to email. I think I have problem here (wrong syntax):

    [CODE]$filenameAndPath = “W:ADMINISTRATION$User.csv”[/CODE]

    So, if someone can help me to fix following issues, it will be much appreciated:

    1. In the first portion of script, use encrypted password file instead of plain text
    2. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was “dana” , $User.csv will be equal to dana.csv)
    3. Remove *.csv file after mail was sent.

    Thank you in advance,

    Igor.

    [CODE]”myPassword” | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File “W:AdminEmailPassword.txt”[/CODE]

    And here is the script itself:

    $User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATIONdana.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    This script working as expected… The only problem is that administrative user need edit it each time and put proper filename (dana.csv, david.csc… etc). Of cource I can use user input method in this script as well, but I want to combine both scripts into the single one… So far I tried this one:

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $zipFilePassword = “123456”
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command

    $User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATION$User.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    But it’s failed to attach file to email. I think I have problem here (wrong syntax):

    [CODE]$filenameAndPath = “W:ADMINISTRATION$User.csv”[/CODE]

    So, if someone can help me to fix following issues, it will be much appreciated:

    1. In the first portion of script, use encrypted password file instead of plain text
    2. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was “dana” , $User.csv will be equal to dana.csv)
    3. Remove *.csv file after mail was sent.

    Thank you in advance,

    Igor.

    [CODE]$User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATIONdana.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    This script working as expected… The only problem is that administrative user need edit it each time and put proper filename (dana.csv, david.csc… etc). Of cource I can use user input method in this script as well, but I want to combine both scripts into the single one… So far I tried this one:

    $7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ” ——————————————————————————— ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $zipFilePassword = “123456”
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command

    $User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATION$User.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    But it’s failed to attach file to email. I think I have problem here (wrong syntax):

    [CODE]$filenameAndPath = “W:ADMINISTRATION$User.csv”[/CODE]

    So, if someone can help me to fix following issues, it will be much appreciated:

    1. In the first portion of script, use encrypted password file instead of plain text
    2. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was “dana” , $User.csv will be equal to dana.csv)
    3. Remove *.csv file after mail was sent.

    Thank you in advance,

    Igor.

    [CODE]$7ZipPath = ‘”C:Program Files7-Zip7z.exe”‘
    $User = Read-Host -Prompt ‘Please Input Desired User Name’
    write-host “”
    write-host ”


    ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” Desired file will be extracted to W:\ADMINISTRATION folder ” -foregroundcolor Cyan
    write-host “”
    write-host ”


    ” -foregroundcolor DarkCyan
    write-host “”
    $zipFile = ‘”W:ADMINISTRATION$User.zip”‘
    $zipFilePassword = “123456”
    $command = “& $7ZipPath e -oW:ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile”
    iex $command

    $User = “[email protected]
    $File = “W:AdminEmailPassword.txt”
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = “[email protected]
    $EmailFrom = “[email protected]
    $Subject = “Some text here”
    $Body = “Some text here”
    $SMTPServer = “smtp.gmail.com”
    $filenameAndPath = “W:ADMINISTRATION$User.csv”
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host “Mail Sent Successfully !!” -foregroundcolor Green [/CODE]

    But it’s failed to attach file to email. I think I have problem here (wrong syntax):

    $filenameAndPath = “W:ADMINISTRATION$User.csv”[/CODE]

    So, if someone can help me to fix following issues, it will be much appreciated:

    1. In the first portion of script, use encrypted password file instead of plain text
    2. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was “dana” , $User.csv will be equal to dana.csv)
    3. Remove *.csv file after mail was sent.

    Thank you in advance,

    Igor.

    [CODE]$filenameAndPath = “W:ADMINISTRATION$User.csv”[/CODE]

    So, if someone can help me to fix following issues, it will be much appreciated:

    1. In the first portion of script, use encrypted password file instead of plain text
    2. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was “dana” , $User.csv will be equal to dana.csv)
    3. Remove *.csv file after mail was sent.

    Thank you in advance,

    Igor.


    igor7
    Member
    #295084

    I’ve figured out what was wrong with issue #2, – I’ve using same variable $User in first portion of script and in second… so second $User variable override already existing, first one. That’s why file wasn’t attached to email. I’ve changed $User variable to $Username in firs portion of the script and now all good:

    [B]$Username = Read-Host -Prompt ‘Please Input Desired User Name’….
    $zipFile = “W:ADMINISTRATION$UserName.zip”…
    $filenameAndPath = “W:ADMINISTRATION$UserName.csv”[/B][/CODE]

    Issue #1: It looks like 7zip not supporting Windows-specific encrypted password, so I need convert it back to plain text before using it. I’ve found how to do it in those sites:[URL=”https://blogs.msdn.microsoft.com/timid/2009/09/10/powershell-one-liner-decrypt-securestring/#comment-6495″]https://blogs.msdn.microsoft.com/tim…/#comment-6495[/URL] and [URL=”http://www.travisgan.com/2015/06/powershell-password-encryption.html”]http://www.travisgan.com/2015/06/pow…ncryption.html[/URL]

    So, this peace of code helped me out fixing encrypted password issue:

    [CODE]$Password = Get-Content ‘W:AdministrationZipPassword.txt’| ConvertTo-SecureString
    $Marshal = [System.Runtime.InteropServices.Marshal]
    $Bstr = $Marshal::SecureStringToBSTR($Password)
    $ZipPassword = $Marshal::PtrToStringAuto($Bstr)
    $Marshal::ZeroFreeBSTR($Bstr)[/CODE]

    Issue #3: I’ve accomplished this goal using separate PowerShell script:

    [CODE]# Removing *.csv file after sending it by email

    Get-ChildItem W:Administration*.csv | Where { ! $_.PSIsContainer } | remove-item

    write-host ” ———————————————- ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” All Operations Completed Successfully!!” -foregroundcolor Green
    write-host “”
    write-host ” ———————————————- ” -foregroundcolor DarkCyan[/CODE]

    Hope, my experience will be useful for somebody else…[CODE]$Username = Read-Host -Prompt ‘Please Input Desired User Name’….
    $zipFile = “W:ADMINISTRATION$UserName.zip”…
    $filenameAndPath = “W:ADMINISTRATION$UserName.csv”
    [/CODE]

    Issue #1: It looks like 7zip not supporting Windows-specific encrypted password, so I need convert it back to plain text before using it. I’ve found how to do it in those sites:https://blogs.msdn.microsoft.com/tim…/#comment-6495 and http://www.travisgan.com/2015/06/pow…ncryption.html

    So, this peace of code helped me out fixing encrypted password issue:

    $Password = Get-Content ‘W:AdministrationZipPassword.txt’| ConvertTo-SecureString
    $Marshal = [System.Runtime.InteropServices.Marshal]
    $Bstr = $Marshal::SecureStringToBSTR($Password)
    $ZipPassword = $Marshal::PtrToStringAuto($Bstr)
    $Marshal::ZeroFreeBSTR($Bstr)[/CODE]

    Issue #3: I’ve accomplished this goal using separate PowerShell script:

    [CODE]# Removing *.csv file after sending it by email

    Get-ChildItem W:Administration*.csv | Where { ! $_.PSIsContainer } | remove-item

    write-host ” ———————————————- ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” All Operations Completed Successfully!!” -foregroundcolor Green
    write-host “”
    write-host ” ———————————————- ” -foregroundcolor DarkCyan[/CODE]

    Hope, my experience will be useful for somebody else…[CODE]$Password = Get-Content ‘W:AdministrationZipPassword.txt’| ConvertTo-SecureString
    $Marshal = [System.Runtime.InteropServices.Marshal]
    $Bstr = $Marshal::SecureStringToBSTR($Password)
    $ZipPassword = $Marshal::PtrToStringAuto($Bstr)
    $Marshal::ZeroFreeBSTR($Bstr)[/CODE]

    Issue #3: I’ve accomplished this goal using separate PowerShell script:

    # Removing *.csv file after sending it by email

    Get-ChildItem W:Administration*.csv | Where { ! $_.PSIsContainer } | remove-item

    write-host ” ———————————————- ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” All Operations Completed Successfully!!” -foregroundcolor Green
    write-host “”
    write-host ” ———————————————- ” -foregroundcolor DarkCyan[/CODE]

    Hope, my experience will be useful for somebody else…[CODE]# Removing *.csv file after sending it by email

    Get-ChildItem W:Administration*.csv | Where { ! $_.PSIsContainer } | remove-item

    write-host ”


    ” -foregroundcolor DarkCyan
    write-host “”
    write-host ” All Operations Completed Successfully!!” -foregroundcolor Green
    write-host “”
    write-host ”


    ” -foregroundcolor DarkCyan[/CODE]

    Hope, my experience will be useful for somebody else…


    wullieb1
    Moderator
    #245666

    Excellent job.

    Thanks for posting back with your outcome.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.