Saturday, 27 December 2008


Demonstrates WMI and Win32_Share
This script looks at objects returned from Get-WMIObject, and
    [WMICLASS] and demonstrates the use of a static method (create)
    and a dynamic or object method (delete). 
File Name: get-win32share.ps1
Author : Thomas Lee -
Left as an exercise for the reader

# Display shares at start
$start = get-wmiobject win32_share | where {$ -match "Foo"}
if ($start) {
"{0} foo shares at start, as follows:" -f $start.count;
else {"No foo shares"}

# Create a foo22 share
"";"Adding Foo22 share"
$class = [WMICLASS]'win32_share'
$ret = $class.create("c:\foo", "foo22", 0,$null,"Test Share Creation with WMI")
if ($ret.returnvalue -eq 0){
"Foo22 Share created OK"}
else {
"Share not created, error code: {0}" -f $ret.returnvalue

# Display results
"";"Foo shares now:"
get-wmiobject win32_share | where {$ -match "foo"}

# Delete the foo22 share
$del = Get-WmiObject win32_share | where {$ -eq "foo22"}
$ret = $del.delete()
if ($ret.returnvalue -eq 0){
"share deleted OK"}
else {
"Share not deleted, error code: {0}" -f $ret.returnvalue

# Display final results
"";"Foo at the end:"
$finish = get-wmiobject win32_share | where {$ -match "foo"}
if ($finish) {
"{0} foo shares at the end, as folllows:" -f $start.count;
else {"No foo shares at the end:"}

No comments: