Avatar of celmajj
celmajj asked on

Powershell : New-SMBSHARE does not create the shared folder.

Dear All,

I have wrote a little powershell script

$useraccess ="user05"
$destination = "\\server01.contoso.lan\j$\groups\testroot\"
$UserFolders = "Scan_user05"                               New-SmbShare -Name $userfolders -Path $destination -FullAccess $useraccess

Open in new window

But it is giving my that error message 

New-SmbShare : The filename, directory name, or volume label syntax is incorrect. 
At line:7 char:9
+         New-SmbShare -Name $userfolders -Path $destination -FullAcces ... +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : NotSpecified: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBSha     re) [New-SmbShare], CimException     + FullyQualifiedErrorId : Windows System Error 123,New-SmbShare

Open in new window


I can't figure out how to fix it. I don't see where the issue is.


Do you have an idea?

I ca

PowershellWindows OS

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
footech

You have to specify a local path (not a UNC path).  So it has to be run on "server01".
Is "user05" a local user or domain user?  I'm pretty sure for a domain user you have to prepend the domain name (like "contoso\user05").
Robert

Sharing a folder does not create the folder.

So if you want to do this via script you would need to first create the folder, set the permissions on the folder, then create the share and set the permissions on the share.

so for example:
$destination = "J:\groups\testroot\"
New-Item $destination –type directory 


$useraccess ="domain\user05"
$shareName = "Scan_User05"

$Acl = Get-Acl $destination

$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($useraccess,"FullControl","Allow")
$acl.SetAccessRule($accessRule)
$acl | Set-Acl $destination
                    
        New-SmbShare -Name $shareName -Path $destination -FullAccess $useraccess

Open in new window


NOTE in this example I only set permissions for the user not for admins etc. 


ASKER
celmajj

@footech,
So there is no way to run that script from a workstation?
user05 is an example. I know what to do if the user is a domain computer.

@Robert,
I know that I have to create the folder first and also set the permissions.

My concern was on the error message as I would like to run the script from a workstation that is part of a domain.

Thks yo all


Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
footech

Not exactly.  The New-SMBShare command has to be run on the server where the directory to be shared is located, but how you run it there is up to you.  You can use PS Remoting to establish a remote session to the server, which you can then use to run the command.
ASKER CERTIFIED SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question