Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/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,通过Microsoft组策略,我定义了在计算机启动时运行Powershell脚本。此外,我还需要在不保存凭据的情况下按计划任务运行Powershell脚本。 在这两种情况下,我都有相同的问题 我想运行Citrix Powershell命令(PSSnapIn),如: 手册: 当然,只有拥有权限的用户才能运行这些Citrix命令。我可以授予域用户运行命令“Set BrokerMachine”的权限,但在上述场景中,PowerShell脚本在系统用户的上下文中运行 我确实用PSExec模拟了系统用户:

通过Microsoft组策略,我定义了在计算机启动时运行Powershell脚本。此外,我还需要在不保存凭据的情况下按计划任务运行Powershell脚本。 在这两种情况下,我都有相同的问题

我想运行Citrix Powershell命令(PSSnapIn),如:

手册:

当然,只有拥有权限的用户才能运行这些Citrix命令。我可以授予域用户运行命令“Set BrokerMachine”的权限,但在上述场景中,PowerShell脚本在系统用户的上下文中运行

我确实用PSExec模拟了系统用户:

我的脚本当然会做其他事情,我想让它们以系统用户的身份运行,但现在我正在寻找一个干净的解决方案来运行那些Citrix命令

如果可能,我不想在脚本中保存凭据

编辑#1:

我将能够使用以下代码解决问题:

$Username = "MySpecialUser"
$Password = 'MyPassword'

$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential $Username, $SecurePassword

$Result = Invoke-Command -Session ( New-PSSession -ComputerName "RemoteServer.domain.local" -Credential $Credential ) -ScriptBlock {  

     Add-PSSnapin Citrix*
     Set-BrokerMachine -MachineName "domain.local\$args" -InMaintenanceMode $True

} -ArgumentList $env:COMPUTERNAME -HideComputerName

Remove-PSSession -InstanceId $Result.RunspaceId
我不喜欢这个,因为:

  • 代码必须包含凭据(ofc我可以加密它…)
  • 我必须在Citrix中为这个特殊用户创建一个权限系统
  • 我必须将特殊用户放入每台服务器上的本地组中,以允许远程管理(安全风险)
  • 我不喜欢使用PSSession

是否有更好/更清洁的解决方案?有什么想法吗?

PowerShell始终在启动它的用户的上下文中运行。您不能使用PowerShell作为登录用户在上下文中运行代码。这是一个Windows安全边界。这就是为什么要使用PSExec。基于你所追求的一些东西。伊姆霍,看看杰亚。PowerShell始终在启动它的用户的上下文中运行。您不能使用PowerShell作为登录用户在上下文中运行代码。这是一个Windows安全边界。这就是为什么要使用PSExec。基于你所追求的一些东西。伊姆霍,看看杰亚。
$Username = "MySpecialUser"
$Password = 'MyPassword'

$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential $Username, $SecurePassword

$Result = Invoke-Command -Session ( New-PSSession -ComputerName "RemoteServer.domain.local" -Credential $Credential ) -ScriptBlock {  

     Add-PSSnapin Citrix*
     Set-BrokerMachine -MachineName "domain.local\$args" -InMaintenanceMode $True

} -ArgumentList $env:COMPUTERNAME -HideComputerName

Remove-PSSession -InstanceId $Result.RunspaceId