首页 > 解决方案 > 我可以以不同的用户身份执行安装吗?

问题描述

我们需要使用我们创建的特定域帐户执行以下命令,但我无法将命令传递给新的 Powershell 会话。我得到的错误是

“...目录名无效”

但是,当我执行一些简单的操作(例如使用相同的代码运行 IE)时,一切正常。

##Create new credential
$username = "DOMAIN\Username"

$password = "P@ssword"

$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

##ScriptBlock to pass to new PS Session started with new credential
$ScriptBlock = { 

$args = "/s"

$username = "DOMAIN\Username"

$password = "P@ssword"

$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

Start-Process -FilePath C:\TEMP\WinMagic\SecureDoc_64.exe -WorkingDirectory C:\TEMP\WinMagic\ -credential $credentials

}

##Execute new powershell session with new credential and scriptblock
Start-Process -FilePath powershell.exe -credential $credentials -ArgumentList "-noexit" , {$ScriptBlock}  

标签: powershell

解决方案


推荐阅读