将文本从剪贴板传送到Powershell中的提示

时间:2019-03-13 17:36:33

标签: powershell clipboard

我想要做的是从已经打开的PS实例中打开提升的PS实例,然后将命令从剪贴板通过管道传递给提示符,而不仅仅是窗口。

1 个答案:

答案 0 :(得分:0)

1)Copy and Paste with Clipboard from PowerShell;
2)Run with elevated permissions
将第一个与第二个相结合将获得预期的结果。
以下脚本将带有单线的字符串复制到剪贴板,从剪贴板接收单线并将其传递给powershell.exe,以高权限启动该程序。
警告:请谨慎使用双引号。

$re = '^\S+\s+\S+\s+S-1-5-32-544'
"whoami.exe /groups | Select-String \""${re}\""" | clip.exe
Add-Type -AssemblyName System.Windows.Forms
$dummy = New-Object System.Windows.Forms.TextBox
$dummy.Multiline = $true
$dummy.Paste()
$command = $dummy.Text
$args = '-NoLogo', '-NoProfile', '-NoExit', "-Command ${command}"
Start-Process -FilePath powershell.exe -Verb runas -ArgumentList $args

以下脚本说明可以传递多行脚本块。
该脚本将自身启动三次并停止。

& ($b = {
Write-Host $b
$re = '^\S+\s+\S+\s+S-1-5-32-544'
whoami.exe /groups | Select-String $re
if( $b -match 'return\s\}###' ) { return }
'&($b = {' + ($b -replace 'return\s\}', '$0#') + '})' | clip.exe
Add-Type -AssemblyName System.Windows.Forms
$dummy = New-Object System.Windows.Forms.TextBox
$dummy.Multiline = $true
$dummy.Paste()
$command = $dummy.Text
$args = '-NoLogo', '-NoProfile', '-NoExit'
$args += '-Command {0}' -f $command
Start-Process -FilePath powershell.exe -Verb runas -ArgumentList $args
})
相关问题