Powershell可以很容易地把自己的脚本函数做成模块以便重复使用。豆子写了个简单的函数,生成帮助文档,然后另存为ps1脚本和psm1的模块,这样就可以反复调用了。
下面是一个基本流程。
首先写了一个函数,基本功能是输入操作系统名字,获取系统里面所有匹配系统的.net 版本,powershell 版本和OS的版本
function get-dotnet { [cmdletbinding()] param( [parameter(mandatory=$true,position=1)][string]$osname ) $a=Get-ADComputer -Filter "operatingsystem -like '*$osname*'" -Properties operatingsystem,ipv4address| Where-Object{$_.ipv4address -ne $null} | select -ExpandProperty name Invoke-Command -ComputerName $a { ""| select @{name=".Net version";expression={ Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -name Version -EA 0 | Where-Object { $_.PSChildName -match '^(?!S)\p{L}'} | Sort-Object version -Descending | Select-Object -ExpandProperty Version -First 1 } },@{name="PowerShell Version";e={$PSVersionTable.psversion.major}},@{name="OS Version";expression={[environment]::osversion.version.build}} } | ft }
结果如下
PS C:\WINDOWS\system32> get-dotnet -osname 2012 .Net version PowerShell Version OS Version PSComputerName RunspaceId ------------ ------------------ ---------- -------------- ---------- 4.5.51650 4 9600 SYDAV01 8b5bfa95-b012-47a5-b78e-8c82d7e120ff 4.5.51650 4 9600 SYDWSUS 08579f5c-2186-4a05-a242-55a2defea0d2 4.5.51650 4 9600 AUSSQL01 54b0b2c0-7ae8-4587-a774-d76bea7ba10e 4.5.51650 4 9600 SYDHARD01 52a283ed-107b-4cfb-ac94-7d183b5e37fb 4.5.51650 4 9600 SYDSRM2012 4d25f13e-8dd0-49d6-b83e-e2c0ba2bfb69 4.5.51650 4 9600 SYDVCS2012 28aabf1a-3165-4e78-b438-0b926df1a1a8 4.5.51650 4 9600 AUSHELP01 0132ddd2-1586-4ebf-b80e-95b1564dc6ee 4.5.51650 4 9600 DRVCS2012 cbb49686-d565-4afc-935a-1436462b16b7 4.5.51650 4 9600 DRRAD2012 74ec057f-bf1d-4728-b3be-1ee9b
如果要给别人使用的话,必须添加说明,方法很简单,ISE打开一个新的窗口,输入ctrl+J, 然后就可以打开模板了
在function的开头,依葫芦画瓢输入下面的信息,这样的话,Powershell 会自动的生成对应的帮助文档
function get-dotnet { <# .Synopsis Get .net and Powershell Version from remtoe machine .DESCRIPTION Get .net and Powershell and OS Version from remtoe machine .EXAMPLE get-dotnet -osname 2012 This will get the .net, powershell and OS version of connected Windows 2012 Server .EXAMPLE get-dotnet -osname "2012 R2" This will get the .net, powershell and OS version of connected Windows 2012 R2 Server .INPUTS String name of OS, such as winows 7, 2008, 2012 etc .OUTPUTS Output from this cmdlet (if any) .NOTES This is a test function .COMPONENT The component this cmdlet belongs to Yuan Li .ROLE The role this cmdlet belongs to Yuan Li .FUNCTIONALITY The functionality that best describes this cmdlet #> [cmdletbinding()]
Powershell自动的生成了对应的帮助文档,很赞啊
PS C:\WINDOWS\system32> help get-dotnet -full NAME get-dotnet SYNOPSIS Get .net and Powershell Version from remtoe machine SYNTAX get-dotnet [-osname] <String> [<CommonParameters>] DESCRIPTION Get .net and Powershell and OS Version from remtoe machine PARAMETERS -osname <String> Required? true Position? 2 Default value Accept pipeline input? false Accept wildcard characters? false <CommonParameters> This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). INPUTS String name of OS, such as winows 7, 2008, 2012 etc OUTPUTS Output from this cmdlet (if any) NOTES This is a test function -------------------------- EXAMPLE 1 -------------------------- PS C:\>get-dotnet -osname 2012 This will get the .net, powershell and OS version of connected Windows 2012 Server
保存Powershell脚本的时候,默认的后缀是ps1。比如我另存为 get-dotnet.ps1,如果我试图调用其中的函数,我需要使用 dot source的格式,比如
PS C:\scripts> . .\get-dotnet.ps1
如果我另存为psm1的格式,那么我可以把他视作一个模块,直接导入
PS C:\scripts> PS C:\scripts> Import-Module .\get-dotnet.psm1 PS C:\scripts> get-module ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Script 0.0 get-dotnet get-dotnet Script 1.0.0.0 ISE {Get-IseSnippet, Import-IseSnippet, New-IseSnippet} Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...} Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...} Script 1.0 tmp_otuyg3x2.xlk {Add-ADComputerServiceAccount, Add-ADDomainControllerPasswordReplicationPolicy, Add-ADFineGrainedPasswordPolicy...
为了更省事,我可以直接搜索模块的默认路径,把我自定义的模块保存进去,那么每次打开ISE就会自动执行了。
比如查看当前的路径,豆子的版本是powershell5
PS C:\scripts> $env:PSModulePath C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\;C:\Prog ram Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\ PS C:\scripts> $env:PSModulePath.Split(";") C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules C:\Program Files\WindowsPowerShell\Modules C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\ C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\
我直接对应的一个目录下创建一个同名的文件夹,把psm1文件放进去
重启ISE,发现模块已经自动加载了