Powershell 启动进程无法执行psexec.exe
我有一个工作脚本,它使用Invoke表达式在Powershell ISE中执行psexecPowershell 启动进程无法执行psexec.exe,powershell,psexec,start-process,Powershell,Psexec,Start Process,我有一个工作脚本,它使用Invoke表达式在Powershell ISE中执行psexec <# $password is encrypted password, need to unencrypt to pass it to psexec #> $bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password) $str = [System.Runtime.InteropServices
<# $password is encrypted password, need to unencrypt to pass it to psexec #>
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$str = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$enable_command = "D:\PSTools\PsExec.exe $comp -u Administrator -p $str -accepteula powershell.exe c:\share\ps_enable.ps1"
Invoke-Expression $enable_command
$bstr=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$str=[System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$enable_command=“D:\PSTools\PsExec.exe$comp-u Administrator-p$str-accepteula powershell.exe c:\share\ps_enable.ps1”
调用表达式$enable_命令
我不想使用Invoke表达式,因为它将数据(包括明文密码)输出到Powershell ISE控制台。但是这个带有启动过程的脚本不起作用
<# $password is encrypted password, need to unencrypt to pass it to psexec #>
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$str = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
Start-Process -FilePath D:\PSTools\PsExec.exe -ArgumentList '$comp', '-u', 'Administrator', '-p', '$str', '-accepteula', 'powershell.exe', 'c:\share\ps_enable.ps1'
$bstr=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$str=[System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
启动进程-文件路径D:\PSTools\PsExec.exe-ArgumentList'$comp','-u',Administrator','-p','$str','-accepteula',powershell.exe',c:\share\ps_enable.ps1'
如何修复?如何在变量中捕获Invoke表达式,或将其导出为Null
$CmdOutput = Invoke-Expression $enable_command
或
编辑:好的,我忘了PSExec喜欢使用StdErr作为显示部分文本的方法,而这些文本不会捕获该部分。您可以做的是将StdErr重定向到StdOut,然后通过管道输出Null或按照建议捕获它。试试这个:
$CmdOutput = Invoke-Expression $enable_command 2>&1
它能显示一切吗?我不确定我是否理解这个问题。您是否培训用户运行ISE并执行此脚本,并且不希望他们看到它使用的密码?好的,请在回答中检查我的编辑。我有一种感觉,你会对这一结果感到非常高兴。我正在为我们的远程技术人员创建一个脚本,以便在他们的站点的计算机上远程部署和安装软件。我想避免脚本落入坏人之手,管理员密码被泄露。如果您担心脚本落入坏人之手,这将无法解决该问题。只要有人编辑并删除
2>&1
,密码就会再次以纯文本显示。将加密密码转换为纯文本始终存在安全风险。一直都是,让我们来。
$CmdOutput = Invoke-Expression $enable_command 2>&1