PowerShell for Windows Admins

Mar 30 2010   2:34PM GMT

Shares: Creating III

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In a previous post

http://itknowledgeexchange.techtarget.com/powershell/shares-creating-ii/

we looked at creating a file share. This function can be expanded to test for the existence of the folder and if we succeed or not.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
function New-Share {
[CmdletBinding(SupportsShouldProcess=$True)]
param (
    [string]$path,
    [string]$name,
    [int][ValidateRange(0,2)]$type,
    [int]$maxcon,
    [string]$desc
)

$fail = DATA {
ConvertFrom-StringData -StringData @’
0 = Success
2 = Access Denied
8 = Unknown Failure
9 = Invalid Name
10 = Invalid Level
21 = Invalid Parameter
22 = Duplicate Share
23 = Redirected Path
24 = Unknown Device or Directory
25 = Net Name Not Found
‘@

}

    if (!(Test-Path -Path $path)){Throw "Folder does not exist"}
    $s = [WmiClass]"Win32_Share" 
    $ret = $s.Create($path, $name, $type, $maxcon, $desc)
    if ($ret.ReturnValue -ne 0){
        Write-Host "Share $name was not created"
        Write-Host "Failure reason: $($fail["$($ret.ReturnValue)"])"
    }
    else {Write-Host "Share $name was created"}
}

We add a hash table of the return codes from the create method. A simple Test-Path enables us to determine if the share exists.

We can test the return code and if the value is zero state the creation succeeded otherwise we report failure and the reason.

PS> New-Share -path c:\share1 -name Test1 -type 0 -maxcon $null -desc "Test share 1 from module"
Share Test1 was not created
Failure reason: Duplicate Share

 

Technorati Tags: ,

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: