Share off directories on each server

How to do it faster instead of manually share it out.
So on each Windows 2008 Server I need to share out two directories.
C:\folder\share1
C:\folder\share2

Is there a way to do it fast with permissions?
LVL 17
Tiras25Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

becraigCommented:
$servers = (gc serverlist.txt)

foreach ($server in $servers)
{
$Foldername = "C:\folder\share1"
$Sharename1 = "Share1"
$Foldername2 = "C:\folder\share2"
$Sharename2 = "Share2"

IF (!(TEST-PATH $Foldername)) 
{NEW-ITEM $Foldername -type Directory }
If (!(GET-WMIOBJECT Win32_Share -filter "name='$Sharename1'"))
{$Shares=[WMICLASS]"WIN32_Share"
$Shares.Create($Foldername,$Sharename1,0) 
write-host "$sharename1  successfully created on $server" -fore green
}
else 
{write-host "$sharename already share exists on $server" -fore green}
IF (!(TEST-PATH $Foldername2)) 
{NEW-ITEM $Foldername2 -type Directory }
If (!(GET-WMIOBJECT Win32_Share -filter "name='$Sharename2'"))
{$Shares=[WMICLASS]"WIN32_Share"
$Shares.Create($Foldername2,$Sharename2,0) 
write-host "$sharename2  successfully created on $server" -fore green
}
else 
{write-host "$sharename2 already share exists on $server" -fore green}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tiras25Author Commented:
Nice!  Does it set the default permissions?
0
becraigCommented:
It does set default share permissions, if you want to change perms you can always use set-acl.

$Access = "domain\user-or-group"

Also if you want to provide access to a specific Group or user we could always specify and use the Net Share command instead:

e.g.
NET SHARE $sharename1=$Foldername ""/GRANT:$Access,FULL""
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Tiras25Author Commented:
Trying to work with your win32 script. No luck as of yet..
0
becraigCommented:
Are you facing a specific error ?

Can you please post that is you are
0
Tiras25Author Commented:
not yet. Just compiled the script and running .  but nothing happens yet.  No shares are being created.
0
becraigCommented:
Ok so I just tested and this works for me.

Please be sure what you are specifying for $servers = (gc serverlist.txt)

If you are not in the same path as the server list text file then simply provide the full path e.g.  $servers = (gc c:\path-to-text-file\serverlist.txt)
0
Tiras25Author Commented:
its just all these commands are not recognized...  Maybe need to edit environment users or systems variables..

'$servers' is not recognized as an internal or external command,
operable program or batch file.

'foreach' is not recognized as an internal or external command,
operable program or batch file.

'{' is not recognized as an internal or external command,
operable program or batch file.

'$Foldername' is not recognized as an internal or external command,
operable program or batch file.

'$Sharename1' is not recognized as an internal or external command,
operable program or batch file.

'$Foldername2' is not recognized as an internal or external command,
operable program or batch file.

'$Sharename2' is not recognized as an internal or external command,
operable program or batch file.
$Foldername)) was unexpected at this time.
0
becraigCommented:
ahh

You need to save it as a ps1 file then run in a powershell window

e.g . save file as "newshare.ps1"

Then run from a powershell window newshare.ps1
of from a cmd window
powershell c:\pathtofile\newshare.ps1
0
Tiras25Author Commented:
Straight from the powershell and still not recognized.

PS C:\path> powershell
The term 'powershell' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:11
+ powershell <<<<
    + CategoryInfo          : ObjectNotFound: (powershell:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
becraigCommented:
Ok like I said only use powershell if you are running from a cmd window, if you are in a powershell native window just run newshare.ps1
0
Tiras25Author Commented:
Sorry for the delay.  Still struggling with this.  error shows

PS C:\path> .\newshare.ps1
 already share exists on servername
sharename already share exists on servername
0
becraigCommented:
My bad, here is an update:


$servers = (gc serverlist.txt)

foreach ($server in $servers)
{
$Foldername = "C:\folder\share1"
$Sharename1 = "Share1"
$Foldername2 = "C:\folder\share2"
$Sharename2 = "Share2"
Invoke-Command -ComputerName $server -ScriptBlock {
$server = hostname
Param($Foldername,$Sharename1,$Foldername2,$Sharename2)
IF (!(TEST-PATH $Foldername)) 
{NEW-ITEM $Foldername -type Directory }
If (!(GET-WMIOBJECT Win32_Share -filter "name='$Sharename1'"))
{$Shares=[WMICLASS]"WIN32_Share"
$Shares.Create($Foldername,$Sharename1,0) 
write-host "$sharename1  successfully created on $server" -fore green
}
else 
{write-host "$sharename already share exists on $server" -fore green}
IF (!(TEST-PATH $Foldername2)) 
{NEW-ITEM $Foldername2 -type Directory }
If (!(GET-WMIOBJECT Win32_Share -filter "name='$Sharename2'"))
{$Shares=[WMICLASS]"WIN32_Share"
$Shares.Create($Foldername2,$Sharename2,0) 
write-host "$sharename2  successfully created on $server" -fore green
}
else 
{write-host "$sharename2 already share exists on $server" -fore green}

#End Invoke
} -ArgumentList $Foldername,$Sharename1,$Foldername2,$Sharename2

}

Open in new window

0
Tiras25Author Commented:
Now cant even connect.

PS C:\path> .\newshare.ps1
[servername] Connecting to remote server failed with the following error message : WinRM cannot process the request. Th
e following error occured while using Kerberos authentication: The network path was not found.
 Possible causes are:
  -The user name or password specified are invalid.
  -Kerberos is used when no authentication method and no user name are specified.
  -Kerberos accepts domain user names, but not local user names.
  -The Service Principal Name (SPN) for the remote computer name and port does not exist.
  -The client and remote computers are in different domains and there is no trust between the two domains.
 After checking for the above issues, try the following:
  -Check the Event Viewer for events related to authentication.
  -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or us
e HTTPS transport.
 Note that computers in the TrustedHosts list might not be authenticated.
   -For more information about WinRM configuration, run the following command: winrm help config. For more information,
 see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken
0
becraigCommented:
Ok so it seems you do not have winrm enabled, let me change to do it remotely.


Can you do a quick test for me


gc serverlist.txt | % { test-wsman -computername  $_}

This will help us to figure out if winrm is enabled and running, if it is not then we will have to use another approach.
0
Tiras25Author Commented:
PS C:\path> gc serverlist.txt | % { test-wsman -computername  $_}
Test-WSMan : The client cannot connect to the destination specified in the request. Verify that the service on the dest
ination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running
on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on t
he destination to analyze and configure the WinRM service: "winrm quickconfig".
At line:1 char:35
+ gc serverlist.txt | % { test-wsman <<<<  -computername  $_}
    + CategoryInfo          : InvalidOperation: (serverhostname:String) [Test-WSMan], InvalidOperationException
    + FullyQualifiedErrorId : WsManError,Microsoft.WSMan.Management.TestWSManCommand
0
becraigCommented:
Here is one not leveraging wmi.

Notes:
uncFoldername should be the Admin($ share) of the drive to enable the mkdir to work

This has not yet been tested so let me know if you face errors and we can cleanup / tweak.

$servers = (gc serverlist.txt)

$Foldername = "C:\folder\share1"
$UNCFoldername = "C$\folder\share1"
$Sharename1 = "Share1"
$uncFoldername2 = "C$\folder\share2"
$Foldername2 = "C:\folder\share2"
$Sharename2 = "Share2"
$Access = "domain\user-or-group-to-have-access"

foreach ($server in $servers)
{
if (Test-Path \\$server\$sharename1)
{Write-Host "$sharepath1 already exists ... Exiting..."}
else
{
mkdir \\$server\$uncFoldername
psexec \\$server cmd /c "NET SHARE $sharename1=$Foldername ""/GRANT:$Access,FULL"""
}
if (Test-Path \\$server\$sharename2
{Write-Host "$sharename2 already exists ... Exiting..."}
else
{
mkdir \\$server\$uncFoldername
psexec \\$server cmd /c "NET SHARE $sharename2=$Foldername2 ""/GRANT:$Access,FULL"""
}
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.