Я понимаю, что вы не можете повысить права существующего процесса, но вы можете создать новый процесс с повышенными привилегиями.
В настоящее время у меня есть два сценария, где один сценарий создает повышенные привилегии и вызывает другой.
# 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
Сначала думал создать функцию New_Function в script1.ps1 и запускать через $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
Есть идеи???
$startInfo.Arguments = New_Function
может установить$startInfo.Arguments
в Hello World, поэтому результирующая командная строка будетpowershell.exe hello world
. Попробуйте установить точку останова в New_Function и посмотреть, когда она будет вызвана. - person JohnL   schedule 07.10.2013