Link to home
Start Free TrialLog in
Avatar of Jean-Jacques CELMA
Jean-Jacques CELMAFlag for France

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

Avatar of footech
footech
Flag of United States of America image

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").
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. 


Avatar of Jean-Jacques CELMA

ASKER

@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


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
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial