My Below Code returns me the value of Barcode and Id as "bac","test_id". But now My requirement is to replace the value of Barcode from bac to some other random text value and Id should be concanated with value of Barcode and Id. What should I pass in Script block for replacing the value of Barcode, I figured out for concanating by passing ({-join($.Barcode,$.Id)}) as script in CreateScriptBlock() but how do I do the replace part?
PowerShell ps = PowerShell.Create();
public ScriptBlock CreateScriptBlock(string script)
{
ps.AddScript(script);
return ps.Invoke()[0].BaseObject as ScriptBlock;
}
public IEnumerable<object> RunScriptBlock(ScriptBlock scriptBlock, Dictionary<string, object> scriptParameters)
{
var vars = scriptParameters.Select(p => new PSVariable(p.Key, p.Value)).ToList();
return scriptBlock.InvokeWithContext(null, vars);
}
void Main()
{
//Sample Data created-----------
var row = new Dictionary<string,object>();
row.Add("BarCode", "bac");
row.Add("Id", "test_id");
//---------------------------------
var pars = new Dictionary<string, object>();
pars.Add("_", row);
var scriptBlock1 = CreateScriptBlock(@"{ $_.BarCode, $_.Id }");
var results = RunScriptBlock(scriptBlock1, pars);
results.Dump();
}
Sample Input: {"some random value", "some random value 2"} Sample OutPut: BarCode: some random value and Id:some random value 2