如何拥有父脚本但又保持与 Powershell 脚本的关注点分离?
因此,我正在为产品专用的特定 IIS 站点设置编写一些 IIS 管理脚本,以完成以下任务:
- Create Site
- Create App Pool
- Create Virtual directories
问题是,我想为每个问题保留单独的脚本,并在父脚本中引用它们。可以运行父脚本来执行完整的部署/设置。或者您可以为特定任务运行单独的脚本。问题在于它们是交互式的,因此它们会请求与完成任务相关的用户信息。
我不确定如何解决每个脚本都有一个脚本主体来从用户获取信息的问题,但如果将其加载到父脚本中,请避免该特定脚本的脚本主体提示用户。
注意:我知道我可以将它们放入模块中,并关闭单独的“导出到环境”功能,但是此脚本将被移动到需要设置的环境,并且必须手动放置模块(psm1)文件进入正确的 PowerShell 模块文件夹只是为了运行脚本,这是我不太喜欢的方法。
我是使用 Powershell 编写脚本的新手,有什么想法或建议吗?
可能的答案*
这可能是(a)解决方案:但我发现我可以从工作目录导入模块,并从那里访问这些导出的函数。
我也对任何其他建议感兴趣。
So I am working on some IIS management scripts for a specific IIS Site setup exclusive to a product to complete tasks such as:
- Create Site
- Create App Pool
- Create Virtual directories
The problem, is I would like to keep separate scripts for each concern and reference them in a parent script. The parent script could be ran to do a full deployment/setup. Or you could run the individual scripts for a specific task. The problem is that they are interactive, so they will request for the user information relevant to completing the task.
I am not sure how to approach the problem where each script will have a script body that will acquire information from the user, yet if it is loaded into the parent script avoid that specific's scripts script body from prompting the user.
NOTE: I know I could put them into modules, and fire off the individual "Exported to the environment" functions, but this script is going to be moved around to the environment that needs setup, and having to manually put modules (psm1) files into the proper PowerShell module folders just to run the scripts is a route I am not particularly fond of.
I am new to scripting with Powershell, any thoughts or recommendations?
Possible Answer*
This might be (a) solution: but I found I could Import-Modules from the working directory and from there have access to those exported functions.
I am interested in any other suggestions as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我解决这个问题的方法是在每个子脚本的顶部实现一个参数块,该块将收集运行所需的信息。如果单独运行子脚本,参数块将提示用户输入运行该单独脚本所需的数据。这还允许父脚本在父脚本调用子脚本时传递运行子脚本所需的数据。所需的数据可以硬编码在父脚本中或提示或它们的某种混合。这样您就可以使子脚本以静默方式运行或通过用户交互运行。您可以通过 Powershell 的参数处理机制免费获得用户交互。在下标中添加一个参数属性,以指示 Powershell 将向用户请求这些特定参数值(如果调用脚本尚未提供这些值)。
在子脚本的顶部,使用参数块来收集所需的数据。
They way I would address it would be to implement a param block at the top of each sub script that would collect the information it needs to run. If a sub script is run individually the param block would prompt the user for the data needed to run that individual script. This also allows the parent script to pass the data needed to run the subscripts as the parent script calls the sub script. The data needed can be hard coded in the parent script or prompted for or some mixture thereof. That way you can make the sub scripts run either silently or with user interaction. You get the user interaction for free from Powershell's parameter handling mechanism. In the subscripts add a parameter attribute to indicate that Powershell will request those particular parameter values from the user if they are not already provided by the calling script.
At the top of your sub scripts, use a parameter block to collected needed data.
您可以有一个
deploy.ps1
脚本,该脚本点源各个脚本,然后调用其中必要的函数:在各个函数中,您可以查看所需的值是否从调用者传入(部署.ps1 或命令行)
例如,create-site.ps1 将类似于:
You can have a
deploy.ps1
script which dot sources the individual scripts and then calls the necessary functions within them:In the individual functions, you can see if the values needed are passed in from the caller ( deploy.ps1 or the command line)
For example, create-site.ps1 will be like:
理想的情况是让模块根据该模块的分发问题负责维护存储其自己的设置,并发出命令来帮助处理设置。
即
编写一个 Set-SiteInfo -Name -Pool -VirtualDirectory 并将该值存储在注册表或模块的本地目录 ($psScriptRoot) 中,然后让模块中的其他命令使用它。
如果模块被放置在低权限用户没有文件写入访问权限的位置(即网站目录或 $psHome),那么最好将这些值存储在注册表中。
希望这有帮助
The ideal is to make the module take care of maintaining storing it's own settings, depending on distribution concerns of that module, and to make commands to help work with the settings.
I.e.
Write a Set-SiteInfo -Name -Pool -VirtualDirectory and have that store values in the registry or in the local directory of the module ($psScriptRoot), then have other commands in the module use this.
If the module is being put in a location where there's no file write access for low-rights users (i.e. a web site directory, or $psHome), then it's a notch better to store the values in the registry.
Hope this Helps