Solved

net share powershell script

Posted on 2014-01-01
4
1,088 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
4 Comments
 
LVL 68

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article will help you understand what HashTables are and how to use them in PowerShell.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now