All posts by steven
Split Function
function Split-array { <# .SYNOPSIS Split an array .PARAMETER inArray A one dimensional array you want to split .EXAMPLE Split-array -inArray @(1,2,3,4,5,6,7,8,9,10) -parts 3 .EXAMPLE Split-array -inArray @(1,2,3,4,5,6,7,8,9,10) -size 3 #> param($inArray,[int]$parts,[int]$size) if ($parts) { $PartSize = [Math]::Ceiling($inArray.count / $parts) } if ($size) { $PartSize = $size $parts = [Math]::Ceiling($inArray.count / $size) } $outArray = @() for ($i=1; $i -le $parts; $i++) { $start = (($i-1)*$PartSize) $end = (($i)*$PartSize) - 1 if ($end -ge $inArray.count) {$end = $inArray.count} $outArray+=,@($inArray[$start..$end]) } return ,$outArray }
http://gallery.technet.microsoft.com/scriptcenter/Split-an-array-into-parts-4357dcc1
File presence check
While (((Test-Path $xmlConfigfile) -eq $false) -or ($NoXML)){ [System.Windows.Forms.MessageBox]::Show("ERROR: $xmlConfigfile not found!") write-host De XML file kan niet gevonden worden -F Red If (!($psISE)){"Press any key to continue...";[void][System.Console]::ReadKey($true)} exit }
Operators
-eq Equal
-ne Not equal
-ge Greater than or equal
-gt Greater than
-lt Less than
-le Less than or equal
-like Wildcard comparison
-notlike Wildcard comparison
-match Regular expression comparison
-notmatch Regular expression comparison
-replace Replace operator
-contains Containment operator
-notcontains Containment operator
To perform a Case-Sensitive comparison just prefix any of the above with “c”
for example -ceq for case-sensitive Equals or -creplace for case-sensitive replace.
Similarly prefixing with “i” will explicitly make the operator case insensitive.
Types
-is Is of a type
-isnot Is not of a type
-as As a type, no error if conversion fails
Logical operators
-and Logical And
-or Logical Or
-not logical not
! logical not
Bitwise operators
-band Bitwise and
-bor Bitwise or
Restore exported ACL on folder
Import-Module ActiveDirectory
$csv = gci .CSVFiles* -Include *.csv
$list += import-csv $CSV -Delimiter ';'
foreach ($entry in $list){
$Folderpath=$entry.'Folder path'
$Identity=$entry.IdentityReference
$AccessControlType=$entry.AccessControlType
$IsInherited=$entry.IsInherited
$Inheritanceflags=$entry.InheritanceFlags
$PropagationFlags=$entry.PropagationFlags
$FileSystemRights=$entry.FileSystemRights
$OldACL = get-acl $Folderpath
$Newpermission = ($Identity,$FileSystemRights,$Inheritanceflags,$PropagationFlags,$AccessControlType)
write-host $Newpermission
$AccessRule = new-object System.Security.AccessControl.FileSystemAccessRule $NewPermission
$OldACL.SetAccessRule($AccessRule)
$OldAcl | Set-ACL $Folderpath
Set ACL on folder
$acl = Get-Acl c:temp $permission = "domainuser","FullControl","Allow" $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission $acl.SetAccessRule($accessRule) $acl | Set-Acl c:temp
Export permissions to CSV file
$OutFile = "C:Permissions.csv"
$Header = "Folder Path,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags"
Del $OutFile
Add-Content -Value $Header -Path $OutFile
$RootPath = "C:Test"
$Folders = dir $RootPath -recurse | where {$_.psiscontainer -eq $true}
foreach ($Folder in $Folders){
$ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access }
Foreach ($ACL in $ACLs){
$OutInfo = $Folder.Fullname + "," + $ACL.IdentityReference + "," + $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags
Add-Content -Value $OutInfo -Path $OutFile
}}
Rechten op nieuwe folder uitdelen
new-item -path h: -name Folder1Folder2 -type directory
$locatie=('\Domain.localdfs$deelnemerusers$samaccountnameFolder1')
Sleep -s 30
$Domain = "$Domain$Groepsnaam"
$NewACL = get-acl $locatie
$Newpermission = ($Domain,"Modify, Synchronize","ContainerInherit, ObjectInherit", "None","Allow")
$AccessRule = new-object System.Security.AccessControl.FileSystemAccessRule $NewPermission
$NewACL.SetAccessRule($AccessRule)
$NewAcl | Set-ACL $locatie
Volgnummer bijhouden in een CSV file
Import-module 'ActiveDirectory' $getadusers = @() $filter = @() $samaccountnamelijst = @() $Header = 'Samaccountname' $CSV = '.\Gemeentenet.csv' $Extension = $null $import = @() Add-Content -Value $Header -Path $CSV $getadusers = Get-aduser -filter * foreach ($entry in $getadusers){if ($entry -ne $null){$samaccountnamelijst += $entry.samaccountname}} foreach ($sam in $samaccountnamelijst){Add-Content -Value $sam -Path $CSV} $Deelnemerlijst = @() $allusers = import-csv $csv foreach ($Deelnemeruser in $allusers){if ($Deelnemeruser -match $deelnemer){$Deelnemerlijst += $Deelnemeruser}} $Deelnemerlijst | export-csv .\$Deelnemer.csv $entry = $null $username = 'othext' $CSV = '.\Gemeentenet.csv' $import = @() $filter = @() $import = import-csv $csv foreach ($entry in $import){if ($entry -match $username){$filter += $entry.samaccountname}} $filter = $filter -split "@{samaccountname=" |sort $filter = $filter -split "$username" | sort $filter = $filter -split "}" | Sort -Descending $nummer = [int]$filter[0] $nummer++ Add-content -Value $volledigesamaccountname -path $csv
XLSX aanmaken in Powershell
$csvs = Get-ChildItem .* -Include *.csv $y=$csvs.Count Write-Host “Detected the following CSV files: ($y)” foreach ($csv in $csvs) { Write-Host ” “$csv.Name } $outputfilename = read-host “Please enter the output file name: “ Write-Host Creating: $outputfilename $excelapp = new-object -comobject Excel.Application $excelapp.sheetsInNewWorkbook = $csvs.Count $xlsx = $excelapp.Workbooks.Add() $sheet=1 foreach ($csv in $csvs) { $row=1 $column=1 $worksheet = $xlsx.Worksheets.Item($sheet) $worksheet.Name = $csv.Name $file = (Get-Content $csv) foreach($line in $file) { $linecontents=$line -split ‘,(?!s*w+”)’ foreach($cell in $linecontents) { $worksheet.Cells.Item($row,$column) = $cell $column++ } $column=1 $row++ } $sheet++ } $xlsx.SaveAs($outputfilename) $excelapp.quit()