0

I want to execute a powershell script from C# code. I don't want to store my powershell script on any folder. script is kind of BIG and it has a parameters a "List" and "string", how to do this.

    $ComputerName = "win12"
    $username="administrator"
    $password="passw0rd@12"

   $secstr = New-Object -TypeName System.Security.SecureString
   $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
   $fname="v.exe"
   $fname="C:\"+$fname

     $cr = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
      $Session = New-PSSession -ComputerName $ComputerName -Credential $cr
       Invoke-Command -Session $Session -ScriptBlock {
  Start-Process -FilePath $($args[0]) -ArgumentList '/a' -Verb runas -WindowStyle Normal
  $process=[io.path]::GetFileNameWithoutExtension( $($args[0]));
 $a = (Get-Process -Name $process)
 $a.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::RealTime
  } -ArgumentList $fname

this is my powershell script.

1
  • 1
    Have you looked at PowerShell.Invoke? Did Google and StackOverflow searches turn up nothing of value? Commented Apr 3, 2015 at 3:19

1 Answer 1

2

Store the PowerShell script as an embedded resource in the assembly then at runtime extract the script and supply it to the PowerShell.AddScript(string script) method. The call the PowerShell.Invoke() method to execute the script.

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

Comments

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.