我正在使用字符串生成器来解析psobject数据,并在输出中获得额外的反斜杠,例如(“ {\” SCOM_MM_Result \“:{” Server \“:),如何避免反斜杠。
命名空间SCOMWebAPI.Services { 公共类MaintennceModeService { 私有静态IEnumerable结果; //私有静态只读对象jsonStr;
internal static string post(MaintenanceMode value, SCOMController sCOMController)
{
// create Powershell runspace
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
Command myCommand = new Command("C:\\SCOM_MM_POST_API.ps1");
CommandParameter Param = new CommandParameter("mgmtserver",value.mgmtserver);
CommandParameter Param1 = new CommandParameter("Computername", value.ComputerName);
CommandParameter Param2 = new CommandParameter("numberOfMin", value.numberOfMin);
CommandParameter Param3 = new CommandParameter("ReasonComment", value.ReasonComment);
myCommand.Parameters.Add(Param);
myCommand.Parameters.Add(Param1);
myCommand.Parameters.Add(Param2);
myCommand.Parameters.Add(Param3);
pipeline.Commands.Add(myCommand);
results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
stringBuilder = stringBuilder.Replace(System.Environment.NewLine, string.Empty);
stringBuilder.Replace(@"\\", string.Empty);
return stringBuilder.ToString();
}
}
}