2015-04-03 119 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

回答