by scottbass at 2012-10-03 21:23:20
Hi,by scottbass at 2012-10-03 21:55:22
I’m writing a script that parses a log file on a local machine, where that log contains a path to a file on a remote machine. I then want to send an email attaching both files. The process under which this script will run is a service account on the local machine, and I can’t guarantee the network drive mappings.
One approach would be to temporarily map a network drive via the script: net use <drive letter>: \machine name<drive letter>$, send the email, net use <drive letter>: /delete. Is there a better approach?
Here is the script so far:
[code2=powershell]param($JAMS_NOTIFY_JAMS_ENTRY, $JAMS_NOTIFY_REASON, $JAMS_NOTIFY_JOB_NAME, $JAMS_NOTIFY_LOG_FILENAME)
# Make sure that the JAMS Snapin is snapped in
Add-PSSnapin MVPSI.JAMS -ErrorAction SilentlyContinue
# Get the JAMS entry object
$entry = Get-JAMSEntry $JAMS_NOTIFY_JAMS_ENTRY
# Get the JAMS entry Job Name
$jobname = $entry.JobName
# Get the JAMS Job Log File Name
$jobLog = $entry.LogFilename
$attachments=@($joblog)
# Get the SAS Log File Name from the JAMS Job Log
# For better performance, specify a total line count guaranteed to find the desired string
$saslog = Get-Content $jobLog -TotalCount 40 | Where {$_ -match "^S*saslogfull
S*(.)$"} # finds the line ^saslogfull R:\JAMS\Logs\MySASLogFilenameOnTheRemoteMachine.log$, where R: is a drive on the remote machine
if ($matches.Count) {$saslog = $matches[1].Trim(); $attachments+=$saslog}
# Send email, adding attachments via pipeline
$attachments |<br>Send-MailMessage
-To "johndoe@acme.com"<br>-Subject "Job Log for $jobname"
-From JAMS@localhost<br>-body "Generated by JAMS job Send-NotificationEmail"
-SMTPServer my.smtp.server[/code2]
Hi,by DanielS at 2012-10-04 12:53:57
Further progress. This is working, let me know if there’s a better way…
Thanks!
[code2=powershell]param(
$JAMS_NOTIFY_JAMS_ENTRY,
$JAMS_NOTIFY_REASON,
$JAMS_NOTIFY_JOB_NAME,
$JAMS_NOTIFY_LOG_FILENAME
)
# Make sure that the JAMS Snapin is snapped in
Add-PSSnapin MVPSI.JAMS -ErrorAction SilentlyContinue
# Get the JAMS entry object
$entry = Get-JAMSEntry $JAMS_NOTIFY_JAMS_ENTRY
# Get the JAMS entry Job Name
$jobname = $entry.JobName
# Get the JAMS Job Log File Name
$jobLog = $entry.LogFilename
$attachments = @($jobLog)
# Get the SAS Log File Name from the JAMS Job Log
# For better performance, specify a total line count large enough to find the desired string but no larger
$sasLog = Get-Content $jobLog -TotalCount 40 | Where {$_ -match "^S*saslogfull
S(.*)$"}
if ($matches.Count) {$sasLog = $matches[1].Trim(); $attachments+=$sasLog}
# Get the drive letter
$drive=($saslog -split "]
# Create a temporary drive mapping
New-PSDrive -name $drive -psprovider Filesystem -Root "\remote_machine_name$drive$" -ErrorAction SilentlyContinue | Out-Null
# Send email, adding attachments via pipeline
$attachments | Send-MailMessage -To "johndoe@acme.com" -Subject "Job Log for $jobname" -From JAMS@localhost -body "Generated by JAMS job Send-NotificationEmail" -SMTPServer my.smtp.server
# Delete temporary drive mapping
Remove-PSdrive -name $drive[/code2]
You could consider using Push-Location (pushd), but it looks fine.