Support
Quality
Security
License
Reuse
kandi has reviewed logcapture and discovered the below as its top functions. This is intended to give you an instant insight into logcapture implemented functionality, and help decide if they suit your requirements.
Get all kandi verified functions for this library.
Get all kandi verified functions for this library.
A testing library for asserting logging messages
QUESTION
Passing quotes to powershell script in c# code doesn't work
Asked 2021-Jan-22 at 19:11I have written c# code which executes powershell scripts with arguments. It works great typically. But, when I try to pass the method an argument that has quotes, such as for a full name, it doesn't seem to recognize the quotes, only the first token.
Here's how I'm creating the script arguments. You can see that the fullName variable should be surrounded by quotes in the resulting scriptArguments....
string scriptArguments = $@"-Phone {phoneNumber} -Name ""{fullName}""";
var result = ExecutePowerShellScript(null, "MyScript.ps1", scriptArguments);
When I look at scriptArguments variable in debug it does appears correctly.
-Phone 321-555-1111 -Name "John Smith"
I can even manually run the shell script and copy/paste the result of scriptArguments in the command line and the ps1 runs perfectly! Such as....
.\MyScript.ps1 -Phone 321-555-1111 -Name "John Smith"
But, when I allow the ExecutePowerShellScript method to run the script it only recognizes the first token of fullName, John. Here's the ExecutePowerShelScript method, in part...
public PowerShellExecutionResult ExecuteScript(string scriptToExecute, string scriptArgument)
{
scriptToExecute = ConfigurationManager.AppSettings[“myscriptpath"] + scriptToExecute;
try
{
using (var process = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = ConfigurationManager.AppSettings["powershell-exe-“path],
Arguments = $"{scriptToExecute} {scriptArgument}"
}
})
{
process.Start();
logCapture.Add(process.StandardOutput.ReadToEnd());
process.WaitForExit();
}
Basically the shell script tells me that "John" already exists in the service database. If anything, it should say "John Smith", not just the first token of the string. Again, if I run this manually it works like a charm. So that would indicate that the problem is not in the shell script itself.
Any idea what is going on? I believe I'm doing everything correctly.
ANSWER
Answered 2021-Jan-22 at 19:11Try to escape the fullName with triple quotes:
string scriptArguments = $"-Phone {phoneNumber} -Name \"\"\"{fullName}\"\"\"";
Triple-escaped quotation marks are necessary to include the character in the final argument. See here.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Find more information at:
Save this library and start creating your kit
Save this library and start creating your kit
Open Weaver – Develop Applications Faster with Open Source