https://github.com/green-fox-academy/safely-syllabus/tree/master/materialreview/scripts/powershell

https://github.com/green-fox-academy/safely-syllabus/tree/master/materialreview/powershell-commands

##### MatRev #####

ALAPOK

# This is a one line comment



#Strings
$aString = "Alma"
[string]$bstring = "Korte"

$aInt = 10
[int]$bint = 5

$aInt = $bInt
$aInt += $bInt
$aInt++
$aInt
$aInt = 10
#Airborne numerical values
$aFloat = 1.5
[float]$bFloat = 1.5

#$bFloat = $aFloat * $aInt
$bFloat = $bFloat * $aInt

$bFloat

#Blocks

$aArray= (1, 2, 3, 4, 5)
$bArray= (1..5)

$Global:aString = "Global string string string, for the whole script, with the definialast"

ElsoPS

param(
    [string]$Parameter1, # Parameter1 were created for .....
    [string]$Parameter2 # Parameter1 was generated for .....
)
[string]$Global:Parameter1=$Parameter1
$Global:Parameter2=$Parameter2

Set-StrictMode -Version 2.0 #warning
###############################################################################################
#function [string]aRutin([int]$iInt){
function aRutin([int]$iInt){
    
    return "S"
}
###############################################################################################
function bRutin($iInt){
    return "S"
}
###############################################################################################
#function [void]cRutin(){
function cRutin(){
    return "S"
}

function Ellenorzes(){
    if(($Global:Parameter1 -eq $null) -or ($Global:Parameter1 -eq ""))
    {
        Write-Host "Parameter1 ures!" -ForegroundColor Red
    }
    else
    {
        Write-Host $Global:Parameter1 -ForegroundColor Cyan
    }

    if(($Global:Parameter2 -ne $null) -and ($Global:Parameter2 -ne ""))
    {
        Write-Host $Global:Parameter2 -ForegroundColor Red
    }
    else
    {
        Write-Host "Parameter2 ures!" -ForegroundColor Cyan
    }
}


function bRutin2($iInt){
    for ($i = 1;$i -le $iInt;$i++)
    {
        Write-Host $i.ToString()
    }
    $i=1
    while ($i -le $iInt)
    {
        Write-Host $i.ToString()
        $i++
    }
    $i = 1
    do
    {
        Write-Host $i.ToString()
        $i++
    }
    while ($i -le $iInt)
    
    return $i

}
###############################################################################################
#####
####
## Main
####
####
###############################################################################################

CLS

# $cString = aRutin -iInt 10
[string]$aString = aRutin -iInt 5
$aString

Write-Host $Global:Parameter1 -ForegroundColor Cyan

Counterorzes

bRutin2 -iInt 5

Built-in variables

#Get-Help Write-Host
#Get-ChildItem | Get-Member

Get-Date -Format "yyyy-MM-dd"
Get-Date -Format "yyyy-MM-dd HH:mm:ss.ms"

$b =$env:COMPUTERNAME
$b

$files=Get-ChildItem -Path "C:\Users\MSI\AppData\Local\Temp" -Filter "*.tmp"
#$files | Select-Object -Property * | Select-Object -Last 5 | Select-Object -Property FullName,Length | Sort-Object Length -Descending
$files | Select-Object -Property * | Select-Object -First 5

$Files5biggest = $files | Sort-Object Length -Descending | Select-Object -First 5 | Sort-Object Name | Select-Object -Property FullName,Length
$Files5biggest | Format-Table -AutoSize

Get-Process | Select-Object -Property * | Select-Object -First 1

$ProcList = Get-Process
$SumPagedMemorySize = 0
foreach ($aProc in $ProcList)
{
    
    $SumPagedMemorySize += $aProc.PagedMemorySize

}
Write-Host $SumPagedMemorySize

File management

$Lines = Get-Content -Path "C:\Users\MSI\Documents\suli\mat review\batch\DeleteOldFiles.log"
$Lines.Length
$LogLines = ""
foreach($aLine in $Lines)
{
    If($aLine.Contains(".tmp"))
    {
        Write-Host $aLine
        $LogLines+=$aLine
        $LogLines+="`n"
    }
}

$LogLines | Out-File -FilePath "C:\Users\MSI\Documents\suli\mat review\PowerShell\___LogDeleteOldFiles.log"
$LogLines.Length

Copy-Item -Force "C:\Users\MSI\Documents\suli\mat review\PowerShell\___LogDeleteOldFiles.log" "C:\Users\MSI\Documents\suli\mat review\PowerShell\___LogDeleteOldFiles2.log"
Remove-Item -Force -Path "C:\Users\MSI\Documents\suli\mat review\PowerShell\___LogDeleteOldFiles2.log"
#Remove-Item -Force -Path "C:\Users\MSI\Documents\suli\mat review\PowerShell\___LogDeleteOldFiles2.log" -Filter "*.log"

#Move-Item -Force "C:\Users\MSI\Documents\suli\mat review\PowerShell\___LogDeleteOldFiles.log" "C:\Users\MSI\Documents\suli\mat review\PowerShell\___LogDeleteOldFiles2.log"


try
{
    $Lines = Get-Content -Path "C:\Users\MSI\Documents\suli\mat review\batch\DeleteOldFiles.log"
}
catch
{
    Write-Host "No such file" -ForegroundColor Red
}
finally
{
    Write-Host "Restart!" -ForegroundColor Cyan
}

Listing files

$SourceFolder = "C:\Users\MSI\Documents\suli\mat review\PowerShell"

$PDFfiles = Get-ChildItem -Path $SourceFolder -Filter "*.log"

#$PDFfiles | Select-Object -Property * -First 1

$PDFfiles5 = $PDFfiles | Sort-Object LastWriteTime | Select-Object -Last 5

$PDFfiles5s = $PDFfiles5 | Select-Object -Property Directory,Name,Length,LastWriteTime,LastAccessTime
Sort-Object -Property Name

$RiportFileName = $SourceFolder + "\PDFriport.csv"
$RiportFileName

$PDFfiles5s | Export-Csv -Path $RiportFileName -Delimiter ';'

Total memory of processes, number of processes

CLS

$Process = Get-Process
$SumMem = 0
$Count = 0
foreach ($aProcess in $Process)
{
    $SumMem += $Process.PagedMemorySize
    $Count++
}
Write-Host "Number of processes:" $Count 

###### WINDOWS admin project tasks ######

CREATE AD USER

$SamAccountName = "hende"
$Name = "Peter Hende"
$Firstname = "Peter"
$Surname = "Hende"
$Description = "Leendo sysadmin"
$City = "Budapest"
$StreetAddress = "Nyílvessző utca 30."
$Company = "HendeWEB"
$PasswordNeverExpires = "yes"
$AccountPassword = "Password1"
$AccountExpirationDate ="2050.05.05"
$Organization = "HendeWEB"
$Path = "IT"
$Path = "ou="+ $Path + ",dc=phende,dc=comp,dc=local"
if ($PasswordNeverExpires = "yes")
{
    $PasswordNeverExpires = $true
}else
{
    $PasswordNeverExpires = $false
}


New-ADUser -SamAccountName $SamAccountName -Name $Name -GivenName $FirstName -Surname $Surname -Description $Description -City $City -StreetAddress $StreetAddress -Company $Company -Organization $Organization -PasswordNeverExpires $PasswordNeverExpires -AccountPassword (ConvertTo-SecureString "$AccountPassword" -AsPlainText -Force) -AccountExpirationDate $AccountExpirationDate -Path $Path

CREATE GPO, GPO LINK (GPO registry)

$Name = "TestGPO"
$OU= "ou=IT,dc=phende,dc=comp,dc=local"

#$keyb = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop'
#$ValueNameb = 'NoChangingWallPaper'

New-GPO -name $Name
#Set-GPRegistryValue -Key $keyb -ValueName $ValueNamec -Type DWORD -Value 1 ` `
New-GPLink -Target $OU

1) On the S drive created during SQL, create a VM folder - we will do this later
use

###### WINDOWS admin project tasks ######

2) Install an AD with a domain name that follows the following naming convention:
first letter of first name+leadname+ comp.local That is, if your name is: Jakab Gipsz, then the domain
name: jgipsz.comp.local

Set-ExecutionPolicy Unrestricted -Force

Add-WindowsFeature AD-Domain-Services -IncludeManagementTools
$SecurePassword = convertto-securestring "sxCTo768" -asplaintext -force

Import module ADDSDeployment
Install-ADDSForest -SafeModeAdministratorPassword $SecurePassword `
-CreateDnsDelegation:$false `
-DatabasePath "C:\Windows\NTDS" `
-DomainMode "WinThreshold" `
-DomainName "phende.comp.local" `
-DomainNetbiosName "PHENDE" ` `
-ForestMode "WinThreshold" `
-InstallDns:$true `
-LogPath "C:\Windows\NTDS" `
-NoRebootOnCompletion:$false `
-SysvolPath "C:\Windows\SYSVOL" `
-Force:$true

3) DNS AD should be integrated


4) Create an IT OU, then two more in it: COMP and USR
a) Turn off inheritance on COMP OU

New-ADOrganizationalUnit IT
New-ADOrganizationalUnit USR -Path "OU=IT,DC=PHENDE,DC=COMP,DC=LOCAL"
New-ADOrganizationalUnit COMP -Path "OU=IT,DC=PHENDE,DC=COMP,DC=LOCAL"
Set-GPInheritance -Target "ou=COMP,ou=IT,dc=phende,dc=comp,dc=local" -IsBlocked Yes 

5) Create a GPO for the IT OU with the following settings
a) Restrict access to the Control Panel (create a user in AD
and set it to that user)
b) Disable the storage of the LAN Manager Hash.
c) Disable the forced system reboot (Windows Update).
d) Disable removable media drives (CD,DVD)
e) Disable the Guest account
f) Set the minimum password length to 10
g) Set the system to remember the last 6 passwords used (Password
history)
h) Create a shortcut on the desktop to open your favourite websites
i) Allow registry access
j) Set the browser (e.g. internet explorer) to open at google.com

$Name = "TestGPO"
$OU= "ou=IT,dc=phende,dc=comp,dc=local"

$keya = 'HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer'
$ValueNamea = 'Nocontrolpanel'

#$keyb = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop'
#$ValueNameb = 'NoChangingWallPaper'

$keyc = 'HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU'
$ValueNamec = "NoAutoBootWithLoggedOnUsers"

$keyd = 'HKLM\Software\Policies\Microsoft\Windows\RemovableStorageDevices\{53f56308-b6bf-11d0-94f2-00a0c91efb8b}'
$ValueNamed = "Deny_Execute"

#$keye = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop'
#$ValueNamee = 'NoChangingWallPaper'

#$keyf = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop'
#$ValueNamfe = 'NoChangingWallPaper'

#$keyg = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop'
#$ValueNameg = 'NoChangingWallPaper'

#$keyh = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop'
#$ValueNameh = 'NoChangingWallPaper'

#$keyi = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop'
#$ValueNamei = 'NoChangingWallPaper'

#$keyj = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop'
#$ValueNamej = 'NoChangingWallPaper'

# https://gpsearch.azurewebsites.net/#4847
# https://gpsearch.azurewebsites.net/Default_legacy.aspx

#new-gpo -name TestGPO | new-gplink -target "ou=marketing,dc=contoso,dc=com" | set-gppermissions -permissionlevel gpoedit -targetname "Marketing Admins" -targettype group



# Set-GPPermission -Name "ScreenSaverTimeOut" -TargetName "Petra" -TargetType User -PermissionLevel GPOApply



# new-gpo -name $Name | new-gplink -target "ou=$OU,dc=comp,dc=local"

#New-GPO -name $Name | Set-GPRegistryValue -Key $key `
#-ValueName $ValueName -Type DWORD -Value 1 | Set-GPPermissions -Replace `
#-PermissionLevel None -TargetName 'Authenticated Users' -TargetType group
#Set-GPPermissions -PermissionLevel gpoapply -TargetName 'Marketing Users' `
#-TargetType group | New-GPLink -Target 'OU=Marketing,DC=cpandl,DC=com' -Order 1


New-GPO -name $Name
Set-GPRegistryValue -Key $keya -ValueName $ValueNamea -Type DWORD -Value 1
#Set-GPRegistryValue -Key $keyb -ValueName $ValueNamec -Type DWORD -Value 1 | `
Set-GPRegistryValue -Key $keyc -ValueName $ValueNamec -Type DWORD -Value 1 | `
Set-GPRegistryValue -Key $keyyd -ValueName $ValueNamed -Type DWORD -Value 1 | `
#Set-GPRegistryValue -Key $keye -ValueName $ValueNamee -Type DWORD -Value 1 | `
#Set-GPRegistryValue -Key $keyf -ValueName $ValueNamef -Type DWORD -Value 1 | `
#Set-GPRegistryValue -Key $keyg -ValueName $ValueNameg -Type DWORD -Value 1 | `
#Set-GPRegistryValue -Key $keyyh -ValueName $ValueNameh -Type DWORD -Value 1 | `
#Set-GPRegistryValue -Key $keyi -ValueName $ValueNamei -Type DWORD -Value 1 | `
#Set-GPRegistryValue -Key $keyj -ValueName $ValueNamej -Type DWORD -Value 1 | `
New-GPLink -Target $OU

6) In DNS, add a CNAME record for the server (the name is optional)


Add-DnsServerResourceRecord -CName -Name "something" -HostNameAlias "gf-win-u03.phende.comp.local" -ZoneName "phende.comp.local"

# Add-DnsServerResourceRecord -CName -Name "hende" -HostNameAlias "gf-win-u03.phende.comp.local" -ZoneName "phende.comp.local" -AllowUpdateAny -TimeToLive 01:00:00
# Add-DnsServerResourceRecordCName -Name "hende" -HostNameAlias "gf-win-u03.phende.comp.local" -ZoneName "phende.comp.local"

7) Get the machine's arp table and MAC address

Get-NetNeighbor
GetMac

8) Install a DHCP server. Assume a network with 200 clients and 10 servers,
5 printers, 4 APs and 3 Layer 3 switches
a) Set the scope as above.
b) The scope should remain inactive

# https://learn.microsoft.com/en-us/windows-server/networking/technologies/dhcp/dhcp-deploy-wps
# https://argonsys.com/microsoft-cloud/library/how-to-manage-dhcp-using-powershell/
Install-WindowsFeature DHCP -IncludeManagementTools
netsh DHCP add SecurityGroups
Restart service dhcpserver

# Add-DhcpServerInDC -DnsName DHCP.phende.comp.local -IPAddress 192.168.1.6
# Get-DhcpServerInDC

Add-DhcpServerV4Scope -Name "DHCP Scope" -StartRange 192.168.1.1 -EndRange 192.168.1.222 -SubnetMask 255.255.255.0
Add-Dhcpserverv4ExclusionRange -ComputerName "phende.comp.local" -ScopeId 192.168.1.0 -StartRange 192.168.1.1 -EndRange 192.168.1.22
Set-DhcpServ4Scope -ComputerName "phende.comp.local" -ScopeId 192.168.1.0 -State InActive
# Remove-Dhcpserververv4ExclusionRange -ComputerName "phende.comp.local" -ScopeId 192.168.1.0 -EndRange 192.168.1.222
# Set-DhcpServerV4OptionValue -DnsServer 127.0.0.1 -Router 10.39.91.1
# Set-DhcpServ4Scope -ScopeId 127.0.0.1 -LeaseDuration 1.00:00:00