Saturday, 27 December 2008

Get-Win32Share.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Demonstrates WMI and Win32_Share 
  4. .DESCRIPTION 
  5.     This script looks at objects retured from Get-WMIObject, and [WMICLASS] and demonstrates 
  6.     the use of a static method (create) and a dynamic or object method (delete).  
  7. .NOTES 
  8.     File Name  : Get-Win32Share.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10. .LINK 
  11.     http://www.pshscripts.blogspot.com 
  12. .EXAMPLE 
  13.     Left as an exercise for the reader 
  14. #> 
  15.  
  16. # Display shares at start 
  17. $start = get-wmiobject win32_share | where {$_.name -match "Foo"
  18. if ($start) { 
  19.   "{0} foo shares at start, as follows:" -f $start.count;  
  20.   $start}  
  21. else {"No foo shares"
  22.  
  23. # Create a foo22 share 
  24. "";"Adding Foo22 share" 
  25. $class = [WMICLASS]'win32_share' 
  26. $ret = $class.create("c:\foo", "foo22", 0,$null,"Test Share Creation with WMI"
  27. if ($ret.returnvalue -eq 0){ 
  28. "Foo22 Share created OK"
  29. else
  30. "Share not created, error code: {0}" -f $ret.returnvalue 
  31.  
  32. # Display results 
  33. "";"Foo shares now:" 
  34. get-wmiobject win32_share | where {$_.name -match "foo"
  35. "" 
  36.  
  37. # Delete the foo22 share 
  38. $del = Get-WmiObject win32_share | where {$_.name -eq "foo22"
  39. $ret = $del.delete() 
  40. if ($ret.returnvalue -eq 0){ 
  41. "share deleted OK"
  42. else
  43. "Share not deleted, error code: {0}" -f $ret.returnvalue 
  44.  
  45. # Display final results 
  46. "";"Foo at the end:" 
  47. $finish = get-wmiobject win32_share | where {$_.name -match "foo"
  48. if ($finish) { 
  49.   "{0} foo shares at the end, as folllows:" -f $start.count;  
  50.   $start}  
  51. else {"No foo shares at the end:"
  52. "" 
Technorati Tags: ,,

0 comments: