Hallo,
obwohl ich die Execution Policy bekomme ich hier eine Excetption, es soll ein robocopy per Powershell aufgerufen werden und sauber geparsed werden!
Fehlermeldung:
System.Management.Automation.Host.HostException: 'A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?'
Hat jemand einen Ansatz für mich woran das liegen könnte?
Code:
public static RobocopyResult RunRobocopy(string script)
{
using (var runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
var robo = @"$RC_Results = " + script + @"
$RC_Summary = $RC_Results | Select-Object -Last 7 | Select-Object -First 5
$RC_Summary[0] = 'Type' + $RC_Summary[0]
$RC_Summary[-1] = $RC_Summary[-1].Insert(39, 'N/A').Insert(49, 'N/A')
$NewRC_Summary = @()
foreach ($Line in $RC_Summary)
{
$NewLine = $Line.Trim()
$NewLine = $NewLine.Replace(' :', '')
$NewLine = $NewLine -replace '\s{2,}', ' '
$NewLine = $NewLine -replace '(\d)\s([bkmg])', '$1$2'
$NewLine = $NewLine.Replace(' ', ',')
$NewRC_Summary += $NewLine
}
$RC_Summary_Object = $NewRC_Summary | ConvertFrom-Csv
$RC_Summary_Object | Format-Table";
var scriptInvoker = new RunspaceInvoke(runspace);
//scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
var pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(robo);
pipeline.Commands.Add("Out-String");
var results = pipeline.Invoke();
runspace.Close();
var stringBuilder = new StringBuilder();
foreach (var obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
var text = stringBuilder.ToString();
var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
var dirs = lines[2].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var files = lines[3].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var sizes = lines[4].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var times= lines[5].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
return new RobocopyResult()
{
TotalDirs = long.Parse(dirs[1]),
TotalFiles = long.Parse(files[1]),
TotalSize = sizes[1],
TotalTimes = times[1],
CopiedDirs = long.Parse(dirs[2]),
CopiedFiles = long.Parse(files[2]),
CopiedSize = sizes[2],
CopiedTimes = times[2],
SkippedDirs = long.Parse(dirs[3]),
SkippedFiles = long.Parse(files[3]),
SkippedSize = sizes[3],
SkippedTimes = times[3],
MismatchDirs = long.Parse(dirs[4]),
MismatchFiles = long.Parse(files[4]),
MismatchSize = sizes[4],
MismatchTimes = times[4],
FailedDirs = long.Parse(dirs[5]),
FailedFiles = long.Parse(files[5]),
FailedSize = sizes[5],
FailedTimes = times[5],
ExtraDirs = long.Parse(dirs[6]),
ExtraFiles = long.Parse(files[6]),
ExtraSize = sizes[6],
ExtraTimes = times[6]
};
}
Du bist doch nun seit über 10 Jahren hier im Forum.
Verwende doch bitte die richtigen Code Tags. Danke!
Funktioniert es mit
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope Process -Force");
anstatt
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
?