I've heard powershell 2.0 CTP has modules, but I can't find much example code or instructions. I've read what little help there seems to be online...
我听说过powershell 2.0 CTP有模块,但我找不到很多示例代码或指令。我看过网上似乎有什么帮助......
But I just keep getting "The term 'Add-Module' is not recognized as a cmdlet..." when I try and load a module.
但是当我尝试加载模块时,我只是继续“使用术语'添加模块'不被识别为cmdlet ...”。
Any help would be gratefully received!
任何帮助都会感激不尽!
Edit (July 2010) Please note this question is based on powershell 2.0 CTP and is therefore a year and half out of date! Please see Samuel Jack's answer for help with the powershell 2.0 RTM.
编辑(2010年7月)请注意这个问题基于powershell 2.0 CTP,因此是一年半过时了!请参阅Samuel Jack的答案,以获取有关powershell 2.0 RTM的帮助。
5 个解决方案
#1
9
With the Win7 build, Add-Module is gone. The new cmdlet is Import-Module. The easiest way to create a module is rename a PS1 file to a PSM1 file. From there you can do all sorts of things including the module manifest.
使用Win7构建,Add-Module消失了。新cmdlet是Import-Module。创建模块的最简单方法是将PS1文件重命名为PSM1文件。从那里你可以做各种各样的事情,包括模块清单。
#2
3
I'm no Powershell expert, but here's what I just figured out using PowerShell 2.0 RTM.
我不是Powershell专家,但这就是我刚才用PowerShell 2.0 RTM想出来的。
Suppose you want to create a module called MyModule:
假设您要创建一个名为MyModule的模块:
- Make sure that you have created the folder %My Documents%\WindowsPowershell\Modules
- Create a folder inside Modules called MyModule
- Put your code in a file inside MyModule and name the file MyModule.psm1
- Remember to use the Export-ModuleMember command as the last thing in your script file.
Export-ModuleMember -Function * -Alias *
will export all functions and aliases - In scripts where you want to use the module, use the command
Import-Module MyModule
确保您已创建文件夹%My Documents%\ WindowsPowershell \ Modules
在名为MyModule的Modules中创建一个文件夹
将您的代码放在MyModule中的文件中,并将文件命名为MyModule.psm1
请记住使用Export-ModuleMember命令作为脚本文件中的最后一项。 Export-ModuleMember -Function * -Alias *将导出所有函数和别名
在要使用该模块的脚本中,使用命令Import-Module MyModule
By default Powershell is configured not to run any kinds of scripts from files, so you need to alter the security settings. Set-ExecutionPolicy Unrestricted
will get you going if you're not concerned about scripts needing to be signed.
默认情况下,Powershell配置为不从文件运行任何类型的脚本,因此您需要更改安全设置。如果您不关心需要签名的脚本,Set-ExecutionPolicy Unrestricted将帮助您。
#3
2
Here's a little bit of information. http://huddledmasses.org/powershell-modules/
这里有一些信息。 http://huddledmasses.org/powershell-modules/
Let's hope that the upcoming CTP3 has some useful documentation about modules.
让我们希望即将推出的CTP3有一些关于模块的有用文档。
#4
1
Modules will hopefully solve a few problems. Right now, we can use dot sourcing to get functions, variables, and scripts into a PowerShell session's global scope.
模块有望解决一些问题。现在,我们可以使用点源来将函数,变量和脚本放入PowerShell会话的全局范围。
The problem is that this can pollute your session with all kinds of global variables and helper functions that an end user may not want/need directly.
问题是,这可能会污染您的会话,其中包含最终用户可能不需要/不需要的各种全局变量和辅助函数。
Modules will allow you as an author to build scripts and only make certain functions/variables avaiable to the end user of the module.
模块将允许您作为作者构建脚本,并且只为模块的最终用户提供某些功能/变量。
They also essentially replace the concept of a PSSnapin. You can use Add-Module Some.dll to add an assembly that has cmdlets in it.
它们也基本上取代了PSSnapin的概念。您可以使用Add-Module Some.dll添加包含cmdlet的程序集。
What is really cool is what is called a Module Manifest. This is a hash table that basically specifies all kinds of dependcies as well as author, name, GUID Identifier, and version number. When a user loads a module that has a module manifest, it will check all the dependencies and run any scripts the module author deems necessary.
什么是真正酷的是所谓的模块清单。这是一个哈希表,基本上指定了各种依赖项以及作者,名称,GUID标识符和版本号。当用户加载具有模块清单的模块时,它将检查所有依赖项并运行模块作者认为必要的任何脚本。
There should be some decent documentation on these when CTP3 ships.
当CTP3发货时,应该有一些关于这些的正确文件。
Hope that helps a bit.
希望有点帮助。
Andy
#5
0
Windows PowerShell v2.0: TFM (sapienpress.com) has information and samples in one of the chapters. It's available as an ebook which is updated as new CTPs are released. I also blogged about them on ConcentratedTech.com, and there's been discussion on them at PowerShellCommunity.org in the forums.
Windows PowerShell v2.0:TFM(sapienpress.com)在其中一章中包含信息和示例。它可以作为电子书使用,随着新CTP的发布而更新。我还在ConcentratedTech.com上发表了关于它们的博客,并在论坛中的PowerShellCommunity.org上对它们进行了讨论。
#1
9
With the Win7 build, Add-Module is gone. The new cmdlet is Import-Module. The easiest way to create a module is rename a PS1 file to a PSM1 file. From there you can do all sorts of things including the module manifest.
使用Win7构建,Add-Module消失了。新cmdlet是Import-Module。创建模块的最简单方法是将PS1文件重命名为PSM1文件。从那里你可以做各种各样的事情,包括模块清单。
#2
3
I'm no Powershell expert, but here's what I just figured out using PowerShell 2.0 RTM.
我不是Powershell专家,但这就是我刚才用PowerShell 2.0 RTM想出来的。
Suppose you want to create a module called MyModule:
假设您要创建一个名为MyModule的模块:
- Make sure that you have created the folder %My Documents%\WindowsPowershell\Modules
- Create a folder inside Modules called MyModule
- Put your code in a file inside MyModule and name the file MyModule.psm1
- Remember to use the Export-ModuleMember command as the last thing in your script file.
Export-ModuleMember -Function * -Alias *
will export all functions and aliases - In scripts where you want to use the module, use the command
Import-Module MyModule
确保您已创建文件夹%My Documents%\ WindowsPowershell \ Modules
在名为MyModule的Modules中创建一个文件夹
将您的代码放在MyModule中的文件中,并将文件命名为MyModule.psm1
请记住使用Export-ModuleMember命令作为脚本文件中的最后一项。 Export-ModuleMember -Function * -Alias *将导出所有函数和别名
在要使用该模块的脚本中,使用命令Import-Module MyModule
By default Powershell is configured not to run any kinds of scripts from files, so you need to alter the security settings. Set-ExecutionPolicy Unrestricted
will get you going if you're not concerned about scripts needing to be signed.
默认情况下,Powershell配置为不从文件运行任何类型的脚本,因此您需要更改安全设置。如果您不关心需要签名的脚本,Set-ExecutionPolicy Unrestricted将帮助您。
#3
2
Here's a little bit of information. http://huddledmasses.org/powershell-modules/
这里有一些信息。 http://huddledmasses.org/powershell-modules/
Let's hope that the upcoming CTP3 has some useful documentation about modules.
让我们希望即将推出的CTP3有一些关于模块的有用文档。
#4
1
Modules will hopefully solve a few problems. Right now, we can use dot sourcing to get functions, variables, and scripts into a PowerShell session's global scope.
模块有望解决一些问题。现在,我们可以使用点源来将函数,变量和脚本放入PowerShell会话的全局范围。
The problem is that this can pollute your session with all kinds of global variables and helper functions that an end user may not want/need directly.
问题是,这可能会污染您的会话,其中包含最终用户可能不需要/不需要的各种全局变量和辅助函数。
Modules will allow you as an author to build scripts and only make certain functions/variables avaiable to the end user of the module.
模块将允许您作为作者构建脚本,并且只为模块的最终用户提供某些功能/变量。
They also essentially replace the concept of a PSSnapin. You can use Add-Module Some.dll to add an assembly that has cmdlets in it.
它们也基本上取代了PSSnapin的概念。您可以使用Add-Module Some.dll添加包含cmdlet的程序集。
What is really cool is what is called a Module Manifest. This is a hash table that basically specifies all kinds of dependcies as well as author, name, GUID Identifier, and version number. When a user loads a module that has a module manifest, it will check all the dependencies and run any scripts the module author deems necessary.
什么是真正酷的是所谓的模块清单。这是一个哈希表,基本上指定了各种依赖项以及作者,名称,GUID标识符和版本号。当用户加载具有模块清单的模块时,它将检查所有依赖项并运行模块作者认为必要的任何脚本。
There should be some decent documentation on these when CTP3 ships.
当CTP3发货时,应该有一些关于这些的正确文件。
Hope that helps a bit.
希望有点帮助。
Andy
#5
0
Windows PowerShell v2.0: TFM (sapienpress.com) has information and samples in one of the chapters. It's available as an ebook which is updated as new CTPs are released. I also blogged about them on ConcentratedTech.com, and there's been discussion on them at PowerShellCommunity.org in the forums.
Windows PowerShell v2.0:TFM(sapienpress.com)在其中一章中包含信息和示例。它可以作为电子书使用,随着新CTP的发布而更新。我还在ConcentratedTech.com上发表了关于它们的博客,并在论坛中的PowerShellCommunity.org上对它们进行了讨论。