我了解您无法提升现有进程,但您可以创建具有提升权限的新进程。
目前我有两个脚本,其中一个脚本创建提升的权限并调用另一个。
# script1.ps1
$abc = $args
$startInfo = $NULL
$process = $NULL
$standardOut = $NULL
$userId = $NULL
$password = get-content C:\cred.txt | convertto-securestring
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell.exe"
$startInfo.Arguments = "C:\script2.ps1 " + $abc
$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$startInfo.Username = "username"
$startInfo.Domain = "DOMAIN"
$startInfo.Password = $password
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$userId = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()
return $userId
一开始想在script1.ps1中创建一个函数New_Function,通过$startInfo.Arguments启动,即$startInfo.Arguments = New_Function
$abc = $args
$startInfo = $NULL
$process = $NULL
$standardOut = $NULL
$userId = $NULL
Function New_Function(){
$foo = "Hello World"
return $foo
}
$password = get-content C:\cred.txt | convertto-securestring
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell.exe"
$startInfo.Arguments = New_Function
$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$startInfo.Username = "username"
$startInfo.Domain = "DOMAIN"
$startInfo.Password = $password
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$userId = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()
return $userId
我得到以下错误,而不是“Hello World”被打印到屏幕上,
The term 'Hello' is not recognized as the name of a cmdlet, function, script fi
le, or operable program. Check the spelling of the name, or if a path was inclu
ded, verify that the path is correct and try again.
At line:1 char:6
+ Hello <<<< World
+ CategoryInfo : ObjectNotFound: (Hello:String) [], CommandNotFou
ndException
+ FullyQualifiedErrorId : CommandNotFoundException
有任何想法吗???