Solved

net share powershell script

Posted on 2014-01-01
4
1,215 Views
Last Modified: 2014-01-05
Hi I need some help getting a script working, So far the script takes column 2 (location of folder) and column 3 (username) from a .csv file, creates the folder and sets permissions. I also need it to share the folder and set share permissions but after a few failed attempts I admit I can't seem to get the syntax right. The share also needs to be hidden so I need it to add a $ sign at the end of the share name. Line 6 (marked by##) of the script below is all that needs tweaking

$scriptblock = {
    param ($arg2, $arg3)
    new-item -itemtype directory -path $arg2
    #set permissions with $arg3
    icacls $arg2 /grant "$($arg3):(OI)(CI)F"
    ##"net share ",$_,"$","=",($arg2).Path,"\",$_," ""/GRANT:Everyone,FULL"" ""/Unlimited"""
}
$s = New-PsSession -computername server1
Import-Csv "C:\test\test.csv" | ForEach-Object {
    Invoke-Command -session $s -ScriptBlock $scriptblock -ArgumentList $_."second", $_."third"
}
0
Comment
Question by:Dead_Eyes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 350 total points
ID: 39750045
You really should name your parameters in a more appealing way...
$scriptblock = {
    param ($folder, $user)
    new-item -itemtype directory -path $folder
    #set permissions with $user
    icacls $folder /grant "$($user):(OI)(CI)F"
    net share "$(Split-Path $folder -leaf)`$=$folder" '/grant:Everyone,Full' '/unlimited'
}
$s = New-PsSession -computername server1
Import-Csv "C:\test\test.csv" | ForEach-Object {
    Invoke-Command -session $s -ScriptBlock $scriptblock -ArgumentList $_."second", $_."third"
}

Open in new window

0
 

Author Comment

by:Dead_Eyes
ID: 39750235
Thanks that's great, I am quite new to the param command so naming them arg2 & arg3 just helped me separate any naming association in my head and better understand how it works but thanks for advice when I get my head fully round it I will start naming them better.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question