Author Posts

November 3, 2014 at 6:43 am

Hello,

I'm trying to write a script to check for a file in a path and send an email. please see the script and let me know where i'm going wrong.

$file1 = C:\test.txt

#Email Settings
$MailFrom = “xxxxx@xxxx.com”
$Mailto = “xxxxxxx@xxx.com”
$MailSubject = “Status of backup file”
#$body = “The following files are missing:`r`n”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

#Check for Existence of Files
if (Test-Path $file1)

{
echo "File $file already exists" ;
Send-MailMessage -to $Mailto -from $MailFrom -subject $MailSubject -body "$file1 exist on the path" -smtpServer xxxx.xxx.com
}
Else
{

#Send Email
Send-MailMessage -to $Mailto -from $MailFrom -subject $MailSubject -body "$file1 does not exist on the path" -smtpServer xxxx.xxxxx.com

}

i'm getting the following error

est-Path : Cannot bind argument to parameter 'Path' because it is null.
At line:12 char:14
+ if (Test-Path <<<< $file1) + CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathComman Also, my file name changes with date like test1103 ( today ) test1104 ( tomorrow ). can anyone please help me on this ? Thanks in advance

November 3, 2014 at 7:17 am

Hello,

Try this to fix the Test-Path issue:

$file1 = "C:\test.txt"

/Alexander

November 3, 2014 at 7:18 am

You need to place quotation marks (single or double, either will work) when assigning a string value to the $file1 variable:

$file1 = C:\test.txt

# Should be:

$file1 = 'C:\test.txt'

November 3, 2014 at 7:19 am

Your issue is the $file1 variable is not recognized as a string. The path should be enclosed in qoutes:

PS C:\Users\rsimmers> $test = C:\Test\123.txt
C:\Test\123.txt : The term 'C:\Test\123.txt' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that 
the path is correct and try again.
At line:1 char:9
+ $test = C:\Test\123.txt
+         ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Test\123.txt:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 

PS C:\Users\rsimmers> $test = "C:\Test\123.txt"

You can shorten this to single line, but I made it two so it's somewhat easier to read. This would produce an email body like "File Exists: True":

$file = "whatever{0}.zip" -f (Get-Date).ToString("MMdd") #Custom Date time format:  http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
Send-MailMessage -to me@test.com -from donotreply@test.com -subject "File Checker" -body ("File Exists: {0}" -f (Test-Path -Path $file)) -smtpServer mailhost.susq.com

November 3, 2014 at 7:35 am

Thanks a lot everyone...

it worked. but my filename changes with with date or time at the end of it... is there any way to find it ?

file name will be same but numbers ( time or date ) changes
e.g., backupfile1103.zip , backupfile1104.zip

Rob, thanks for the code. Is there any way to find the file using strings or something like this ?

like $filename = "backupfile%.zip"

Thanks

November 3, 2014 at 7:41 am

Yes, that is what I posted.

$file = "whatever{0}.zip" -f (Get-Date).ToString("MMdd")

results in:

PS C:\Users\rsimmers> "whatever{0}.zip" -f (Get-Date).ToString("MMdd")
whatever1103.zip

November 3, 2014 at 7:51 am

Thanks again Rob..

we don't know which date it will be so it's not safe to use mmdd.....i'm thinking to find the filename using extension ( because we will be having only one file with .zip extension )

also, in the above reply you mentioned "$file = "whatever{0}.zip" -f (Get-Date).ToString("MMdd")" but $file already contains path right ? how to proceed with this ?

November 3, 2014 at 8:17 am

You would use Get-ChildItem with a wildcard, something like this:

$file = @(Get-ChildItem "C:\users\rsimmers\desktop\help*.zip" | Select FullName, LastWriteTime)

if ($file.Count -gt 0) {
    $body = "Found {0} zip files: `r`n`r`n{1}" -f $file.Count, ("{0} - {1}" -f $file.FullName, $file.LastWriteTime)
}
else {
    $body = "No zip files found, rut roh raggy"
}

$body

Depending on the version of Powershell, you would have to test formatting. If there is a possibility of multiple files, I would use HTML email and use ConvertTo-HTML to generate a table from the $file object.

November 3, 2014 at 8:53 am

Thanks a lot Rob for your help...

i'm not getting the full output but it's finding the .zip file.

btw can you tell me what is – ?

code is some what stuck here

November 3, 2014 at 9:11 am

i'm seeing this output :

Found 1 zip files:

November 3, 2014 at 9:12 am

Our forums software is still HTML escaping some things in code blocks that are occasionally annoying. I think that particular sequence should just be a hyphen character.