Solved

net share powershell script

Posted on 2014-01-01
4
1,191 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 69

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

756 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