表达式只允许作为管道的第一个元素
我是用 powershell 写作的新手,但这就是我想要实现的目标。
- 我想比较两个 Excel 文件的日期以确定一个文件是否比另一个文件新。
- 我想在没有 Excel 的计算机上将文件从 csv 转换为 xls。仅当上述陈述成立时,初始 xls 文件已被复制。
- 我想将新转换的 xls 文件复制到另一个位置
- 如果文件已打开,它将无法复制,因此我想发送有关此操作成功或失败的电子邮件警报。
这是我遇到问题的脚本。错误是“表达式只允许作为管道的第一个元素。”我知道这与电子邮件操作有关,但我不知道如何手动写出包含所有这些变量的内容。可能还有更多错误,但我现在没有看到它们。感谢您的帮助,我很感激!
$CSV = "C:filename.csv"
$LocalXLS = "C:\filename.xls"
$RemoteXLS = "D:\filename.xls"
$LocalDate = (Get-Item $LocalXLS).LASTWRITETIME
$RemoteDate = (Get-Item $RemoteXLS).LASTWRITETIME
$convert = "D:\CSV Converter\csvcnv.exe"
if ($LocalDate -eq $RemoteDate) {break}
else {
& $convert $CSV $LocalXLS
$FromAddress = "[email protected]"
$ToAddress = "[email protected]"
$MessageSubject = "vague subject"
$SendingServer = "mail.mail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SendEmailSuccess = $MessageBody = "The copy completed successfully!" | New-Object System.Net.Mail.SMTPClient mail.mail.com $SMTPMessage
$RenamedXLS = {$_.BaseName+(Get-Date -f yyyy-MM-dd)+$_.Extension}
Rename-Item -path $RemoteXLS -newname $RenamedXLS -force -erroraction silentlycontinue
If (!$error)
{ $SendEmailSuccess | copy-item $LocalXLS -destination $RemoteXLS -force }
Else
{$MessageBody = "The copy failed, please make sure the file is closed." | $SMTPClient.Send($SMTPMessage)}
}
I'm new at writing in powershell but this is what I'm trying to accomplish.
- I want to compare the dates of the two excel files to determine if one is newer than the other.
- I want to convert a file from csv to xls on a computer that doesn't have excel. Only if the statement above is true, the initial xls file was copied already.
- I want to copy the newly converted xls file to another location
- If the file is already open it will fail to copy so I want to send out an email alert on success or failure of this operation.
Here is the script that I'm having issues with. The error is "Expressions are only allowed as the first element of a pipeline." I know it's to do with the email operation but I'm at a loss as to how to write this out manually with all those variables included. There are probably more errors but I'm not seeing them now. Thanks for any help, I appreciate it!
$CSV = "C:filename.csv"
$LocalXLS = "C:\filename.xls"
$RemoteXLS = "D:\filename.xls"
$LocalDate = (Get-Item $LocalXLS).LASTWRITETIME
$RemoteDate = (Get-Item $RemoteXLS).LASTWRITETIME
$convert = "D:\CSV Converter\csvcnv.exe"
if ($LocalDate -eq $RemoteDate) {break}
else {
& $convert $CSV $LocalXLS
$FromAddress = "[email protected]"
$ToAddress = "[email protected]"
$MessageSubject = "vague subject"
$SendingServer = "mail.mail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SendEmailSuccess = $MessageBody = "The copy completed successfully!" | New-Object System.Net.Mail.SMTPClient mail.mail.com $SMTPMessage
$RenamedXLS = {$_.BaseName+(Get-Date -f yyyy-MM-dd)+$_.Extension}
Rename-Item -path $RemoteXLS -newname $RenamedXLS -force -erroraction silentlycontinue
If (!$error)
{ $SendEmailSuccess | copy-item $LocalXLS -destination $RemoteXLS -force }
Else
{$MessageBody = "The copy failed, please make sure the file is closed." | $SMTPClient.Send($SMTPMessage)}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您尝试从管道链中执行独立的代码块时,您会收到此错误。
作为一个不同的示例,想象一下使用 jQuery 的代码:
每个点 (
.
) 都会将数组链接到下一个函数中。在上面的函数中,这与console
中断,因为它并不意味着有任何值通过管道传入。如果我们想打破链接来执行一些代码(也许是在链中的对象上 - 我们可以这样做与each
类似:解决方案是 powershell 是相同的。如果您想单独针对链中的每个项目运行脚本,您可以使用
ForEach-Object
或其别名 (%
)。假设您在 Powershell 中有以下函数:
最后一行无法执行,因为它是一个脚本,但是 我们可以使用 ForEach 解决这个问题,如下所示:
You get this error when you are trying to execute an independent block of code from within a pipeline chain.
Just as a different example, imagine this code using jQuery:
Each dot (
.
) will chain the array into the next function. In the above function this breaks withconsole
because it's not meant to have any values piped in. If we want to break from our chaining to execute some code (perhaps on objects in the chain - we can do so witheach
like this:The solution is powershell is identical. If you want to run a script against each item in your chain individually, you can use
ForEach-Object
or it's alias (%
).Imagine you have the following function in Powershell:
The last line cannot be executed because it is a script, but we can fix that with ForEach like this:
当您在管道的接收端使用表达式而无法从管道接收对象时,基本上会发生此错误。
如果您执行以下操作,您会收到错误:
甚至这样:
我不知道为什么要尝试在任何地方进行管道传输。我建议您学习有关 Powershell 管道的基础知识。你的做法是错误的。另外,我认为您可以参考下面的链接来了解如何发送邮件,应该是直接的,没有使用管道添加的复杂性: http://www.searchmarked.com/windows/how-to-send-an-email-using-a-windows-powershell-script.php
This error basically happens when you use an expression on the receiving side of the pipeline when it cannot receive the objects from the pipeline.
You would get the error if you do something like this:
or even this:
I don't know why are trying to pipe everywhere. I would recommend you to learn basics about Powershell pipelining. You are approaching it wrong. Also, I think you can refer to the link below to see how to send mail, should be straight forward without the complications that you have added with the pipes : http://www.searchmarked.com/windows/how-to-send-an-email-using-a-windows-powershell-script.php