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

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    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.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.