PowerShell for Windows Admins

Mar 21 2010   7:22AM GMT

Shares: Creating II



Posted by: Richard Siddaway
PowerShell v2, Shares

Now we have seen how to create a share lets extend the script a bit.  This is how a lot of my scripts get developed.  I need to solve a particular problem so I write a script to do that task.  While I’m writing I try to see how I can make the script generic. Sometimes the script is written immediately to solve the generic problem and other times I have to solve the immediate problem and go back to the generic bit later.

001
002
003
004
005
006
007
008
009
010
011
012
function New-Share {
[CmdletBinding(SupportsShouldProcess=$True)]
param (
    [string]$path,
    [string]$name,
    [int][ValidateRange(0,2)]$type,
    [int]$maxcon,
    [string]$desc
)
    $s = [WmiClass]"Win32_Share" 
        $s.Create($path, $name, $type, $maxcon, $desc)
}

We take our earlier script and create a function. This forms part of my module for working with the file system.

I’m creating it as an advanced function so I can use the –whatif parameters later.  The function takes a set of parameters and creates the share.

Its used like this

New-Share -path c:\share1 -name Test1 -type 0 -maxcon $null -desc "Test share 1 from module"

Next stage is to add the code to test the existence of the folder and to check that the share creates properly

Technorati Tags: ,

Comment on this Post

Leave a comment: