2015-04-03 112 views
0

我想从C#代码执行PowerShell脚本。我不想将我的PowerShell脚本存储在任何文件夹中。脚本是一种大,它有一个参数“列表”和“字符串”,如何做到这一点。将powershell脚本添加到C#代码

$ComputerName = "win12" 
    $username="administrator" 
    $password="[email protected]" 

    $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 

这是我的powershell脚本。

+1

你看过[PowerShell.Invoke](https://msdn.microsoft.com/en-us/library/dd182449%28v=vs.85%29.aspx)吗?谷歌和StackOverflow搜索没有价值? – HABO 2015-04-03 03:19:53

回答