【转】PowerShell 函数(Function)

 转至:http://blog.csdn.net/kk185800961/article/details/49022395

函数基本操作:

 [plain] view plain copy

  1. #创建函数  
  2. Function GetSQLService  
  3. {  
  4. Get-Service -DisplayName "*SQL*"  
  5. }  
  6.   
  7. #调用函数,直接写函数名  
  8. GetSQLService   
  9.   
  10.   
  11. #查看函数定义  
  12. $Function:GetSQLService  
  13.   
  14.   
  15. #导出函数定义到文本  
  16. $Function:GetSQLService | Out-File E:GetSQLService.ps1  
  17.   
  18.   
  19. #删除函数  
  20. del Function:GetSQLService  
  21.   
  22. #查看内部自定义函数:  
  23. dir function: | ft -AutoSize  

创建带参数的函数,三种方法(会覆盖同名函数):

 [plain] view plain copy

  1. #创建带参数的函数,三种方法(会覆盖同名函数)  
  2. Function GetSQLService($ServiceName)  
  3. {  
  4. Get-Service -DisplayName "*$ServiceName*"  
  5. }  
  6.   
  7.   
  8. Function GetSQLService  
  9. {  
  10. param($ServiceName)  
  11. Get-Service -DisplayName "*$ServiceName*"  
  12. }  
  13.   
  14.   
  15. #有默认值的参数  
  16. Function GetSQLService  
  17. {  
  18. param($ServiceName='SQL')  
  19. Get-Service -DisplayName "*$ServiceName*"  
  20. }  
  21.   
  22.   
  23. #多个参数  
  24. Function GetSQLService  
  25. {  
  26. param($ServiceName,$KeyWord)  
  27. Get-Service -DisplayName "*$ServiceName*$KeyWord*" | Format-Table -AutoSize  
  28. }  


调用函数: 

[plain] view plain copy
  1. GetSQLService  
  2. GetSQLService SQL  
  3. GetSQLService -ServiceName SQL  
  4. GetSQLService -KeyWord MSSQLSERVER  
  5. GetSQLService -ServiceName SQL -KeyWord MSSQLSERVER  

       

万能参数,无需声明参数,直接使用内部参数:

[plain] view plain copy
  1. Function GetSQLService  
  2. {  
  3. Get-Service -DisplayName "*$args*"  
  4. }  

      
[plain] view plain copy

  1. #这里用其他参数名,调用时函数将无法识别参数,不可这样使用。只能用$args  
  2. Function GetSQLService  
  3. {  
  4. Get-Service -DisplayName "*$ServiceName*"  
  5. }  

求和函数示例:

[plain] view plain copy 
  1. #求和函数,调用函数时参数 $args 可以同时输入多个值。  
  2. Function Add  
  3. {  
  4. $sum=0  
  5. $args | foreach {$sum=$sum+$_}  
  6. $sum  
  7. }  
  8.   
  9.   
  10. Add 1 2 3 4 5 6 7 8 9 10  

     

返回多值的函数,也可使用 return 返回,使用 return 后函数将结束返回,后面语句不会执行

[plain] view plain copy
  1. Function Test { "Zero", "One", "Two", "Three" }  
  2.   
  3.   
  4. Function Test   
  5. {  
  6. "Zero"  
  7. "One"  
  8. "Two"  
  9. "Three"   
  10. }  
  11.   
  12.   
  13. Function Test { "Zero" "One" "Two" "Three" } #这样写错误!  
  14.   
  15.   
  16.   
  17. Function Test   
  18. {  
  19. "Zero"  
  20. "One"  
  21. return "Two"  
  22. "Three"   
  23. }  

    

输出注释,不会作为结果:

[plain] view plain copy
  1. Function Test   
  2. {  
  3. Write-Host "此处为注释不作为结果,但同样会输出"  
  4. "Zero"  
  5. "One"  
  6. return "Two"  
  7. "Three"   
  8. }  
  9.   
  10.   
  11.   
  12. Test  
  13. $Test = Test  

     

隐藏函数内部的错误:

[plain] view plain copy
  1. #函数内部有错误,调用时也返回错误!  
  2. Function Test { Stop-Process -Name "kk123456" }  
  3.   
  4.   
  5. #使用参数 $ErrorActionPreference 可隐藏错误  
  6. Function Test   
  7. {   
  8. $ErrorActionPreference="SilentlyContinue"  
  9. Stop-Process -Name "kk123456"   
  10. $ErrorActionPreference="Continue"  
  11. }  

     

管道应用:函数内部处理上一个的结果集: 

[plain] view plain copy
  1. Function Test { $input }  
  2.   
  3. 2,0,1,2 | Test  
  4.   
  5.   
  6.   
  7. Function Test  
  8. {   
  9. Foreach ($element in $input)  
  10. {  
  11. "value: "+$element  
  12. }  
  13. }  
  14.   
  15. 2,0,1,2 | Test  

      

使用过滤器 Filter(特殊函数)的流模式处理管道数据,避免结果集太大占用过多内存或进程等待太久

 [plain] view plain copy

  1. Filter Test  
  2. {   
  3. Foreach ($element in $input)  
  4. {  
  5. "value: "+$element  
  6. }  
  7. }  
  8.   
  9. 2,0,1,2 | Test  

     

使用Filter时, $input 每次传递一个元素,不再是整个结果集,因此不必要了,用变量 $_ 替代

[plain] view plain copy
  1. Filter Test{ $_.name }  
  2.   
  3. Get-Service -DisplayName "*MSSQLSERVER*" | Test  

      

函数在内部 process 也可以处理多结果集:

[plain] view plain copy
  1. Function Test{    
  2. begin{ $i=1 }    
  3.    
  4. process{    
  5.      $_.name  
  6.      $i++    
  7. }    
  8.    
  9. end{}    
  10. }    
  11.   
  12. Get-Service -DisplayName "*MSSQLSERVER*" | Test  

     

原文地址:https://www.cnblogs.com/keepSmile/p/5821358.html