Keistad-ICT
  1. U bevindt zich hier:  
  2. Startpagina
  3. PowerShell

PowerShell

PowerShell script om in de Active Directory een account in een OU aan te maken.

Gegevens
Categorie: PowerShell
Gepubliceerd: 26 augustus 2024
Hits: 41


PowerShell script om SQL Server Agent service account aan te maken op een Windows Server 2019 of 2022 Domain Controller.

Het account wordt in een eigen OU aangemaakt. De OU wordt ook aangemaakt.

 Auteur: Marcel Runte
 Datum: 01-2024

 


Clear-Host
Write-Host -ForegroundColor Green "SQL Agent Service account in de Active Directory aanmaken..." 
Start-Sleep 10

$Null = Import-Module ActiveDirectory

# OU voor SQL Agent service account aanmaken
$Null = New-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $False -Name "SQLServer"       -Path "DC=GenG,DC=LOCAL"

# SQL Agent account aanmaken in de OU
New-ADUser -Name "srv_sqlagent" -GivenName "srv_sqlagent" -Surname "srv_sqlagent" -DisplayName "SQL Agent Service Account" -SamAccountName "srv_sqlagent" -UserPrincipalName "Dit e-mailadres wordt beveiligd tegen spambots. JavaScript dient ingeschakeld te zijn om het te bekijken." -Description "voor de SQL Agent Service" -Path "OU=SQLServer,DC=GenG,DC=local" -AccountPassword (ConvertTo-SecureString -AsPlainText "Pa$$w0rd!2024" -Force) -Enabled $true -PasswordNeverExpires $true

##############################################################
# Berichtje op het scherm tonen en sluiten na 10 seconden        
#                               
$Title = " G E F E L I C I T E E R D !"   
$Body = "De server is klaar!"
$Shell = new-object -comobject wscript.shell -ErrorAction Stop
$Box = $Shell.popup(" `n $Body",3,"$Title",0)
$Box | Out-Null
###############################################################

PowerShell script om Active Directory te installeren

Gegevens
Categorie: PowerShell
Gepubliceerd: 26 augustus 2024
Hits: 43


PowerShell script om Windows Server 2019 of 2022 in te stellen als Domain Controller.

 LET OP:  Zet dit script in C:\Scripts\ van de VM en sla het daarin op als: 2_InstallAD.ps1

Dit script wordt dan aangeroepen vanuit het script 1_PrepareDC.ps1

 Auteur: Marcel Runte
 Datum: 01-2024

# Script als RunAs Administrator runnen
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`"";
    exit;
}

 

# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
#Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force

<# Installatie en configuratie van de server tot Domain Controller  #>
$DomainName = "GenG.LOCAL"
$DomainNetBiosName= "GenG"
$SafemodePW = ConvertTo-SecureString -string "Welkom!123" -AsPlainText -Force

Write-Host  -ForegroundColor Green 'Active Directory en DNS installeren...'
Write-Host  -ForegroundColor Yellow 'Negeer de WARNING-berichten en wacht geduldig af.'
Write-Host  -ForegroundColor Green 'De server herstart automatisch als Active Directory is geïnstalleerd!'
$Null = Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools
$Null = Install-WindowsFeature DNS -IncludeManagementTools

#Server promoten naar Domain Controller
Write-Host  -ForegroundColor Green 'Server tot Domain Controller promoten...'
$Null = Install-ADDSForest -DomainName $DomainName -DomainNetBiosName $DomainNetBiosName -CreateDnsDelegation:$false -InstallDns:$true -SafeModeAdministratorPassword $SafemodePW -NoRebootOnCompletion:$false -Force


# AutoLogon weer uitschakelen
# $username = 'geng\administrator'
$username = '$DomainNetBiosName\Administrator'
#$password = 'Pa$$w0rd'
#$password = Get-Content C:\Scripts\pw.txt
$RegistryLocation = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
Remove-ItemProperty $RegistryLocation -Name 'AutoAdminLogon' 
Remove-ItemProperty $RegistryLocation -Name 'DefaultUsername'
Remove-ItemProperty $RegistryLocation -Name 'DefaultPassword'

# De gebruiker 'SQLAgent' aanmaken voor de SQL Agent service

function Set-RunOnce
{
    [CmdletBinding()]
    param
    (
        #The Name of the Registry Key in the Autorun-Key.
        [string]
        $KeyName = 'Run',

        #Command to run
        [string]
        $Command = '%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -file C:\Scripts\3_AddSQLServerAccount.ps1'
  
    ) 

    
    if (-not ((Get-Item -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce).$KeyName ))
    {
        New-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce' -Name $KeyName -Value $Command -PropertyType ExpandString
    }
    else
    {
        Set-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce' -Name $KeyName -Value $Command -PropertyType ExpandString
    }
}


# De Functie 'Set-RunOnce' aanroepen om na de herstart het SQL Server Agent account in de AD aan te maken
$Null = Set-RunOnce

Write-Host -Foregroudcolor Blue 'De gebruiker "SQLAgent" wordt na de reboot in de AD aangemaakt.'
Start-Sleep 10

Add-Content -Path C:\Users\Administrator\Desktop\Uitzetten.ps1 -Value "Stop-Computer"

Write-Host 'Installatie afronden...' -ForegroundColor Green
Write-Host 'De computer herstart automatisch!'
Write-Host '.'
Start-Sleep -s 60
Restart-Computer

PowerShell script om een Windows Server te installeren

Gegevens
Categorie: PowerShell
Gepubliceerd: 26 augustus 2024
Hits: 33


PowerShell script om Windows Server 2019 of 2022 in te stellen als een Domain Controller 

 LET OP:  Zet dit script in C:\Scripts\ van de VM.


 Auteur: Marcel Runte
 Datum: 01-2024

# Script als Administrator draaien
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`"";
    exit;
}

# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
# Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force

Clear-Host

# Display the splash screen 
Write-Output "###########################################################"
Write-Output "#                                                         #"
Write-Output "#   Met dit script wordt een Domain Controller gemaakt!   #"
Write-Output "#                                                         #"
Write-Output "###########################################################"
Write-Host ' '
Write-Host ' '
#  Variabelen definiëren
$username       = 'administrator'
# Write-Host 'De huidige computernaam is: ' Get-Content env:computername
$HostName       = Read-Host -Prompt 'Geef de nieuwe computernaam op: [DC01]'
Write-Host -ForegroundColor Black '.'
Write-Host -ForeGroundColor Yellow 'Controleer dat CAPS LOCK UIT staat!'
Write-Host -ForegroundColor Black '.'

# Wachtwoord opgeven
#$password       = Read-Host -Prompt 'Geef het Administrator wachtwoord op: [Pa$$w0rd]'  -AsSecureString
$pwd1 = Read-Host -Prompt 'Geef het Administrator wachtwoord op: [Pa$$w0rd]' -AsSecureString
$pwd2 = Read-Host "Geef het wachtwoord nog een keer op " -AsSecureString
$pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd1))
$pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd2))
if ($pwd1_text -ceq $pwd2_text) {
Write-Host "De wachtwoorden zijn hetzelfde"
} else {
Write-Host "De wachtwoorden komen niet overeen! Start het script opnieuw!"
Exit
}


#$IPLAN instellen
$IPLAN = '172.16.20.200'
$prompt = Read-Host "Druk op <ENTER> om dit IP-adres te accepteren of geef een ander IP-adres op [$($IPLAN)]"
if ($prompt -eq "") {} else {
    $IPLAN = $prompt
    }


Write-Host -ForeGroundColor Yellow 'De computernaam wordt '$HostName' '
Write-Host -ForeGroundColor Yellow 'Het IP-adres wordt    '$IPLAN' '
#Write-Host -ForeGroundColor Yellow 'De Default Gateway wordt   '$GWLAN' '
$confirm = Read-Host "Klopt dit? y/n"
if ($confirm -eq 'y') {
# Te nemen actie: Netwerk instellen
Write-Host -ForeGroundColor Green 'Netwerkconfiguratie instellen'
$Null  = Rename-NetAdapter -Name "Ethernet0" -NewName "LAN"
$Null  = Set-NetIPInterface -InterfaceAlias "LAN" -DHCP Disabled
$Null  = New-NetIPAddress   -InterfaceAlias "LAN" –IPAddress $IPLAN -PrefixLength 24 -DefaultGateway 172.16.20.1
#$Null = Set-DNSClientServerAddress -InterfaceAlias "LAN" –ServerAddresses (“127.0.0.1”,”172.16.20.200”)
$Null  = Set-DNSClientServerAddress -InterfaceAlias "LAN" –ServerAddresses (“127.0.0.1”,"$IPLAN")
$Null  = Disable-NetAdapterBinding  -InterfaceAlias "LAN" -ComponentID ms_tcpip6
} else {
  Write-Host -ForeGroundColor Magenta "Het script is afgebroken! Start het script opnieuw!"
  Exit
}


# Tijdzone
Write-Host -ForeGroundColor Green 'Tijdzone instellen op W. Europe Standard Time.'
$Null = Set-TimeZone -Name "W. Europe Standard Time"
# Opstarten van Server Manager uitschakelen
Write-Host -ForeGroundColor Green 'Het automatisch opstarten van Server Manager uitschakelen...' 
$Null = Get-ScheduledTask -TaskName ServerManager | Disable-ScheduledTask -Verbose
# Windows Updates uitzetten
Write-Host -ForeGroundColor Green 'Windows Updates uitschakelen...'
$Null = Get-Service -DisplayName "Windows Update" | Stop-Service
$Null = Set-Service -Name "wuauserv" -StartupType Disabled 
Start-Sleep 10

<#  Functie om na de reboot het vervolg-script aan te roepen #>
function Set-RunOnce
{
    [CmdletBinding()]
    param
    (
        #The Name of the Registry Key in the Autorun-Key.
        [string]
        $KeyName = 'Run',

        #Command to run
        [string]
        $Command = '%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -file c:\Scripts\2_InstallAD.ps1'
  
    ) 

     
    if (-not ((Get-Item -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce).$KeyName ))
    {
        New-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce' -Name $KeyName -Value $Command -PropertyType ExpandString
    }
    else
    {
        Set-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce' -Name $KeyName -Value $Command -PropertyType ExpandString
    }
}


# De Functie 'Set-RunOnce' aanroepen 
$Null = Set-RunOnce

# Automatisch inloggen na de reboot
#
#The purpose of this PowerShell script is to set the AutoLogon and WinLogon registry strings using PowerShell, to setup a Windows Server or Workstation for Automatic Logon.
#The $UserName and $Password variables need to be configured for your environment. 
#Please make sure the $UserName variables follows #DOMAINNAME\SAMACCOUNTNAME format.
#This needs to be run using an Elevated PowerShell ISE or PowerShell window (with Admin access on the computer you are running this on).
$RegistryLocation = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
Set-ItemProperty $RegistryLocation -Name 'AutoAdminLogon' -Value '1'  
Set-ItemProperty $RegistryLocation -Name 'DefaultUsername' -Value $username
Set-ItemProperty $RegistryLocation -Name 'DefaultPassword' -Value $password

# Hostname aanpassen en herstarten
Write-Host -ForeGroundColor Green 'Hostname aanpassen naar '$Hostname'...'
$Null = Rename-Computer  -NewName $HostName -PassThru -LocalCredential Administrator -Restart

 

PowerShell - Scherm leegmaken

Gegevens
Categorie: PowerShell
Gepubliceerd: 21 december 2022
Hits: 121

PowerShell cmdlet om het scherm leeg te maken:

Clear-Host

Main Menu

  • Home
  • Arduino
  • ESP8266
  • Microsoft SQL Server
  • Raspberry Pi
  • Maqueen V3.0
  • PowerShell
  • Proxmox
  • Home Assistant
  • Node-RED
  • Beleggen
  • Sparen
  • Traden - Handelen
  • Linux - Ubuntu
  • MySQL