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.
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
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
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…
… 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.
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#
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.