表达式只能作为管道的第一个元素 [英] Expressions are only allowed as the first element of a pipeline

查看:44
本文介绍了表达式只能作为管道的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用 powershell 编写的新手,但这正是我想要完成的.

I'm new at writing in powershell but this is what I'm trying to accomplish.

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

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

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@address.com"
$ToAddress = "email@address.com"
$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)}
}   

推荐答案

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

You get this error when you are trying to execute an independent block of code from within a pipeline chain.

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

Just as a different example, imagine this code using jQuery:

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

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

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)})

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

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 (%).

假设您在 Powershell 中有以下功能:

Imagine you have the following function in Powershell:

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

最后一行无法执行,因为它是一个脚本,但是我们可以使用 ForEach 来修复它,如下所示:

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" }

这篇关于表达式只能作为管道的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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