Scripted Directory / Share / Permissions Build
“Context -
This was built to assist with preparing for the Ivanti source (consisting of packages / patches / patches / images) on each of our 70 global servers.”
posted Jun 15, 2018, 9:43 AM
Here recently I determined that I would have to build directories, shares and set permissions on up to 70 different servers. This screamed script. I wanted to share what I have created and with a little modification u can tailor it to suit your needs. Script is tuned for Server 2008 / 2012. Below is a description:
Right click on the Powershell Script (Bottom of page / Requires Modification) and select:
Prompt for Credentials (Useful for cross domain)
Input your Server Name (Can be modified to do a list from a TXT file)
Input the Drive Letter (In this example the folder structure is built on the root of the drive and can be modified to be nested)
Input the User Account u want to assign Permissions (Permissions can be adjusted based on Icacls Parameters - https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/icacls). In this example I am assigning modify.
End Result (May flash by quickly upon completion)
So there are several additions I can do to this to make it more universally applicable such as prompting for the Directory Structure, Share name, OS, and even Permissions....also I can build in some error control / validation... but I will save that for 2.0 or leave it up to you. This will get u started if u find yourself in need. Script is below however u need to remember to Test Test Test in order to be 100% certain of the outcome.
**************************************
#Cedentials,Computer Prompt,Drive Letter
$Credential = $Host.ui.PromptForCredential("Credentials to Access Server", "Please enter your USERNAME and PASSWORD for the DOMAIN of the SERVER you would like to connect to.", "", "NetBiosUserName")$CompName=Read-Host -Prompt "Enter the name of the server"
#Execution
Invoke-Command -ComputerName $CompName -Credential $Credential -ScriptBlock{$DrvLtr=Read-Host -prompt "Enter the drive letter with colon (i.e. E:) of the server that will be used for storing content"$ServiceAcct=Read-Host -Prompt "Enter the name of the service account to use <Domain>_Iva_Content"$OS=gwmi win32_operatingsystem | select captionIf ($OS -like "*windows server 2008*")
{
#Drivers
New-Item $DrvLtr\Ivanti\Source\Drivers –type directory -Forcenet share "Drivers$=$DrvLtr\Ivanti\Source\Drivers" "/grant:everyone,FULL"icacls.exe "$DrvLtr\Ivanti\Source\Drivers" /grant "${ServiceAcct}:(OI)(CI)M" /T
#Images
New-Item $DrvLtr\Ivanti\Source\Images –type directorynet share "Images$=$DrvLtr\Ivanti\Source\Images" "/grant:everyone,FULL"icacls.exe "$DrvLtr\Ivanti\Source\Images" /grant "${ServiceAcct}:(OI)(CI)M" /T
#Packages
New-Item $DrvLtr\Ivanti\Source\Packages –type directorynet share "Packages$=$DrvLtr\Ivanti\Source\Packages" "/grant:everyone,FULL"icacls.exe "$DrvLtr\Ivanti\Source\Packages" /grant "${ServiceAcct}:(OI)(CI)M" /T
#Patches
New-Item $DrvLtr\Ivanti\Source\Patches –type directorynet share "Patch=$DrvLtr\Ivanti\Source\Patches" "/grant:everyone,FULL"icacls.exe "$DrvLtr\Ivanti\Source\Patches" /grant "${ServiceAcct}:(OI)(CI)M" /T}
If ($OS -like "*windows server 2012*"){
#Drivers
New-Item $DrvLtr\Ivanti\Source\Drivers –type directory -ForceNew-SMBShare –Name “Drivers$” –Path “$DrvLtr\Ivanti\Source\Drivers” -FullAccess everyone icacls.exe "$DrvLtr\Ivanti\Source\Drivers" /grant "${ServiceAcct}:(OI)(CI)M" /T
#Images
New-Item $DrvLtr\Ivanti\Source\Images –type directoryNew-SMBShare –Name “Images$” –Path “$DrvLtr\Ivanti\Source\Images” -FullAccess everyone icacls.exe "$DrvLtr\Ivanti\Source\Images" /grant "${ServiceAcct}:(OI)(CI)M" /T
#Packages
New-Item $DrvLtr\Ivanti\Source\Packages –type directoryNew-SMBShare –Name “Packages$” –Path “$DrvLtr\Ivanti\Source\Packages” -FullAccess everyone icacls.exe "$DrvLtr\Ivanti\Source\Packages" /grant "${ServiceAcct}:(OI)(CI)M" /T
#Patches
New-Item $DrvLtr\Ivanti\Source\Patches –type directoryNew-SMBShare –Name “Patch” –Path “$DrvLtr\Ivanti\Source\Patches” -FullAccess everyone icacls.exe "$DrvLtr\Ivanti\Source\Patches" /grant "${ServiceAcct}:(OI)(CI)M" /T}}
**************************************