1

I am trying to call PowerShell ISE Script from the C#.

I have command that I am running it on the PowerShell

. .\Commands.ps1; Set-Product -bProduct 'Reg' -IPPoint 'ServerAddress' -Location  'testlocation' -Terminal 3

Now I am trying to create the Command with the c# I have wrote some code Like this.

//Set Execution Policy to un restrict
            powershell.AddCommand("Set-ExecutionPolicy");
            powershell.AddArgument("unrestricted");
            powershell.Invoke();
            powershell.Commands.Clear();

        

powershell.AddScript("K:\\Auto\\Cases\\Location\\Commands.ps1", false);
            powershell.AddArgument("Set-Product").AddParameter("bProduct ", "Reg").
                AddParameter("IPPoint", "ServerAddress").
                AddParameter("Location", "testlocation").AddParameter("Terminal", 3);

            powershell.Invoke();

I can see its running fine. But its not updating values in my xml file. It suppose to update my values in file. When I try to run it with powershell It does run and works file. But c# code does not work.

Any hint or clue will be appreciated.

0

1 Answer 1

5

Mind the semicolon, so this is basically two statements:

1.) Dot-sourcing the script Commands.ps1

. .\Commands.ps1

2.) Invoking the cmdlet Set-Product

Set-Product -bProduct 'Reg' -IPPoint 'ServerAddress' -Location  'testlocation' -Terminal 3

So, you have to treat them as such. Also, AddScript expects code, not a file name.

powershell
    // dot-source the script
    .AddScript(@". 'K:\Auto\Cases\Location\Commands.ps1'")

    // this is the semicolon = add another statement
    .AddStatement()

    // add the cmdlet
    .AddCommand("Set-Product")
    .AddParameter("bProduct", "Reg")
    .AddParameter("IPPoint", "ServerAddress")
    .AddParameter("Location", "testlocation")
    .AddParameter("Terminal", 3)

    // invoke all statements
    .Invoke();

(Alternatively to AddStatement() you can of course split this up in two calls and call Invoke() twice.)

Sign up to request clarification or add additional context in comments.

2 Comments

So. it just need . there and it will work fine ?
Thanks that works AddScript(@". K:\Auto\Cases\Location\Commands.ps1")

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.