create shared folder

Hello readers

I i’m working on a new function that will create a new folder and make it a share @ the same time.
Now i’m having a small problem when i try to create a share by WMI i get a return message what says "

21 Error 21: Invalid Parameter

i hope that some of you cant see what my problem is.
i think its about converting the 2 varible’s into 1 what gives me the problem.

BTW its my first code so if you have some tips as well feel free :slight_smile:

Function new-sharedfolder{
            [Parameter( Mandatory=$True,

            [parameter( Mandatory=$true)]
            [Parameter( Mandatory=$True)]

            [parameter (mandatory=$true)]

            [string[]]$computername = "localhost"


        foreach($folder in $folders){
            $folderstatus = $True
            Write-Verbose "Creating folder $folder "
            Try {
                new-item $drive":\"$folder -ItemType directory -ErrorAction stop | Out-Null
            catch {                Write-Warning "Folder $sharepath1 already excist"             $folderstatus = $False
            if ($folderstatus) {
                foreach($user in $Users){
                Write-Verbose "updating ACL for $user"
                    $acl = Get-Acl $drive":\"$folder
                    $acl.SetAccessRuleProtection($True, $False)
                    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Administrators","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
                    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$User","$permission", "ContainerInherit, ObjectInherit", "None", "Allow")
                    Set-Acl $drive":\"$folder $acl
                Write-Verbose "Creating Share $folder "
                $Share = [WMICLASS] "\\$computername\Root\Cimv2:Win32_Share"
            $result = $Share.Create("$sharepath, $folder, 0")

	            switch ($result.ReturnValue)
		            0 {$text += "has been success fully created" }
		            2 {$text += "Error 2: Access Denied" }
		            8 {$text += "Error 8: Unknown Failure" }
		            9 {$text += "Error 9: Invalid Name"}
		            10 {$text += "Error 10: Invalid Level" }
		            21 {$text += "Error 21: Invalid Parameter" }
		            22 {$text += "Error 22 : Duplicate Share"}
		            23 {$text += "Error 23: Redirected Path" }
		            24 {$text += "Error 24: Unknown Device or Directory" }
		            25 {$text += "Error 25: Net Name Not Found" }
	            #Create Custom return object and Add results
	            $return = New-Object System.Object
	            $return | Add-Member -type NoteProperty -name ReturnCode -value $result.ReturnValue
	            $return | Add-Member -type NoteProperty -name Message -value $text	
	            #Return result object

                Write-Verbose "reading folder information"
                 Get-Acl $drive":\"$folder  | Format-List

new-sharedfolder -folder "labs2" -drive e -users "First1.Last1","first1.last2" -permission ReadAndExecute -Verbose

You need to remove the quotes from your parameters in $share.Create.

i.e. This:

$Share.Create(“$sharepath, $folder, 0″)

should be written like this:

$Share.Create($sharepath, $folder, 0)

Oh, and if this is really your first script, you’re off to an absolutely fantastic start. :slight_smile:

thanks for the feedback that was the solution for my problem
i was afraid that the problem was this part of code

New-Variable sharepath -Value $drive":\"$folder

because of the mixture of 2 variables and flat text into one variable

[quote=8289]Oh, and if this is really your first script, you’re off to an absolutely fantastic start.
Thanks with guideline created by Don Jones in his collection of CBT Nuggets gives me a good start.
and my past with writing PHP and HTML websites from scratch are also working ofcourse :slight_smile: