Hello,

I’m trying to write my first PowerShell script. I hope you can help me.

I receive several emails every day to create a folder, group and add permissions. I would like to make my work easier

For example:

20111-New-Project

  1. I need to create a folder on the server with the same name

  2. In AD I have to create a group - 20111_RW

  3. Depends who sent me this email, I need to add different members to this group (North, South, East or West)

  4. Add this group to the folder with modification permissions

I have a problem with the point 3

I would like to define groups as numbers but i don’t know is it possible.

I don’t even know how to ask Google about it …

  1. North_RW and North_RO,

2.South_RW and South_RO,

3.East_RW and East_RO,

4.West

I want PS to ask me what members to add when creating the group.

Any suggestions how to do this?

param(
    [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $Client
    )

$NoProject = $Client.substring(0,5)

$Domain = "domain.local"
$ADPath = "OU=Test0,OU=Groups,DC=domain,DC=local"
$ClientPath = "\\Server\TEST\$Client"

$GroupParams= @{
    'Name' = "${NoProject}_RW" 
    'SamAccountName' = "$NoProject" 
    'GroupCategory' = "Security"
    'GroupScope' = "Global"
    'DisplayName' = "$Client Read-Write Access"
    'Path' = "OU=Test0,OU=Groups,DC=domain,DC=local"
    'Description' = "Members of this group have read-write access to the test share"
}

New-ADGroup @GroupParams

# Check if folder exist, if not create new Client folder
If(!(test-path $ClientPath))
    {
        New-Item -Path $ClientPath -ItemType Directory

# Get permissions
        $acl = Get-Acl -Path $ClientPath

# Get Security Groups
        get-adobject -searchbase $ADPath -ldapfilter {(objectclass=group)}

# set new permissions
        $acl | Set-Acl -Path $ClientPath
    }
6 Spice ups

Welcome

If you post code, please use the ‘Insert Code’ button. Please and thank you!

192033ab-bb8f-4032-88a5-8e2313af0344-codebutton_small.png

you can either make a member parameter and make it mandatory so it’ll propmt there, or you can just use ‘read-host’ ?

Could you please send me some examples/links ?
I don’t see a member parameter anywhere as I have described.

You have to create that…

function start-addstuff {

param(
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]$Client,
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string[]]$members
)

Write-Output "Members to add: $members"

}

start-addstuff
cmdlet start-addstuff at command pipeline position 1
Supply values for the following parameters:
Client: ClientABC
members[0]: mike
members[1]: bob
members[2]: steve
members[3]: linda
members[4]: garry
members[5]: joe
members[6]: 
Members to add: mike bob steve linda garry joe

To the OP:

If I understand you correctly, Your #3 is analogous to a lookup table – if 1, add these groups, if 2, add these, if 3, add those, ad nauseum.

If that’s correct, create a CSV file with the number in column 1 and the various groups in column 2 separated by a semi-colon (for example).

Save the CSV where it can be managed by you and whoever helps you manage this process.
Each time you run your script, you get the data via import-csv.
The data can then be loaded into a hash, and you can use the key (the number column) to retrieve the values (the list of groups) OR you can just use the data out of the CSV.

Try this and see if you can teach yourself how to use a hash. If you get stuck, post the code related to making and using it.

1 Spice up