将powershell脚本添加到C#代码
问题描述:
我想从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脚本。
答
将PowerShell脚本作为嵌入资源存储在程序集中,然后在运行时提取脚本并将其提供给PowerShell.AddScript(string script)方法。调用PowerShell.Invoke()方法来执行脚本。
你看过[PowerShell.Invoke](https://msdn.microsoft.com/en-us/library/dd182449%28v=vs.85%29.aspx)吗?谷歌和*搜索没有价值? – HABO 2015-04-03 03:19:53