Expressions are allowed only as the first element of the pipeline

I'm new to writing in PowerShell, but this is what I'm trying to accomplish.

  • I want to compare the dates of two excel files to determine if it is newer than the other.
  • I want to convert a file from csv to xls to a computer that does not have excel. Only if the statement above is true, the initial xls file has already been copied.
  • I want to copy a newly converted xls file to another location
  • If the file is already open, it will not be able to copy, so I want to send an email notification of the successful completion or failure of this operation.

Here is the script I'm having problems with. Error: "Expressions are allowed only as the first element of the pipeline." I know this is due to the email operation, but I donโ€™t understand how to write it manually with all the variables included. There are probably more errors, but I do not see 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)} } 
+8
source share
2 answers

You get this error when you try to execute an independent block of code from the pipeline chain.

As another example, imagine this code using jQuery:

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

Each dot ( . ) Will bind an array to the next function. In the above function, this is aborted using console because it is not intended to enter any values. If we want to get out of our chain to execute some code (possibly on objects in the chain), we can do this with each like this:

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

Powershell's solution is identical. If you want to run a script for each element in your chain separately, you can use ForEach-Object or its alias ( % ).

Imagine that Powershell has the following function:

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

The last line cannot be executed because it is a script, but we can fix it with ForEach as follows:

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

This error mainly occurs when you use an expression on the receiving side of the pipeline, when it cannot receive objects from the pipeline.

You will get an error if you do something like this:

 $a="test" | $a 

or even this:

 "test" | $a 

I donโ€™t know why to try to blow everywhere. I would advise you to learn the basics of Powershell pipelining. You are approaching this wrong. In addition, I think you can refer to the link below to see how to send mail, should be straightforward without the complications that you added with the pipes: http://www.searchmarked.com/windows/how-to-send -an-email-using-a-windows-powershell-script.php

+4
source

All Articles