Powershell 查询 - 找不到路径的一部分
Powershell query- Could not find a part of the path
我正在尝试使用强大的 shell 脚本从 csv 列表查询电子邮件 ID 并发送批量个性化 emails.Both csv 文件和脚本位于桌面上同一文件夹(\DECSRV02\Folder Redirection\paranjyoti\Desktop\Powershell_massemail)。
该脚本包含 csv 导入为:
$csv = Import-Csv 'C:\Users\paranjyoti\Desktop\Powershell_massemail\UserList.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
}
虽然 运行 脚本,错误出现为 "Could not find a part of the path"
The attached error message image is shown
请你帮忙找出错误。
看来您需要检查实际路径是否存在?
我得到同样的错误,因为我没有文件夹结构:
PS C:\temp\csvimport> .\csvimport.ps1
Import-Csv : Could not find a part of the path 'C:\Users\paranjyoti\Desktop\Powershell_massemail\UserList.csv'.
At C:\temp\csvimport\csvimport.ps1:1 char:8
+ $csv = Import-Csv 'C:\Users\paranjyoti\Desktop\Powershell_massemail\U ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Import-Csv], DirectoryNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
但如果只是找不到文件,则另当别论:
PS C:\temp\csvimport> .\csvimport.ps1
Import-Csv : Could not find file 'C:\userlist.csv'.
At C:\temp\csvimport\csvimport.ps1:1 char:8
+ $csv = Import-Csv 'C:\userlist.csv'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Import-Csv], FileNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
不确定路径的哪一部分不正确或丢失。该练习留给 reader。 ;)
错误显示您的桌面路径已被重定向。
为了获取路径,可以使用注册表:
$desktop = (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders').Desktop
$csvPath = Join-Path -Path $desktop -ChildPath 'Powershell_massemail\UserList.csv'
$csv = Import-Csv -Path $csvPath
我还可以建议您对 Send-MailMessage
cmdlet 使用 Splatting 来摆脱那些讨厌的反引号并使代码更具可读性:
$mailParams = @{
Body = $mailBody
BodyAsHtml = $true
Subject = $subject
Encoding = [System.Text.Encoding]::UTF8
Credential = $Credential
SmtpServer = 'smtp.office365.com'
UseSsl = $true
}
Send-MailMessage @mailParams
希望对您有所帮助
我收到如下错误,尽管它不是上述问题的确切答案,但我会在这里分享给遇到类似问题的任何人。
Copy-Item : Could not find a part of the path
"D:\projects\changeDetection\cubeRepos\cm-2.0\ConceptMiner2\CM.Spidering.CsSched
uleLightweight\csx\Debug\roles\CM.Spidering.CsScheduleLightweight.Worker\approot\Templates\TEST\TEST\REG\TEST--TEST--REG--CACH
E-REFRESH\TEST--TEST--REG--CACHE-REFRESH.structure.json".
但我验证了上述文件夹路径存在,并且可以在所需目录中创建文件。一些谷歌搜索得到了问题的真正原因。这是因为 windows 对于 文件路径 有 260 个字符 的限制。
您会注意到我的路径恰好有 260 个字符,但路径长度验证中有一个小问题。 Path 在路径末尾附加了一个空字符。所以总 路径长度变为 261 导致它失败。
最快的解决方法是重命名文件,使新文件名少于 260 个字符。有关此错误的更多信息以及扩展路径限制的方法,请参阅 Microsoft Documentation
我正在尝试使用强大的 shell 脚本从 csv 列表查询电子邮件 ID 并发送批量个性化 emails.Both csv 文件和脚本位于桌面上同一文件夹(\DECSRV02\Folder Redirection\paranjyoti\Desktop\Powershell_massemail)。 该脚本包含 csv 导入为:
$csv = Import-Csv 'C:\Users\paranjyoti\Desktop\Powershell_massemail\UserList.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
}
虽然 运行 脚本,错误出现为 "Could not find a part of the path" The attached error message image is shown 请你帮忙找出错误。
看来您需要检查实际路径是否存在?
我得到同样的错误,因为我没有文件夹结构:
PS C:\temp\csvimport> .\csvimport.ps1
Import-Csv : Could not find a part of the path 'C:\Users\paranjyoti\Desktop\Powershell_massemail\UserList.csv'.
At C:\temp\csvimport\csvimport.ps1:1 char:8
+ $csv = Import-Csv 'C:\Users\paranjyoti\Desktop\Powershell_massemail\U ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Import-Csv], DirectoryNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
但如果只是找不到文件,则另当别论:
PS C:\temp\csvimport> .\csvimport.ps1
Import-Csv : Could not find file 'C:\userlist.csv'.
At C:\temp\csvimport\csvimport.ps1:1 char:8
+ $csv = Import-Csv 'C:\userlist.csv'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Import-Csv], FileNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
不确定路径的哪一部分不正确或丢失。该练习留给 reader。 ;)
错误显示您的桌面路径已被重定向。 为了获取路径,可以使用注册表:
$desktop = (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders').Desktop
$csvPath = Join-Path -Path $desktop -ChildPath 'Powershell_massemail\UserList.csv'
$csv = Import-Csv -Path $csvPath
我还可以建议您对 Send-MailMessage
cmdlet 使用 Splatting 来摆脱那些讨厌的反引号并使代码更具可读性:
$mailParams = @{
Body = $mailBody
BodyAsHtml = $true
Subject = $subject
Encoding = [System.Text.Encoding]::UTF8
Credential = $Credential
SmtpServer = 'smtp.office365.com'
UseSsl = $true
}
Send-MailMessage @mailParams
希望对您有所帮助
我收到如下错误,尽管它不是上述问题的确切答案,但我会在这里分享给遇到类似问题的任何人。
Copy-Item : Could not find a part of the path "D:\projects\changeDetection\cubeRepos\cm-2.0\ConceptMiner2\CM.Spidering.CsSched uleLightweight\csx\Debug\roles\CM.Spidering.CsScheduleLightweight.Worker\approot\Templates\TEST\TEST\REG\TEST--TEST--REG--CACH E-REFRESH\TEST--TEST--REG--CACHE-REFRESH.structure.json".
但我验证了上述文件夹路径存在,并且可以在所需目录中创建文件。一些谷歌搜索得到了问题的真正原因。这是因为 windows 对于 文件路径 有 260 个字符 的限制。 您会注意到我的路径恰好有 260 个字符,但路径长度验证中有一个小问题。 Path 在路径末尾附加了一个空字符。所以总 路径长度变为 261 导致它失败。
最快的解决方法是重命名文件,使新文件名少于 260 个字符。有关此错误的更多信息以及扩展路径限制的方法,请参阅 Microsoft Documentation