是否可以编写自修改的 PowerShell 脚本?
是否可以编写自修改的 PowerShell 脚本?
假设我有一个名为 foo.ps1
的脚本:
"Write-Host `"Goodbye world`"" | Set-Content "foo.ps1"
Write-Host "Hello world"
该脚本打印出 Hello world
。我可以使用什么技术让它打印出再见世界
?
Is it possible to write self-modifying PowerShell scripts?
Suppose I have a script named foo.ps1
:
"Write-Host `"Goodbye world`"" | Set-Content "foo.ps1"
Write-Host "Hello world"
This script prints out Hello world
. What techniques could I use to get it to print out Goodbye world
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说这没有意义,修改脚本后,您需要重新点源它才能获得新的语句。
该脚本将自身更改三次:
并打印:
另一种可能性是编写一个接受脚本块作为输入参数的脚本。
To me it does not make sense, after you have modified a script, you need to re-dotsource it in order to get new statments.
This script changes itself three times:
and prints:
Another possibility is to write a script which accepts as input parameters script blocks.
是的,你和我都会有更好的方法来完成你的任务并完成任务。
Yes you an however I would be there is a better way to do what you and to accomplish.