[Windows PowerShell09]-PowerShell 函数
来源:互联网 发布:美工简历 编辑:程序博客网 时间:2024/06/06 18:47
PowerShell 函数跟普通函数非常类似,同样支持无参数、一个参数、多个参数传入,无返回值和返回一个值的情形。除此之外,PowerShell支持函数默认值,这样可以模仿.net的函数重载的情形。PowerShell函数参数的重载除了使用([Type] [Value1],[Type] [Value2]...[Type] [Valuen])的形式之外,可以使用param([Type] [Value1],[Type] [Value2]...[Type] [Valuen])的形式传递。PowerShell除了可以给函数传递参数之外,还可以给脚本块(脚本文件传递参数),注:PowerShell支持脚本文件的互相调用,在调用的时候支持给脚本文件传递参数。
1)函数调用函数,函数参数传递
#Region Parameter$LogPath = "E:\PowerShellScript\Log\Log.txt"#EndRegion#Region functions# Auth:Justin# Date:2014-10-3# Desc:Get current date with string format yy/mm/dd hh:mm:ssfunction GetCurrentTime(){return (Get-Date).ToString()}# Auth:Justin# Date:2014-10-3# Desc:Write log with format([Time]|[Type]|[Message]) to .txt file.function WriteLog($Msg,$Type){$CurrentDate = GetCurrentTimeif(Test-Path $LogPath){$CurrentDate+"|"+$Type+"|"+$Msg >> $LogPath}else{$CurrentDate+"|"+$Type+"|"+$Msg > $LogPath}}#EndRegion这个例子演示了函数的参数传递形式,函数的调用,写文件的方法(> 和 >>),Region的使用,函数返回值的使用及字符串操作等几个小知识点,虽然函数功能很简单,但麻雀虽小五脏俱全。函数可以使用Param的形式进行传递。注:下面两个函数式解压缩zip文件的
# Auth:Justin# Date:2014-10-3# Desc:To extract zip file to specific locationfunction ExtractZip{param([string]$zipfilename, [string] $destination)if(Test-Path $zipfilename){$shellApplication = new-object -com shell.application$zipPackage = $shellApplication.NameSpace($zipfilename)$destinationFolder = $shellApplication.NameSpace($destination)$destinationFolder.CopyHere($zipPackage.Items())}}# Auth:Justin# Date:2014-10-3# Desc:To add zip file to specific locationfunction AddZip{param([string]$sourcefiles, [string]$zipfilename) dir $sourcefiles | foreach-object { if(!$zipfilename) {$zipfile = $_.FullName + ".zip"; }else {$zipfiles = $zipfilename;} if(!(test-path($zipfile))) {set-content $zipfile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18));(dir $zipfile).IsReadOnly = $false; } $shellApplication = new-object -com shell.application;$zipPackage = $shellApplication.NameSpace($zipfile);$zipPackage.CopyHere(($_.FullName));}}
这两个函数演示了param的使用,调用的时候可以直接传入一个参数,也可以传入两个参数。
ExtractZip "E:\PowerShellScript\Log\Log.zip" "E:\PowerShellScript\Log\"AddZip "E:\PowerShellScript\Log\Log.txt"
PowerShell 函数还支持默认值。
function GetComputerInfo($ComputerName = "localhost"){Get-WmiObject -Class win32_bios -ComputerName $ComputerName}#Call functionGetComputerInfoGetComputerInfo "Justin-pc"
运行结果:
SMBIOSBIOSVersion : HEET42WW (1.23 )Manufacturer : LENOVOName : Phoenix BIOS SC-T v2.2SerialNumber : PF1DY83Version : ACRSYS - 1230SMBIOSBIOSVersion : HEET42WW (1.23 )Manufacturer : LENOVOName : Phoenix BIOS SC-T v2.2SerialNumber : PF1DY83Version : ACRSYS - 1230
PowerShell 支持给脚本文件传值。
TestFileParam.ps1:
param( [string]$UserName)function PrintUserName(){ Write-Host "Hello "$UserName}PrintUserName $UserName
调用过程如下:
这样PowerShell就可以做很多流程处理的事情,工作流由于耦合性太高,通常使用PowerShell来替代工作流。
0 0
- [Windows PowerShell09]-PowerShell 函数
- Windows Powershell中的函数参数
- Windows PowerShell
- Windows powershell
- windows powershell
- windows powershell
- Windows PowerShell 2.0语言之函数和过滤器
- Windows PowerShell 2.0语言之函数和过滤器
- Windows PowerShell 2.0之函数和脚本块共存
- Windows PowerShell 1.0 bug
- Running Windows PowerShell Scripts
- Windows PowerShell 入门指南
- Windows PowerShell™ 用户手册
- 易學易用的 Windows PowerShell
- 学习windows powershell(概览)
- 学习windows powershell(对象)
- Windows Powershell in Action
- Professional Windows PowerShell
- WebCollector爬虫使用内置的Jsoup进行网页抽取
- 用css美化博客内容
- 【Java.Web】Servlet —— 总结
- HTTP Status 404 ,The requested resource (/struts2_test/login) is not available.
- sgu112:a^b - b^a
- [Windows PowerShell09]-PowerShell 函数
- HDU-4438 Hunters 期望 水
- 【Java.Web】JSP —— HTML, Servlet,JSP比较, JSP基础
- Lightoj 1005 Rooks
- HDU 1281 棋盘游戏 二分图的最大匹配
- Django+sqlite使用时出现的一个错误的解决方法
- 对C#中Thread.IsBackground属性的理解
- HDU 1498 50 years, 50 colors 最小点覆盖
- AC自动机(good)codeforces86C