0
public string RunScript(string scriptText)
{
   Runspace runspace = RunspaceFactory.CreateRunspace();
   runspace.Open();

   Pipeline pipeline = runspace.CreatePipeline();
   pipeline.Commands.AddScript(scriptText);

   pipeline.Commands.Add("Out-String");

   Collection<PSObject> results = pipeline.Invoke();

   runspace.Close();

   StringBuilder stringBuilder = new StringBuilder();
   foreach (PSObject obj in results)
   {
       stringBuilder.AppendLine(obj.ToString());
   }

   return stringBuilder.ToString();
}

public string LoadScript(string filename)
{
    try
    {

        using (StreamReader sr = new StreamReader(filename))
        {

            StringBuilder fileContents = new StringBuilder();

            string curLine;

            while ((curLine = sr.ReadLine()) != null)
            {

                fileContents.Append(curLine + "\n");
            }

            return fileContents.ToString();
        }
    }
    catch (Exception e)
    {

        string errorText = "The file could not be read:";
        errorText += e.Message + "\n";
        return errorText;
    }
}

This is my class library. when first time i press the button my script runs fine but then again pressed it raise the exception:

Cannot invoke this function because the current host does not implement it.

If i deleted the folder Logs which is created during script execution then again it works fine.

My script:

rmdir D:\Logs
mkdir D:\Logs
Get-EventLog Application |Format-List | Out-File D:\Logs\app.txt
Get-EventLog System |Format-List | Out-File D:\Logs\app.txt

1 Answer 1

1

rmdir needs you to confirm before deleting the folder if the folder is not empty (you need to type 'Y' and then Enter). And that is why you get an exception because user interaction is not supported.

Use the Remove-Item command with -Recurse -Force parameters can silently remove the directory. Try to use the following commands instead of rmdir/mkdir.

Remove-Item D:\Logs -Recurse -Force
New-Item -ItemType directory -Path D:\Logs
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much.. It works for me but it take too much time to generate the result .is there any way to generate faster result?
I am afraid it has to be that long, one my machine the file is about 60M, there are a lot of entries in the event log
so there is no way to faster the solution? and if i want to read i file from location given by user then what should i do?
First you can do this on a background thread so UI can remain responsive. But I don't know how to make it faster... Maybe if you only need a portion of the event log (like from a certain application), you can use different parameters so Get-EventLog only exports the entries you need, thus making it faster
Hey When I added Write -host method to script it show me the same error.IS there any other way to show to user that which line of script is executed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.