Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从脚本中提升Powershell脚本_Powershell - Fatal编程技术网

如何从脚本中提升Powershell脚本

如何从脚本中提升Powershell脚本,powershell,Powershell,我是Powershell的新手,但在很多方面我都很喜欢它。出于各种原因,我创建了一个脚本,从硬盘上的文件加载一组本地管理员凭据,并创建一个PSCredential对象$MyCred 我想使用$MyCred来提升一个单独的脚本(对打开的RDP连接进行一些注册表更改)。我已尝试将$MyCred传递到Start Process cmdlet: 启动进程Powershell.exe-凭证$MyCredential 然后我收到以下错误: 启动进程:由于以下错误,无法运行此命令:目录名无效 如果使用-cre

我是Powershell的新手,但在很多方面我都很喜欢它。出于各种原因,我创建了一个脚本,从硬盘上的文件加载一组本地管理员凭据,并创建一个PSCredential对象$MyCred

我想使用$MyCred来提升一个单独的脚本(对打开的RDP连接进行一些注册表更改)。我已尝试将$MyCred传递到Start Process cmdlet:

启动进程Powershell.exe-凭证$MyCredential

然后我收到以下错误:

启动进程:由于以下错误,无法运行此命令:目录名无效

如果使用-credential和空变量运行启动进程,系统会提示输入用户名和密码。当我输入它们时,我会得到一个提升的powershell提示符,没有任何问题,并且能够在注册表中对测试系统进行更改

我已经验证了$myCred的内容,它正确地存储了U/N和p/W(如中所示,与我手动输入的内容相同)。使用新的PSSEssion-like

新建PSSession-凭证$MyCredential

返回拒绝访问,我已经读到,在许多系统上默认情况下,访问也被禁用

在理想情况下,代码将类似于:

启动进程Powershell.exe-凭证$MyCredential-文件C:\MyScript.ps1

这将启动一个提升的powershell,在第二个脚本中运行一些命令,然后终止。我错过了什么

谢谢你给我的一切帮助,谢谢!我可能是一件非常明显的事情



背景是,我们有一些计算机,我们自己无法得到我们的手,也无法通过RDP达到。我们的网站上确实有用户可以为我们运行脚本。但重要的是,他们不能获得本地管理员密码。因此,我们希望创建一个脚本,该脚本读取加密的密码文件,生成本地admin PSCredential对象,并将其传递给一个脚本,该脚本进行必要的注册表更改以允许RDP访问

你错过了NoExit这个论点

Start-Process -Filepath "Powershell.exe" -ArgumentList "-NoExit -File "C:\myscript.ps1" -Credential $MyCredential 

关于这个主题有很多用例文章。 使用“PowerShell自升脚本”进行快速web搜索,即可显示这些脚本。 甚至直接从微软那里


此代码来自一个有用的指南:

它检查当前脚本的安全性,如果需要提升,脚本将作为管理员重新启动。如果UAC已启用,它将提示您确认。重新启动后,它将具有必要的访问权限,并在检查后运行代码

# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
 if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
  $CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
  Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
  Exit
 }
}

# Remainder of script here

更新:我发现提升后,这将无法维护当前的工作目录。

Hi Wasif,感谢您的输入。现在我得到:>启动进程:找不到接受参数“Powershell.exe”的位置参数。您好,瓦西夫,再次感谢,但我仍然收到该错误?不完全是我想要的,因为这不会将PSCredential对象传递到新的PS窗口。但是谢谢,我很感兴趣地读了它。
# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
 if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
  $CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
  Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
  Exit
 }
}

# Remainder of script here