PowerShell查询-无法找到路径的一部分 [英] Powershell query- Could not find a part of the path

查看:43
本文介绍了PowerShell查询-无法找到路径的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用POWER外壳脚本从csv列表中查询电子邮件ID并发送大量个性化电子邮件。csv文件和脚本位于桌面上的同一文件夹中(DECSRV02文件夹RedirectionparanjyotiDesktopPowershell_massemail). 该脚本将CSV导入包含为:

$csv = Import-Csv 'C:UsersparanjyotiDesktopPowershell_massemailUserList.csv'
$Credential = Get-Credential
Foreach($Message in $csv){
$Recipient = $Message.EmailAddress
$FirstName = $Message.FirstName
$LastName = $Message.Surname
$Course = $Message.Course
$Grade = $Message.Grade
$Subject = "$FirstName $LastName - $Course Exam Result"
Write-Host "Sending email to $FirstName $LastName"
Write-Host "Email Address: $Recipient"
$mailBody = 
@"
Hello $FirstName,</br>
We have marked your recent exam and the results are as follows:</br>
</br>
Student Name: $LastName, $FirstName</br>
Course: $Course</br>
Result: $Grade</br>
</br>
Thank you for taking a course at our school,</br>
The Faculty
"@

Send-MailMessage -Body $mailBody -BodyAsHtml `
-From "faculty@greatschool.com" -To $Recipient `
-Subject $subject -Encoding $([System.Text.Encoding]::UTF8) `
-Credential $Credential -SmtpServer "smtp.office365.com" -UseSSLcopy
}
在运行脚本时,错误出现为"找不到路径的一部分" The attached error message image is shown 请您帮忙找出错误。

推荐答案

该错误显示您的桌面路径已重定向。 为了获取路径,您可以使用注册表:

$desktop = (Get-ItemProperty -Path 'HKCU:SoftwareMicrosoftWindowsCurrentVersionExplorerUser Shell Folders').Desktop
$csvPath = Join-Path -Path $desktop -ChildPath 'Powershell_massemailUserList.csv'
$csv = Import-Csv -Path $csvPath

我还建议您对Send-MailMessagecmdlet使用Splatting,以消除那些讨厌的反号,并使代码更具可读性:

$mailParams = @{
    Body       = $mailBody
    BodyAsHtml = $true
    Subject    = $subject
    Encoding   = [System.Text.Encoding]::UTF8
    Credential = $Credential
    SmtpServer = 'smtp.office365.com'
    UseSsl     = $true
}

Send-MailMessage @mailParams

希望能有所帮助

这篇关于PowerShell查询-无法找到路径的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆