84366992 发表于 2018-9-3 08:19:40

PowerShell笔记:powershell和服务器管理器

  1. PowerShell管理单元(Snap-in): Windows PowerShell管理单元是被编译成DLL文件的.NET程序,包括提供程序、cmdlet和函数。Windows PowerShell包含一些内置的管理单元,同时我们也可以自己手动添加自己写的管理单元 。
  Windows PowerShell V2内置管理单元包括:Microsoft.PowerShell.Core, Microsoft.PowerShell.Host, Microsoft.PowerShell.Management, Microsoft.PowerShell.Security, Microsoft.PowerShell.Utility, Microsoft.PowerShell.Diagnostics, Microsoft.PowerShell.WSMan.Management
  查看计算机中的管理单元:get-pssnapin
  查看每个Windows PowerShell提供程序所属的管理单元:get-psprovider | format-list name,pssnapin
  查看某个管理单元中的cmdlet:get-command -module
  对于内置的管理单元,每次启动Windows PowerShell时会被自动添加到会话中来。而对于我们自己写的或者从别的地方得到的管理单元,则需要首先注册这些管理单元然后再将他们添加到会话中来,"get-pssnapin -registered"命令可以用来查看你系统中注册过的管理单元,那么如何来注册管理单元呢?可以参考http://msdn.microsoft.com/en-us/library/ms714644(VS.85).aspx
  将一个管理单元添加到当前对话:add-snapin
  将一个管理单元移除出当前对话:remove-snapin
  将管理单元添加到当前对话中后,管理单元只在当前会话中可用,如果我们想要在将来的会话中继续使用此管理单元,我们需要将它添加到配置文件中,首先将管理单元保存到一个控制台文件(.psc1),然后在需要使用此管理单元时用这个控制台文件启动PowerShell。
  保存到控制台文件:export-console
  使用控制台文件启动PowerShell:powershell.exe -psconsolefile
  2. Windows PowerShell提供程序(providers)提供的数据显示在一个驱动器中,可以像浏览硬盘一样浏览该驱动器,并且允许查看、查找和管理相关的数据。
  PowerShell内置的提供程序:Alias, Certificate, Environment, FileSystem, Function, Registry, Variable, WSMan.
  列出所有可用的提供程序: get-psprovider
  获得一个提供程序驱动器的所有属性:get-psdrive| format-list *
  查看驱动器的内容:get-childitem :\childitem
  举例:get-childitem hklm:\software
  进入驱动器:set-location :
  3. Windows PowerShell模块(module): 模块包含了若干Windows PowerShell命令。比如cmdlets,提供程序(Providers),函数(functions),变量(variables),别名(aliases)。
  Windows PowerShell模块是独立的、可重用的执行单元,可以包括以下内容:

[*]通过.PSM1文件调用的脚本函数;
[*]被编译成.DLL文件并且通过.PSD1调用的.NET组件;
[*]通过.DLL调用的PowerShell管理单元;
[*]在.PS1XML文件中描述的自定义视图和数据类型。
  使用模块需要执行以下任务:

[*]安装模块
  如果当前用户没有模块文件夹为当前用户创建模块文件夹:new-item -type directory -path $home\Documents\WindowsPowerShell\Modules
  将整个要安装的模块文件夹copy到上面的文件夹中
  我们可以将模块安装到任意文件夹,但为了便于管理,建议安装到默认文件夹。
  获取已安装的模块:get-module -listavailable
  获取当前会话中导入的模块:get-module

[*]将模块导入到Windows PowerShell会话
  导入默认模块路径的模块:import-module
  导入非默认路径的模块时要将模块的完整路径输入,例如,导入c:\ps-test文件夹下的TestModules模块:
  Import-module c:\ps-test\TestModules
  将所有模块导入到当前会话中:
  对于Windows7和Windows Server 2k8 R2,只需要右击任务栏上的powershell图标,然后选择"Import all modules"即可
  对于其他版本的Windows操作系统,可以这样导入:get-module -listavailable | import-module
  移除模块:remove-module

[*]找到模块添加的命令
  Get-command -module

[*]使用模块添加的命令
  模块默认路径包括两种,system和current user
  System: $pshome\Modules
  (%windir%\System32\WindowsPowerShell\v1.0\Modules)
  Current user: $home\Documents\WindowsPowerShell\Modules
  (%UserProfile%\Documents\WindowsPowerShell\Modules)
  - or -
  $home\My Documents\WindowsPowerShell\Modules
  (%UserProfile%\My Documents\WindowsPowerShell\Modules)
  查看模块默认路径: $env:psmodulepath
  添加模块默认路径:$env:psmodulepath = $env:psmodulepath + ";"

页: [1]
查看完整版本: PowerShell笔记:powershell和服务器管理器