表达式只允许作为管道的第一个元素

发布于 2024-12-01 02:56:12 字数 1741 浏览 0 评论 0原文

我是用 powershell 写作的新手,但这就是我想要实现的目标。

  1. 我想比较两个 Excel 文件的日期以确定一个文件是否比另一个文件新。
  2. 我想在没有 Excel 的计算机上将文件从 csv 转换为 xls。仅当上述陈述成立时,初始 xls 文件已被复制。
  3. 我想将新转换的 xls 文件复制到另一个位置
  4. 如果文件已打开,它将无法复制,因此我想发送有关此操作成功或失败的电子邮件警报。

这是我遇到问题的脚本。错误是“表达式只允许作为管道的第一个元素。”我知道这与电子邮件操作有关,但我不知道如何手动写出包含所有这些变量的内容。可能还有更多错误,但我现在没有看到它们。感谢您的帮助,我很感激!

$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.

  1. I want to compare the dates of the two excel files to determine if one is newer than the other.
  2. 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.
  3. I want to copy the newly converted xls file to another location
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-12-08 02:56:12

当您尝试从管道链中执行独立的代码块时,您会收到此错误。

作为一个不同的示例,想象一下使用 jQuery 的代码:

$("div").not(".main").console.log(this)

每个点 (.) 都会将数组链接到下一个函数中。在上面的函数中,这与 console 中断,因为它并不意味着有任何值通过管道传入。如果我们想打破链接来执行一些代码(也许是在链中的对象上 - 我们可以这样做与 each 类似:

$("div").not(".main").each(function() {console.log(this)})

解决方案是 powershell 是相同的。如果您想单独针对链中的每个项目运行脚本,您可以使用 ForEach-Object 或其别名 (%)。

假设您在 Powershell 中有以下函数:

$settings | ?{$_.Key -eq 'Environment' } | $_.Value = "Prod" 

最后一行无法执行,因为它是一个脚本,但是 我们可以使用 ForEach 解决这个问题,如下所示:

$settings | ?{$_.Key -eq 'Environment' } | %{ $_.Value = "Prod" }

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:

$("div").not(".main").console.log(this)

Each dot (.) will chain the array into the next function. In the above function this breaks with console 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 with each like this:

$("div").not(".main").each(function() {console.log(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:

$settings | ?{$_.Key -eq 'Environment' } | $_.Value = "Prod" 

The last line cannot be executed because it is a script, but we can fix that with ForEach like this:

$settings | ?{$_.Key -eq 'Environment' } | %{ $_.Value = "Prod" }
无力看清 2024-12-08 02:56:12

当您在管道的接收端使用表达式而无法从管道接收对象时,基本上会发生此错误。

如果您执行以下操作,您会收到错误:

$a="test" | $a

甚至这样:

"test" | $a

我不知道为什么要尝试在任何地方进行管道传输。我建议您学习有关 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:

$a="test" | $a

or even this:

"test" | $a

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文