added folders and scripts

This commit is contained in:
Gramzon
2025-01-20 15:48:15 +01:00
parent f8ac849f76
commit 44898bf457
15 changed files with 667 additions and 0 deletions
@@ -0,0 +1,87 @@
# Windows Server PowerShell Guide - Tidssone og Tastaturoppsett
## Viktig - Kjøre PowerShell som Administrator
#For å kunne utføre kommandoene i denne guiden, må PowerShell kjøres med administratorrettigheter. Dette kan gjøres på to måter:
#1. Høyreklikk på PowerShell-ikonet og velg "Kjør som administrator"
#2. Eller søk etter PowerShell i startmenyen, høyreklikk og velg "Kjør som administrator"
#Man vil kunne se at PowerShell kjører som administrator ved at vindustittelen viser "Administrator: Windows PowerShell" istedenfor bare "Windows PowerShell".
## Kontrollere Tidssone på Windows Server
#For å sjekke hvilken tidssone serveren er satt til, bruker man følgende kommando:
#powershell
Get-TimeZone
#Denne kommandoen benytter seg av `Get-TimeZone` cmdlet'en som viser nåværende tidssone-innstillinger. Resultatet vil vise Id, DisplayName og StandardName for gjeldende tidssone.
#Hvis serveren ikke er satt til norsk tidssone, kan man endre dette med følgende kommando:
#powershell
Set-TimeZone -Id "W. Europe Standard Time"
#Denne kommandoen bruker `Set-TimeZone` cmdlet'en med `-Id` parameteren. "W. Europe Standard Time" er den korrekte identifikatoren for norsk tidssone (UTC+01:00 Oslo, København, Stockholm).
## Kontrollere Tastaturoppsett
#For å sjekke nåværende tastaturoppsett, bruk følgende kommando:
#powershell
Get-WinUserLanguageList
#Denne cmdlet'en viser alle installerte språk- og tastaturinnstillinger. Man ser på LanguageTag og InputMethodTips for å identifisere tastaturoppsettet.
#Hvis norsk tastatur ikke er installert eller satt som standard, kan man legge til dette med følgende kommandoer:
#powershell
$CurrentLanguage = New-WinUserLanguageList -Language "nb-NO"
$CurrentLanguage[0].InputMethodTips.Add("0414:00000414")
Set-WinUserLanguageList -LanguageList $CurrentLanguage -Force
#La oss bryte ned denne sekvensen:
#1. `New-WinUserLanguageList` cmdlet'en oppretter en ny språkliste med norsk (bokmål) som språk
#2. `InputMethodTips.Add()` legger til det norske tastaturoppsettet (0414:00000414 er koden for norsk tastaturlayout)
#3. `Set-WinUserLanguageList` anvender de nye innstillingene med `-Force` parameteren for å overskrive eksisterende innstillinger
#Etter at kommandoene er kjørt, må man logge ut og inn igjen for at endringene skal tre i kraft.
## Nettverkskonfigurasjon
#Normalt ville en domenekontroller være konfigurert med en statisk IP-adresse for å sikre stabil tilgang til domenets tjenester. I dette tilfellet, siden maskinene kjører i et OpenStack-miljø hvor IP-adressene styres dynamisk av plattformen, beholder vi den dynamiske IP-konfigurasjonen.
#For å se gjeldende IP-konfigurasjon, kan følgende kommandoer benyttes:
#powershell
Get-NetIPAddress
#Denne kommandoen viser alle IP-adresser konfigurert på maskinen. For mer detaljert informasjon om nettverksadaptere, bruk:
#powershell
Get-NetAdapter
#For å se full TCP/IP-konfigurasjon inkludert gateway og DNS-servere:
#powershell
Get-NetIPConfiguration -Detailed
#Denne cmdlet'en gir en omfattende oversikt over:
#- IP-adresser (både IPv4 og IPv6)
#- Standard gateway
#- DNS-serverinnstillinger
#- Nettverksadapter status
#- DHCP-status
## Viktige Notater
#- Alle disse kommandoene må kjøres med administratorrettigheter
#- Endringer i tastaturoppsett krever en utlogging for å aktiveres fullstendig
#- Tidssoneendringer trer i kraft umiddelbart
#- Kommandoene kan verifiseres ved å kjøre de første kommandoene på nytt etter endringene er gjort
#- I et produksjonsmiljø ville en domenekontroller normalt ha statisk IP-adresse
+243
View File
@@ -0,0 +1,243 @@
# Managing Active Directory Groups with PowerShell
## Basic Group Creation and Deletion
#Let's start with the basic commands to create and delete Active Directory groups.
### Creating a Basic Security Group
#powershell
# Create a new security group
New-ADGroup -Name "IT Support" `
-GroupScope Global `
-GroupCategory Security `
-Path "OU=Groups,DC=InfraIT,DC=sec"
### Deleting a Group
#Powershell
# Remove a group
Remove-ADGroup -Identity "IT Support" -Confirm:$false
## Creating Multiple Groups Using an Array
#Here's how to create multiple groups using an array structure.
#powershell
# Define your groups with their properties
$groups = @(
@{
Name = "IT Support"
Path = "OU=IT,OU=Groups,DC=InfraIT,DC=sec"
Scope = "Global"
Category = "Security"
},
@{
Name = "HR Team"
Path = "OU=HR,OU=Groups,DC=InfraIT,DC=sec"
Scope = "Global"
Category = "Security"
},
@{
Name = "Finance Users"
Path = "OU=Finance,OU=Groups,DC=InfraIT,DC=sec"
Scope = "Global"
Category = "Security"
}
)
# Create each group
foreach ($group in $groups) {
New-ADGroup -Name $group.Name `
-GroupScope $group.Scope `
-GroupCategory $group.Category `
-Path $group.Path
}
## Advanced Group Management with Error Handling
#Here's a more robust script that includes existence checking and error handling.
#powershell
function New-CustomADGroup {
param (
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
[string]$Path,
[ValidateSet("Global", "Universal", "DomainLocal")]
[string]$Scope = "Global",
[ValidateSet("Security", "Distribution")]
[string]$Category = "Security",
[string]$Description
)
try {
# Check if group exists
$existingGroup = Get-ADGroup -Filter "Name -eq '$Name'" -ErrorAction SilentlyContinue
if ($null -eq $existingGroup) {
# Create new group
$params = @{
Name = $Name
GroupScope = $Scope
GroupCategory = $Category
Path = $Path
}
if ($Description) {
$params.Add("Description", $Description)
}
New-ADGroup @params
Write-Host "Successfully created group: $Name" -ForegroundColor Green
return $true
} else {
Write-Host "Group already exists: $Name" -ForegroundColor Yellow
return $false
}
} catch {
Write-Host "Failed to create group: $Name" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
return $false
}
}
## Managing Group Membership
#Here's how to manage group members, including both users and groups.
### Adding Members to a Group
powershell
function Add-CustomADGroupMember {
param (
[Parameter(Mandatory)]
[string]$GroupName,
[Parameter(Mandatory)]
[string[]]$Members
)
try {
# Check if group exists
$group = Get-ADGroup -Identity $GroupName -ErrorAction Stop
foreach ($member in $Members) {
try {
# Try to get member (could be user or group)
$adObject = Get-ADObject -Filter {(objectClass -eq "user") -or (objectClass -eq "group")} -Properties ObjectClass |
Where-Object {$_.Name -eq $member}
if ($null -ne $adObject) {
# Check if already a member
$isMember = Get-ADGroupMember -Identity $GroupName | Where-Object {$_.Name -eq $member}
if ($null -eq $isMember) {
Add-ADGroupMember -Identity $GroupName -Members $adObject
Write-Host "Successfully added $member to $GroupName" -ForegroundColor Green
} else {
Write-Host "$member is already a member of $GroupName" -ForegroundColor Yellow
}
} else {
Write-Host "Member not found: $member" -ForegroundColor Red
}
} catch {
Write-Host "Failed to add member $member to $GroupName" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
}
}
} catch {
Write-Host "Group not found: $GroupName" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
}
}
### Removing Members from a Group
#powershell
function Remove-CustomADGroupMember {
param (
[Parameter(Mandatory)]
[string]$GroupName,
[Parameter(Mandatory)]
[string[]]$Members
)
try {
# Check if group exists
$group = Get-ADGroup -Identity $GroupName -ErrorAction Stop
foreach ($member in $Members) {
try {
# Check if member exists in group
$isMember = Get-ADGroupMember -Identity $GroupName | Where-Object {$_.Name -eq $member}
if ($null -ne $isMember) {
Remove-ADGroupMember -Identity $GroupName -Members $member -Confirm:$false
Write-Host "Successfully removed $member from $GroupName" -ForegroundColor Green
} else {
Write-Host "$member is not a member of $GroupName" -ForegroundColor Yellow
}
} catch {
Write-Host "Failed to remove member $member from $GroupName" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
}
}
} catch {
Write-Host "Group not found: $GroupName" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
}
}
## Complete Example with All Features
#Here's a complete example that puts it all together:
#powershell
# Define groups to create
$groups = @(
@{
Name = "IT Support"
Path = "OU=IT,OU=Groups,DC=InfraIT,DC=sec"
Scope = "Global"
Category = "Security"
Members = @("John.Doe", "Jane.Smith", "Help Desk")
},
@{
Name = "HR Team"
Path = "OU=HR,OU=Groups,DC=InfraIT,DC=sec"
Scope = "Global"
Category = "Security"
Members = @("Sarah.Johnson", "HR Managers")
}
)
# Create groups and add members
foreach ($group in $groups) {
if (New-CustomADGroup -Name $group.Name -Path $group.Path -Scope $group.Scope -Category $group.Category) {
if ($group.Members) {
Add-CustomADGroupMember -GroupName $group.Name -Members $group.Members
}
}
}
# Example of removing members
Remove-CustomADGroupMember -GroupName "IT Support" -Members @("John.Doe")
# Example of adding new members
Add-CustomADGroupMember -GroupName "HR Team" -Members @("New.Employee")
#This script demonstrates:
#1. Creating groups with error handling
#2. Checking for existing groups
#3. Adding both users and groups as members
#4. Removing members
#5. Handling errors at each step
#6. Providing clear feedback for all operations
#Remember to replace "DC=InfraIT,DC=sec" and the OU paths with your actual domain structure. Also ensure that the users and groups you're referencing actually exist in your Active Directory environment.
+299
View File
@@ -0,0 +1,299 @@
# Managing Active Directory OUs with PowerShell
## Basic OU Creation and Deletion
#Let's start with the simplest way to create and delete an Organizational Unit (OU) in Active Directory using PowerShell.
### Creating a Basic OU
#powershell
New-ADOrganizationalUnit -Name "TestOU" -Path "DC=infrait,DC=sec"
### Deleting the OU
#There are two approaches to delete an OU:
#### Option 1: Disable Protection and Delete
#powershell
# First, disable the protection
Set-ADOrganizationalUnit -Identity "OU=TestOU,DC=infrait,DC=sec" -ProtectedFromAccidentalDeletion $false
# Then delete the OU
Remove-ADOrganizationalUnit -Identity "OU=TestOU,DC=infrait,DC=sec" -Confirm:$false
#### Option 2: Create OU Without Protection
#When creating new OUs, you can disable the protection from the start:
#powershell
# Create OU with protection disabled
New-ADOrganizationalUnit -Name "TestOU" -Path "DC=infrait,DC=sec" -ProtectedFromAccidentalDeletion $false
# Now you can delete it without first disabling protection
Remove-ADOrganizationalUnit -Identity "OU=TestOU,DC=infrait,DC=sec" -Confirm:$false
## Checking OU Existence Before Creation
#Now, let's make our script more robust by checking if the OU exists before trying to create it.
### Checking and Creating an OU
#powershell
# First command: Check if OU exists
if (-not(Get-ADOrganizationalUnit -Filter "Name -eq 'TestOU'" -SearchBase "DC=infrait,DC=sec")) {
New-ADOrganizationalUnit -Name "TestOU" -Path "DC=infrait,DC=sec"
}
### Deleting with Verification
#powershell
# Second command: Check if OU exists before deleting
if (Get-ADOrganizationalUnit -Filter "Name -eq 'TestOU'" -SearchBase "DC=infrait,DC=sec") {
Remove-ADOrganizationalUnit -Identity "OU=TestOU,DC=infrait,DC=sec" -Recursive -Confirm:$false
}
## Advanced Error Handling with Try-Catch
#Let's enhance our script with proper error handling using try-catch blocks.
#powershell
# Define the OU details
$ouName = "TestOU"
$domainPath = "DC=infrait,DC=sec"
$ouPath = "OU=$ouName,$domainPath"
# Try to create the OU with error handling
try {
# Check if OU exists
if (-not(Get-ADOrganizationalUnit -Filter "Name -eq '$ouName'" -SearchBase $domainPath)) {
New-ADOrganizationalUnit -Name $ouName -Path $domainPath
Write-Host "Successfully created OU: $ouName" -ForegroundColor Green
} else {
Write-Host "OU already exists: $ouName" -ForegroundColor Yellow
}
} catch {
Write-Host "Failed to create OU: $ouName" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
}
```
## Creating Nested OUs
#When working with nested OUs, it's important to understand the correct path structure and how to create OUs within other OUs.
### Finding the Correct Path
#To find the path of an existing OU:
#powershell
# Get the Distinguished Name of an existing OU
Get-ADOrganizationalUnit -Filter "Name -eq 'ParentOU'" -SearchBase "DC=infrait,DC=sec" |
Select-Object -ExpandProperty DistinguishedName
### Creating an OU Inside Another OU
#powershell
# First, create the parent OU
New-ADOrganizationalUnit -Name "ParentOU" -Path "DC=infrait,DC=sec"
# Then create a child OU inside the parent OU
New-ADOrganizationalUnit -Name "ChildOU" -Path "OU=ParentOU,DC=infrait,DC=sec"
### Complete Example with Nested OUs and Error Handling
#powershell
# Define the OU structure
$parentOUName = "ParentOU"
$childOUName = "ChildOU"
$domainPath = "DC=infrait,DC=sec"
# Function to create an OU with error handling
function Create-ADOU {
param (
[string]$Name,
[string]$Path
)
try {
if (-not(Get-ADOrganizationalUnit -Filter "Name -eq '$Name'" -SearchBase $Path)) {
New-ADOrganizationalUnit -Name $Name -Path $Path
Write-Host "Successfully created OU: $Name" -ForegroundColor Green
return $true
} else {
Write-Host "OU already exists: $Name" -ForegroundColor Yellow
return $true
}
} catch {
Write-Host "Failed to create OU: $Name" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
return $false
}
}
# Create parent OU
$parentCreated = Create-ADOU -Name $parentOUName -Path $domainPath
# If parent was created successfully, create child OU
if ($parentCreated) {
$parentPath = "OU=$parentOUName,$domainPath"
Create-ADOU -Name $childOUName -Path $parentPath
}
#This final example shows how to:
#1. Create a reusable function for OU creation
#2. Handle errors appropriately
#3. Create nested OUs
#4. Verify success at each step
#5. Provide clear feedback to the user
### Complete Example with Multiple Nested OUs
#powershell
# Define the OU structure using a hashtable
$ouStructure = @{
"IT" = @(
"Hardware",
"Software",
"Network",
"Support"
)
"HR" = @(
"Recruitment",
"Training",
"Benefits",
"Employee Records"
)
"Finance" = @(
"Accounting",
"Payroll",
"Budgeting",
"Reporting"
)
}
$domainPath = "DC=InfraIT,DC=sec"
# Function to create an OU with error handling
function New-CustomADOU {
param (
[string]$Name,
[string]$Path,
[switch]$DisableProtection
)
try {
# Check if OU exists - we need to handle the case where the search base doesn't exist
try {
$existingOU = Get-ADOrganizationalUnit -Filter "Name -eq '$Name'" -SearchBase $Path -ErrorAction Stop
} catch {
# If SearchBase doesn't exist, we know the OU doesn't exist
$existingOU = $null
}
if (-not $existingOU) {
# Create new OU
$params = @{
Name = $Name
Path = $Path
ProtectedFromAccidentalDeletion = -not $DisableProtection
}
New-ADOrganizationalUnit @params
Write-Host "Successfully created OU: $Name in $Path" -ForegroundColor Green
return $true
} else {
Write-Host "OU already exists: $Name in $Path" -ForegroundColor Yellow
return $true
}
} catch {
Write-Host "Failed to create OU: $Name" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
return $false
}
}
# Function to remove an OU with error handling
function Remove-CustomADOU {
param (
[string]$Identity
)
try {
# Check if OU exists
$ou = Get-ADOrganizationalUnit -Identity $Identity -ErrorAction SilentlyContinue
if ($ou) {
# Disable protection
Set-ADOrganizationalUnit -Identity $Identity -ProtectedFromAccidentalDeletion $false
# Remove OU
Remove-ADOrganizationalUnit -Identity $Identity -Confirm:$false
Write-Host "Successfully removed OU: $Identity" -ForegroundColor Green
return $true
} else {
Write-Host "OU does not exist: $Identity" -ForegroundColor Yellow
return $true
}
} catch {
Write-Host "Failed to remove OU: $Identity" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
return $false
}
}
# Create all OUs
foreach ($parentOU in $ouStructure.Keys) {
# Create parent OU
$parentPath = $domainPath
Write-Host "`nCreating parent OU: $parentOU" -ForegroundColor Cyan
$parentCreated = New-CustomADOU -Name $parentOU -Path $parentPath
if ($parentCreated) {
# Verify parent OU exists before creating children
$parentFullPath = "OU=$parentOU,$domainPath"
$verifyParent = Get-ADOrganizationalUnit -Identity $parentFullPath -ErrorAction SilentlyContinue
if ($verifyParent) {
Write-Host "Verified parent OU exists, creating children..." -ForegroundColor Cyan
# Create child OUs
foreach ($childOU in $ouStructure[$parentOU]) {
$childPath = $parentFullPath
New-CustomADOU -Name $childOU -Path $childPath
}
} else {
Write-Host "Parent OU verification failed for: $parentOU" -ForegroundColor Red
Write-Host "Cannot create child OUs" -ForegroundColor Red
}
}
}
# Example of how to remove the entire structure
function Remove-OUStructure {
param (
[hashtable]$Structure,
[string]$DomainPath
)
# Remove child OUs first
foreach ($parentOU in $Structure.Keys) {
foreach ($childOU in $Structure[$parentOU]) {
$childPath = "OU=$childOU,OU=$parentOU,$DomainPath"
Remove-CustomADOU -Identity $childPath
}
# Then remove parent OU
$parentPath = "OU=$parentOU,$DomainPath"
Remove-CustomADOU -Identity $parentPath
}
}
# Example usage to remove the structure:
# Remove-OUStructure -Structure $ouStructure -DomainPath $domainPath
#This example demonstrates:
#1. Creating a complex OU structure using a hashtable
#2. Reusable functions for creating and removing OUs
#3. Proper error handling and protection management
#4. Clear feedback for each operation
#5. Hierarchical creation (parents before children)
#6. Safe removal process (children before parents)
#7. Status checking before each operation
#Remember to replace "DC=InfraIT,DC=Sec" with your actual domain path in all examples.
+38
View File
@@ -0,0 +1,38 @@
# Installing RSAT Tools for Windows using PowerShell
#**Note**: These commands require administrative privileges. Make sure to run PowerShell as Administrator before executing them.
## Listing Available RSAT Tools
#First, let's check which RSAT tools are available on your system:
#powershell
Get-WindowsCapability -Name RSAT* -Online | Select-Object Name, State
#![alt text](ListRSAT.png)
## Installing Specific RSAT Tools
### Active Directory Tools
#To install Active Directory Domain Services and Lightweight Directory Services Tools:
#powershell
Add-WindowsCapability -Online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0"
#[alt text](InstallRSAT_ADDS.png)
### DNS Server Tools
#To install DNS Server Tools:
#powershell
Add-WindowsCapability -Online -Name "Rsat.Dns.Tools~~~~0.0.1.0"
## Verifying Installation
#To verify that the tools were installed successfully:
#powershell
Get-WindowsCapability -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0 -Online | Select-Object Name, State
Get-WindowsCapability -Name Rsat.Dns.Tools~~~~0.0.1.0 -Online | Select-Object Name, State
#**Note**: These commands require administrative privileges. Make sure to run PowerShell as Administrator before executing them.
View File