Get UNC path for remote file

by scottbass at 2012-10-03 21:23:20

Hi,

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&lt;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*saslogfullS*(.)$"} # 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 &quot;Job Log for $jobname&quot;
-From JAMS@localhost <br>-body &quot;Generated by JAMS job Send-NotificationEmail&quot;
-SMTPServer my.smtp.server[/code2]
by scottbass at 2012-10-03 21:55:22
Hi,

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*saslogfullS
(.*)$"}
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]
by DanielS at 2012-10-04 12:53:57
You could consider using Push-Location (pushd), but it looks fine.