0

目标:在不显示窗口的情况下运行powershell脚本(如果它弹出几秒钟就可以了)。

问题:脚本tcplisten.ps1仅在窗口显示给用户时才有效。以下所有尝试均无效。因为当我运行时netstat -ano -p tcp,端口9999没有在监听。

tcplisten.ps1

$Listener = [System.Net.Sockets.TcpListener]9999;
$Listener.Start()

尝试:

电源外壳程序

powershell.exe -windowstyle hidden .\tcplisten.ps1

隐藏命令

powershell -windowstyle hidden -command $Listener = [System.Net.Sockets.TcpListener]9999; $Listener.Start()

-NoProfile -NonInteractive -ExecutionPolicy 绕过

powershell -NoP -NonI -W Hidden -Exec Bypass -Command

启动过程

Start-Process powershell.exe -ArgumentList "-WindowsStyle hidden -file .\tcplisten.ps1"

vbs 脚本

使用此脚本创建一个 .vbs 并运行它

command = "powershell.exe -nologo -command C:\Users\Utente\Desktop\tcplisten.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0

问:有解决办法吗?我做错了什么吗?

4

2 回答 2

0

试试这个 vbscript :

Option Explicit
Dim Ws,Port,StrCmd
Set Ws = WScript.CreateObject("WScript.Shell")
Port = 9999
REM We can run this script as admin or not 
Call RunAsAdmin()
Call Start_TcpListener(Port)
REM We try to launch the default browser by opening this url with port
ws.Run """http://127.0.0.1:9999""",1,False
wscript.sleep 5000
REM Here just for checking if the port is open or not !
StrCmd = "CMD /K Title Checking Openning Port on "& Port & "& netstat -ano -p tcp | find "& chr(34) & Port & chr(34) &""
Ws.Run StrCmd,1,True
'------------------------------------------------------------------------------
Sub Start_TcpListener(Port)
    Dim Ws,PS_CMD,shell
    Set Ws = WScript.CreateObject("WScript.Shell")
    PS_CMD = "$Listener = [System.Net.Sockets.TcpListener]"& Port &";" & vbCrLF &_
    "$Listener.Start{(}{)}~cls~"
    ws.run "Powershell",1,False
    ws.AppActivate "PowerShell"
    wscript.sleep 2000
    Ws.sendkeys(PS_CMD)
    wscript.sleep 2000
    Set shell = CreateObject("Shell.Application")
    Shell.MinimizeAll
End Sub
'------------------------------------------------------------------------------
Sub RunAsAdmin()
If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , chr(34) & WScript.ScriptFullName & chr(34) & " /elevate", "", "runas", 1
  WScript.Quit
End If
End Sub
'------------------------------------------------------------------------------
于 2020-06-20T06:05:41.393 回答
0

尝试将其作为后台作业运行。此命令在计算机而不是任何用户上运行,因此在您注销时它会继续运行:

Invoke-Command -ComputerName $env:COMPUTERNAME -FilePath 'C:\Users\Utente\Desktop\tcplisten.ps1' -AsJob

于 2021-02-26T16:22:17.927 回答