Author Posts

January 7, 2018 at 10:54 pm

I would like to unzip a folder based on date and time and copy it to the target destination.

After the files are transferred by file type. Send an email notification letting the recipient know that the files is available in the path.

I need help fixing the script below.

#powershell Set-ExecutionPolicy RemoteSigned

$Username = "Outbound@happyland.com";
$Password= "12wse43s2017";

function Send-ToEmail([string]$email, $foldername){

$message = new-object Net.Mail.MailMessage;
$message.From = "Outbound@happyland.com";
$message.To.Add($email);

$FileNames = Get-ChildItem -Path $foldername
if(Compare-Object $srcOrTarget "SRC"){
$message.Subject = "$foldername folder received ";
$message.Body = "$FileNames files are received in the source folder";
}else {
$message.Subject = "$foldername folder copied";
$message.Body = "$FileNames files are copied in the target folder";

}
$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587");
$smtp.EnableSSL = $true;
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message);

}

$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("C:\AllScripts\")
foreach ($item in $zip.items()) {
$shell.Namespace("C:\Target\20171126").CopyHere($item)
}

$Files = Get-ChildItem $sourceFolder\*.* -include *.zip,*.out,*.err
$srcZipFile = $Files | where {($_.extension -eq ".zip") }#Identifying zip file in the source path#

if ((Test-Path $sourceFolder\*.out) -or (Test-Path $sourceFolder\*.err)){
Send-ToEmail -email "cohen.carryl@gmail.com" $sourceFolder $srcOrTarget;
if (Test-Path $srcZipFile){
$zipFolderName = $([io.fileinfo]"$srcZipFile").basename
Unzip "$srcZipFile" "$sourceFolder\$zipFolderName"
$tempList = get-childitem "$sourceFolder\$zipFolderName" -recurse
$zipFileList = $tempList | where {($_.extension -eq ".err") -or ($_.extension -eq ".out") }

if ($zipFileList.length -gt 0 ){

Remove-Item $sourceFolder\$zipFolderName -Force -Recurse

}else{
Remove-Item $sourceFolder\$zipFolderName -Force -Recurse

}
}

Copy-Item $sourceFolder $targetPath -Force -recurse
if (Test-Path $targetPath){
Send-ToEmail -email "cohen.carryl@gmail.com" $targetFolder;
}
}

January 7, 2018 at 11:03 pm

You don't post here for the first time. Is it really asked to much to format your code as code? And this is not a free debugging service here. You didn't even told us what's the problem you have.

January 7, 2018 at 11:26 pm

This almost a exact duplication of a post Olaf, others and I responded to already.
See the below: https://powershell.org/forums/topic/zip-folder-dated-yesterday-and-copy-to-network-location
The above talks to zipping, but you should just have to switch the .Net call to ExtractToDirectory instead of CreateFromDirectory.

Well, other than the addition of the whole email thing. But that is what Send-MailMessage is for.

    Get parameter, example, full and Online help for a cmdlet or function

    (Get-Command -Name Send-MailMessage).Parameters
    Get-help -Name Send-MailMessage -Examples
    Get-help -Name Send-MailMessage -Full
    Get-help -Name Send-MailMessage -Online

January 8, 2018 at 12:24 am

Hi Olaf,

This is the error message, that I'm getting

Get-ChildItem : Cannot find path 'C:\AllScripts\20180107\20180107' because it does not exist.
At C:\Users\greatness\Downloads\scriptToMail (1).ps1:36 char:11
+ ... $Files = Get-ChildItem $sourceFolder\*.* -include *.zip,*.out,*.er ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\AllScripts\20180107\20180107:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

January 8, 2018 at 1:06 am

At no point in you script are you showing the ...

    .Fullname property 

... in source or destinations.

IF all things are happening in the same folder, this is not a big deal, but is they are separate locations, you have to fully qualify them using the above property.

You really don't need the double quotes here:

   $([io.fileinfo]"$srcZipFile").basename.

I'm old, and sometimes visual acuity (after 40+ years in the industry) is not where I'd like it to be these days, depending on how long I've been starting at the blinking blue screen, but...

Where are these ...

    $sourceFolder
    $srcOrTarget
    $targetPath

... populated from?

You don't show this in your post, which means unless you are doing this in code elsewhere, which you are not showing, these are just random variables with a UNC to nowhere.

There are many other aspects of this function that seem very out of place. Personally, I'd rewrite this whole thing...

    $shell = New-Object -ComObject shell.application

    $zip = $shell.NameSpace("C:\AllScripts\")

    foreach ($item in $zip.items())
    {
        $shell.Namespace("C:\Target\20171126").CopyHere($item)
    } 

    $Files = Get-ChildItem $sourceFolder\*.* -include *.zip, *.out, *.err
    $srcZipFile = $Files | where {($_.extension -eq ".zip") }#Identifying zip file in the source path#



    if ((Test-Path $sourceFolder\*.out) -or (Test-Path $sourceFolder\*.err))
    {
        if (Test-Path $srcZipFile)
        {
            $zipFolderName = $([io.fileinfo]$srcZipFile).basename

            Unzip "$srcZipFile" "$sourceFolder\$zipFolderName"

            $tempList = get-childitem "$sourceFolder\$zipFolderName" -recurse

            $zipFileList = $tempList | where {($_.extension -eq ".err") -or ($_.extension -eq ".out") }

            if ($zipFileList.length -gt 0 )
            {

                Remove-Item $sourceFolder\$zipFolderName -Force -Recurse

            }
            else
            {
                Remove-Item $sourceFolder\$zipFolderName -Force -Recurse

            }
        }
    }

... One step and a time. Stepping through each piece before adding another to make sure the results are what they should be. Save the whole mail thing for last.

January 8, 2018 at 1:26 am

My apologies postanote

#powershell Set-ExecutionPolicy RemoteSigned

$Username = "cohen.carryl@gmail.com";
$Password= "N4ture2017";

function Send-ToEmail([string]$email, $foldername, $srcOrTarget){

$message = new-object Net.Mail.MailMessage;
$message.From = "ccarrylab@gmail.com";
$message.To.Add($email);

$FileNames = Get-ChildItem -Path $foldername
if(Compare-Object $srcOrTarget "SRC"){
$message.Subject = "$foldername folder recived ";
$message.Body = "$FileNames files are received in the source folder";
}else {
$message.Subject = "$foldername folder copied";
$message.Body = "$FileNames files are copied in the target folder";

}
$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587");
$smtp.EnableSSL = $true;
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message);

}

$srcPath = "C:\Source"
$targetPath = "C:\Users\greatness\Target\"
$fname = (get-date).ToString("yyyMMdd")
$sourceFolder = "$srcPath\$fname"
$targetFolder = "$targetPath\$fname"

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}

if (Test-Path $sourceFolder ){
$Files = Get-ChildItem $sourceFolder\*.* -include *.zip,*.out,*.err
$srcZipFile = $Files | where {($_.extension -eq ".zip") }#Identifying zip file in the source path#

if ((Test-Path $sourceFolder\*.out) -or (Test-Path $sourceFolder\*.err)){
$srcOrTarget="SRC"
Send-ToEmail -email "ccarrylab@gmail.com" $sourceFolder $srcOrTarget;
if (Test-Path $srcZipFile){
$zipFolderName = $([io.fileinfo]"$srcZipFile").basename
Unzip "$srcZipFile" "$sourceFolder\$zipFolderName"
$tempList = get-childitem "$sourceFolder\$zipFolderName" -recurse
$zipFileList = $tempList | where {($_.extension -eq ".err") -or ($_.extension -eq ".out") }

if ($zipFileList.length -gt 0 ){

Remove-Item $sourceFolder\$zipFolderName -Force -Recurse

}else{
Remove-Item $sourceFolder\$zipFolderName -Force -Recurse

}
}

Copy-Item $sourceFolder $targetPath -Force -recurse
if (Test-Path $targetPath){
$srcOrTarget = 'TGT'
Send-ToEmail -email "ccarrylab@gmail.com" $targetFolder $srcOrTarget;
}
}
}else{

Write-Host "There is no files received today yet"
}

January 8, 2018 at 1:56 am

No worries, but this still means you should step through all this one at a time to make sure you have what you'd expect at each point. I did this using just your code, as is, and you are not getting stuff they way your are trying to use it.

This also appears to be an old VBScript file which you are trying to convert to PoSH. Which is fine, but no need to stick with COM, it's just old and a bear to work with, when you don't have to. You also have no error checking in here at all, especially since you should validate results before a email send.

I'd suggest a rewrite only using PoSH cmdlets and .Net as needed. If you are on v5, then there are cmdlets for zip/unzip, if not then use the .Net option in the post I show above.

Again, the whole step through thing is really important. For example, just taking the variables you just added. Take a look, and see what's wrong with this picture.

    ($srcPath = "C:\Source")
    C:\Source

    ($targetPath = "C:\Users\greatness\Target\")
    C:\Users\greatness\Target\

    ($fname = (get-date).ToString("yyyMMdd"))
    20180107

    ($sourceFolder = "$srcPath\$fname")
    C:\Source\20180107

    ($targetFolder = "$targetPath\$fname")
    C:\Users\greatness\Target\\20180107

Or do this and see why this would not work the way you are trying to use it.

    $zip.Items() | Format-Table -AutoSize