Monday, 14 August 2017

Creating Multiple Groups using DSADD command- It works for Groups with spaces in it

Hello Guys,


I had requirement to create multiple groups in domain. I thought it was pretty straight forward so just used my old Script to do the job for me.

My script input file named: groups.txt ( Where you see the group name as space in it )



My Old Scripts contains following "DSADD" code with "FOR" loop



When I executed the script I got the following error popped out in the CLI window.
If you notice that the error was due to the space in the group name. The for loop script by default took the space delimited and try to create the Group named: "TestingGroupName" but it would have got succeeded first time if "TestingGroupName" does not exist. Second time onwards it will throw the below error that already a group exist.


 So, I have to manually specify the delimiter by comma instead of space in the script

New Script with delimiter by comma:  




Now with the new script, I am able to create the group name with space in it.



Hope, this might help you as well.. 

Simple Batch Script to Share and setup Share permission for Multiple Folders

Hello Everyone,

We have recently received request to share and set share permission for about 500 users home directories.

Initially, we did not thought to script it and start working to share them manually :( .  I know it was bad idea to share all the folders and set share permission manually.

Later, we thought avoid this manual work again , just in case, if we get the similar request in future. So, I have started to work on the batch scripting with "NET SHARE"  command.

Overview of Script as snapshot :





Script details :

1) Prepare “ShareFolderList.txt” input file in the c:\temp directory. Sample file is attached for reference 
Sample file : 


2) Create new File “FolderSharing.bat” in the c:\temp directory, copy the content below and save it as (.bat) file


REM ******** Start of Script *******

REM "To Share the list of folders in the input file"
REM  This script is created to share the adagility.net cluster P:Homedirectories

REM Author: Murugan Natarajan / http://techmurugan.blogspot.in/
REM Created on: 24/7/2017
REM ------------------------------------------------------------------------------------------

REM Error checking for the input file for looping


if not exist "c:\temp\ShareFolderList.txt" GOTO Error

REM if input file exist then process the command to share the folders and set share permission

If exist "c:\temp\ShareFolderList.txt" GOTO LineExecute1

:LineExecute1
 
for /F %%a in (c:\temp\ShareFolderlist.txt) do NET SHARE "%%a$"="D:\HomeDIRs\%%a"  /Grant:Everyone,Change /Grant:Administrators,Full

goto END

:Error
Echo "There is no ShareFolderList.txt file available at c:\temp path"

:END

REM   *****  END of Script ******


Note: I have add error line , just to make sure that input file is created before running the batch file. If there is no c:\temp\ShareFolderList.txt file in the path, then script will END and it will not process the "NET SHARE" command.

3) Open command prompt “Runas Administrator – if needed ”
4) In the c:\temp directory, Type “FolderSharing.bat” (without quotes) and press enter
5) Now, script runs in the command prompt with processing results in the console.

Note: For testing purpose, I suggest to try with only two folders and check the sharing and share permission. If all working without any issues, we can add more folder names to the “ShareFolderlist.txt” input file and excute the FolderSharing.bat file again.

To verify the share is created or not , we can use the command “Net share” in the command prompt window.

 

Creating Multiple Groups using DSADD command- It works for Groups with spaces in it

Hello ,

I had requirement to create multiple groups in domain. I thought it's pretty straight forward so just used my old Script to do the job for me.

1) My script input file named: groups.txt ( Where you see the group name has space in it )

                                                                 

2) My Old Scripts contains following "DSADD" code using "FOR" loop method


"for /f %%i in (groups.txt) do dsadd group cn="%%i",OU=test,DC=test,DC=pr -scope l"



3) When I executed the script I got the following error popped out in the command prompt window.





 If you notice that the error was due to the space in the group name. The for loop script by default took the space as delimiter


So, I have to manually specify the delimiter by ""comma"" instead of the default ""space"" in the script

New Script with delimiter by comma: 

   
Now with the new script, I am able to create the group name with space in it.






Success message of group created: -






Hope, this might help you as well...


Tuesday, 25 April 2017

Search for User availability in domain using Powershell


Most of the time, we get request from the different teams to validate the list of domain accounts existance in domain and ask to provide the report. 

By using the POWERSHELL method we can easily can generate the report. The script may be looking to much of data but I tried to make it simple for beginners like me to understand it... :)

Here is the script content : 

# Before starting to use this script, please make notepad file with filename as Users.txt in   # the same directory where this powershell script is stored

# The input file should contain the user login name ( Samaccountname )

$logpath=".\"
$logdate = (Get-Date -Format ddMMyyyy)
$logfilename= $logdate+"User-check"+".log"
$log1=$logpath +"\" +$logfilename

Import-Module Activedirectory

# Simple method to test whether input file provided or not. If not provided, script will Exit.

if (Test-path -Path ".\users.txt")
{

Write-Host "Input file is valid"
}
else
{
Write-host "Input file is not avialable, Existing the Script"
EXIT
}

# Storing the "Users.txt" file contents to the variable called "data"

$data=Get-content '.\users.txt'

# Adding header information to the log file

Add-content $log1 -value "Samaccountname,Availability"


# Looping through the each account to variable called "User"

# Then using Try & Catch Method, script check for the account existance in Domain


foreach ($user in $data)
{

Try {
        Get-aduser $user -ErrorAction stop |out-null


        Add-Content $log1 -Value "$User,YES"
    }
    Catch {
        Add-Content $log1 -Value "$user,NO"
    }
  
  }

EXIT

# END Of Script ###########################################################