Reputation: 49
I need help guys.
This command doesn't work for me because
The system cannot find the file specified.
powershell -command "& { Send-MailMessage -From "janohanes155 <[email protected]>" -To "janohanes155 <[email protected]>" -Subject "Sending the Attachment" -Body "Got it" -Attachments "c:\shade\info.txt" -Priority High -dno onSuccess, onFailure -SmtpServer "smtp.gmail.com" -Credential "********/*********"}"
What should I do?
Thank you for help.
Upvotes: 1
Views: 49
Reputation: 440122
Your problem is that your double-quoted string contains embedded "
chars. that you forgot to escape.
Additionally, since there are potentially 2 layers of escaping - one for the current shell and one for the target shell, additional escaping may be needed.
The specific method of escaping depends on where you're invoking the command from.
The following examples use a simplified command that more succinctly demonstrates the problem; in pure PowerShell form, the command is:
Write-Output "jdoe <[email protected]>"
The curious thing is that when PowerShell is passed a -Command
value from the outside, it requires embedded "
chars. to be \
-escaped, while PowerShell-internally `
-escaping must be used.
From the Windows Run dialog (Win+R):
All that is needed is to \
-escape the embedded "
chars., for the benefit of the target PowerShell instance (no current shell is involved):
powershell -command "& { Write-Output \"jdoe <[email protected]>\" }"
From cmd.exe
(Command Prompt):
powershell -command "& { Write-Output \"jdoe ^<[email protected]^>\" }"
In addition to \
-escaping "
, you must also ^
-escape <
and >
characters that cmd.exe
thinks are outside of a double-quoted string, due to not recognizing \
-escaped "
chars. as escaped (cmd.exe
only recognizes ""
).
It is the unintended interpretation of <
and >
as shell redirection operators by cmd.exe
that caused the error message you've encountered.
From PowerShell itself:
powershell -command "& { Write-Output \`"jdoe <[email protected]>\`" }"
In addition to \
-escaping "
for the target PowerShell instance, you must also `
-escape "
chars. for the current PowerShell instance (sic).
Upvotes: 1