Archive for the ‘Scripts’ Category

We’ll start with inheritance.Sometime (when creating folder for roaming profiles),we need to disable inheritance in order to avoid users to access other user’s folders.
$acl = Get-Item $dir |get-acl
$acl.SetAccessRuleProtection($true,$true)
$acl |Set-Acl

First,we export ACL’s to variable,then in SetAccessRuleProtection($true,$true)

we are actually managing permissions.First parameter enables ($true),or disables ($false) inheritance

,while the second one manages Access control entries (ACE), ($true-keep current ACE’s,$false-remove them and start with new ones)

And third line simply applies our decisons

Setting NTFS permissions

To set NTFS permissions,we first need to install  File System Security PowerShell Module

To see current NTFS permissions type
Get-Item “c:\1” | Get-NTFSAccess

Untitled

To set permissions we need to type:
Add-NTFSAccess -Path C:\1   -Account ‘example\Authenticated Users ‘  -AccessRights’Fullcontrol

For removing permissions

Remove-NTFSAccess -Path “c:\1” -Account “example\domain users” -AccessRights FullControl

Inherited permissions cannot be removed

To remove all NTFS permissions for account

Get-ChildItem -Path c:\1 -Recurse |

Get-NTFSAccess -Account “example\test group” -ExcludeInherited |

Remove-NTFSAccess

Get-ChildItem with -recurse switch procesess files and folders recusively

Setting ACE permissions

Flag combinations can be found on microsoft site:

https://msdn.microsoft.com/en-us/library/ms229747%28v=vs.110%29.aspx

From this table we can combine flags and apply them to folders,subfolders or files,for example,to set ACE’s

to Full control for Folder (folder test has no subfolders):

$InheritanceFlags=[System.Security.AccessControl.InheritanceFlags]”ContainerInherit, none”

$PropagationFlags=[System.Security.AccessControl.PropagationFlags]”None”

$FileSystemAccessRights=[System.Security.AccessControl.FileSystemRights]”FullControl”

$acl=get-acl c:\test

$AccessRule=NEW-OBJECT System.Security.AccessControl.FileSystemAccessRule(“test group”,$FileSystemAccessRights,$InheritanceFlags,$PropagationFlags,$AccessControl)
$Acl | set-acl c:\test

Advertisements

You have to retrive data from database,results consist from two column,during october results are populated in columns A and B,and during november results must be retrived in colums C and D,and in december E and F respectively.Here is how to accomplish that:

First,be sure that macros are enabled (File-Options-Trust Center-Trust Center Settings-Macro Settings-Enable All macros

Insert new module and paste this code

(I am using Oracle OO40 driver to connect to the database,but your method may vary)

Sub auto_open()

Open “C:\Local Data\autowater\pass.txt” For Input As #1
Input #1, shorttext
Close #1
mystring = shorttext
Open “C:\Local Data\autowater\pass1.txt” For Input As #2
Input #2, shorttext
Close #2
mystring1 = shorttext

Dim objSession As Object
Dim objDataBase As Object

Dim v As Variant
Dim pom As String
If (Month(Date)) = 10 Then

v = v + 1
pom = “A:B”

End If

If (Month(Date)) = 11 And Day(Date) = 1 Then

v = v + 1
pom = “A:B”

ElseIf (Month(Date)) = 11 And Day(Date) <> 1 Then

v = v + 3
pom = “C:D”

End If

If (Month(Date)) = 12 And Day(Date) = 1 Then

v = v + 3
pom = “C:D”

ElseIf (Month(Date)) = 12 And Day(Date) <> 1 Then

v = v + 5
pom = “E:F”

End If

Set objSession = CreateObject(“OracleInProcServer.XOraSession”)

Set objDataBase = objSession.OpenDatabase(mystring, mystring1, 0&)

Dim sql As String

Sheets(“sheet2″).Select
Range(pom).Select
Selection.ClearContents

Application.ScreenUpdating = False

Application.Calculation = xlCalculationManual

sql = sql & ” your SQL here”

Set oraDynaSet = objDataBase.DBCreateDynaset(sql, 0&)

If oraDynaSet.RecordCount > 0 Then
oraDynaSet.MoveFirst
For x = 0 To oraDynaSet.Fields.Count – 1
Cells(1, x + v) = oraDynaSet.Fields(x).Name

Next

For y = 0 To oraDynaSet.RecordCount – 1
For x = 0 To oraDynaSet.Fields.Count – 1
Cells(y + 2, x + v) = oraDynaSet.Fields(x).Value
Next
oraDynaSet.MoveNext
Next
End If
Application.Calculation = xlCalculationAutomatic
Set objSession = Nothing
Set objDataBase = Nothing
End Sub

We have a block of code which needs to be executed only on weekdays,how to accomplish that.First,create a file with vbs extension and paste a following code in it:

dtmToday = Date()

dtmDayOfWeek = DatePart(“w”, dtmToday)

Select Case dtmDayOfWeek

Case 1

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Sunday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 2

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Monday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 3

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Tuesday”)
objFileToWrite.Close
Set objFileToWrite = Nothing

Case 4

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Wednesday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 5

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Thursday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 6

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Friday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
Case 7

Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\@IT\Batch\day.txt”,2,true)
objFileToWrite.WriteLine(“Saturday”)
objFileToWrite.Close
Set objFileToWrite = Nothing
End Select

name file check.vbs,save and close the file.

This code create a file called C:\@IT\Batch\day.txt in which everu day of the week is written.We will use thata in this file later on.

Now open a batch file with block code which needs to be executed every day

Our batch code is in file called everyday.bat (it this file is code which runs everyday,plus block which needs to be run only on weekdays).

Let’s say our block code is called weekday (runs only by weekdays)

rem code which is executed every day

//some code

Find “Satyrday” “C:\@IT\Batch\day.txt”>”C:\@IT\Batch\vidi day.txt” || Find “Sunday” “C:\@IT\Batch\day.txt”>”C:\@IT\Batch\desired day.txt”

if %ERRORLEVEL% NEQ 0 (
goto :end
) else (
goto:weekday
)

:weekday

some code which runs on weekday

:end

exit

Find command searchs for string saturday or sunday (||) in file day.txt and writes results in file desired day.txt.If it find the match then code in :weekend is run,othervise,batch exits.

Now we need to create batch file which first calls check.vbs and then executes everyday.bat

create a new file with bat.extension and write:

start check.vbs

start everyday.bat

name this file whatever You like and include it in task scheduler.

Let’s say you must create backup every day,but backups must distinguish themeslves by months.Our goal is,for every month to create separate folder,and within that folder,create subfolder for evrery day of the month (C:\Backup1January1.01)

Please note that in my example i used dd.mm.yyyy date format !!

Let’s begin:

Open notepad and write these lines:

:: variables
:: setting v to equal the current numeric month
set v=%date:~3,2%
set y= %date:~6,98%
set folder=%date:~0,5%
:: putting all the possible months into a variable which will be parsed in the for loop
set month_text=”01 JanuarY” “02 February” “03 March” “04 April” “05 May” “06 June” “07 July” “08 August” “09 September” “10 October” “11 November” “12 December”

:: for loop is parsing the month_text variable and using the find command with the numeric month to set the current_month variable
for %%I in (%month_text%) do echo %%I |find “%v%” &&set current_month=%%~I
set drive=H:\%current_month%%y%\

:: checking if current month folder exists. If not create it.
IF NOT EXIST “%drive%” mkdir “%drive%”
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
%backupcmd% “” “%drive%\%folder%”

save a file with .bat extension,and double click it to see results.

Also,note that destination and source path path must be surrounded by quotes

I copied to H:\,but you can change the destination

Powershell:Bulk add Domain users

Posted: June 9, 2015 in Scripts
Tags:

Create bunch of users using PowerShell

csv file with user details
With powershell You can create many users with a single line of code.During my preparation for MCSA exam on Windows Server 2012 i tried this.I attached file which contains 2 users,You can add as many as You like.This site didn’t allow me to upload csv extension,so before You try,the extension of file must be changed to .csv (instead 1.xls).

Save file on Your Windows Server 2012 server (in my case C:\),open powershell as admin and type next lines:

Import-csv “c:\1.csv” | ForEach-Object {new-aduser -name $_.name -UserPrincipalName $_.use
rprincipalname -DisplayName $_.displayname -givenName $_.givenname -surname $_.surname -path $_.path -profilepath $_.pro
filepath -homedrive $_.homedrive -HomeDirectory $_.homedirectory -AccountPassword (convertto-securestring $_.accountpass
word -asplaintext -force) -Enabled ([System.Convert]::toboolean($_.enabled)) -ChangePasswordAtLogon ([system.convert]::
ToBoolean($_.changepasswordatlogon)) }

Press Enter and user creation is done !.