Skip to content

Commit

Permalink
Windows Alternativen
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-b committed Apr 20, 2024
1 parent 3641f76 commit a756a08
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 0 deletions.
85 changes: 85 additions & 0 deletions 01-2-Cloudbase-init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## Übung 01-1: Infrastruktur als Code - Cloudbase-init

«Cloud-Init ist der Standard für die automatische Installation von Linux basierenden Systemen in der Cloud.

Cloudbase-Init™ ist das Windows-Äquivalent des Cloud-Init-Projekts.

Laut ChatGPT kann mittels folgendem PowerShell Script eine VM, inkl. Cloud-init Script, erstellt werden

# Variablen definieren
$VMName = "MeineCloudVM"
$VMPath = "C:\VMs\$VMName"
$VMVHDPath = "$VMPath\$VMName.vhdx"
$ISOPath = "C:\Pfad\zum\WindowsImage.iso"
$VMNetwork = "Default Switch" # Der Name des virtuellen Netzwerkschalters in Hyper-V

# Virtuelle Maschine erstellen
New-VM -Name $VMName -Path $VMPath -MemoryStartupBytes 4GB -NewVHDPath $VMVHDPath -NewVHDSizeBytes 60GB

# ISO-Datei als DVD-Laufwerk hinzufügen
Add-VMDvdDrive -VMName $VMName -Path $ISOPath

# Netzwerkkarte hinzufügen
Add-VMNetworkAdapter -VMName $VMName -SwitchName $VMNetwork

# VM starten
Start-VM -Name $VMName

# Warten bis die VM gestartet ist
Start-Sleep -Seconds 60

# Cloudbase-init Konfiguration
$CloudConfig = @"
[CloudbaseInit]
plugins=cloudbaseinit.plugins.windows.sethostname.SetHostNamePlugin,cloudbaseinit.plugins.common.sshpublickeys.SetUserSSHPublicKeysPlugin,cloudbaseinit.plugins.windows.createuser.CreateUserPlugin,cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin,cloudbaseinit.plugins.windows.winrmcertificateauth.CertificateAuthPlugin,cloudbaseinit.plugins.common.fileexec.FileExecPlugin,cloudbaseinit.plugins.windows.networkconfig.NetworkConfigPlugin,cloudbaseinit.plugins.windows.ntpclient.NtpClientPlugin
verbose=true
debug=true
username=Administrator
groups=Administrators
inject_user_password=true
bsdtar_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\bsdtar.exe
ntp_use_short_format=false
ntp_use_bundled_clock=true
ntp_servers=pool.ntp.org
ntp_retry_count=3
ntp_retry_interval=10
ntp_peer_delay=30
ntp_peer_timeout=10
bsdtar_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\bsdtar.exe
bsdtar_timeout=10
bsdtar_dest_folder=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\etc
bsdtar_cacert_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\etc\pki\cacert.pem
bsdtar_user_agent=Cloudbase-Init-UserAgent
logdir=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\log
logfile=cloudbase-init.log
delete_ssh_keys=false
bsdtar_timeout=300
bsdtar_dest_folder=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\etc
bsdtar_cacert_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\etc\pki\cacert.pem
bsdtar_user_agent=Cloudbase-Init-UserAgent
bsdtar_log_file=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\log\bsdtar.log
bsdtar_log_level=DEBUG
logrotate=true
logfile_maxsize=10
logfile_backup_count=5
retry_count=5
retry_count_retry_interval=10
retry_delay=1
ntp_use_bundled_clock=true
proxy=None
"@

# Cloudbase-init Konfiguration in die VM kopieren
Set-Content -Path "$VMPath\cloud-config.txt" -Value $CloudConfig

# Cloudbase-init starten
Invoke-Command -VMName $VMName -ScriptBlock {
# Cloudbase-init installieren
msiexec /i "C:\Program Files\Cloudbase Solutions\Cloudbase-Init\cloudbase-init-setup-1.1.0-x64.msi" /qn /L*v C:\Windows\Temp\cloudbase-init-setup.log

# Cloudbase-init konfigurieren
C:\Program Files\Cloudbase Solutions\Cloudbase-Init\Python\Scripts\cloudbase-init.exe --config-file=C:\cloud-config.txt --install
}

Write-Host "Die virtuelle Maschine '$VMName' wurde erstellt und Cloudbase-init wird konfiguriert."

43 changes: 43 additions & 0 deletions 01-3-powershell/CreateVM.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Funktion zur Erstellung einer virtuellen Maschine in Hyper-V
function Create-VM {
param (
[string]$VMName,
[string]$VHDPath,
[string]$SwitchName
)

$VMPath = "C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\"
$VHDFile = "$VMPath\$VMName.vhdx"
$VM = New-VM -Name $VMName -Path $VMPath -NewVHDPath $VHDFile -NewVHDSizeBytes 20GB
Add-VMNetworkAdapter -VMName $VMName -SwitchName $SwitchName
}

# Funktion zur Ausführung eines PowerShell-Skripts in einer VM
function Execute-ScriptInVM {
param (
[string]$VMName,
[string]$ScriptPath
)

$VM = Get-VM -Name $VMName
$VM | Start-VM
$VM | Get-VMConsole | Wait-VM
$Session = New-PSSession -VMName $VMName
Copy-Item -Path $ScriptPath -ToSession $Session -Destination "C:\"
Invoke-Command -Session $Session -ScriptBlock { Invoke-Expression -Command "C:\$(Split-Path -Leaf $using:ScriptPath)" }
Remove-PSSession -Session $Session
}

# Name der VM und des PowerShell-Skripts
$VMName = "TestVM"
$ScriptPath = "Install.ps1"
$boxPath = "C:\Users\Public\Documents\windows-10.box"

# Erstelle die VM in Hyper-V
Create-VM -VMName $VMName -VHDPath "$VMName.vhdx" -SwitchName "Default Switch"

# Warte einige Sekunden, damit die VM vollständig gestartet ist
Start-Sleep -Seconds 30

# Führe das PowerShell-Skript in der VM aus
Execute-ScriptInVM -VMName $VMName -ScriptPath $ScriptPath
79 changes: 79 additions & 0 deletions 01-3-powershell/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
## Übung 01-2: Windows PowerShell

PowerShell (auch Windows PowerShell und PowerShell Core) ist ein plattformübergreifendes Framework von Microsoft zur Automatisierung, Konfiguration und Verwaltung von Systemen, das einen Kommandozeileninterpreter inklusive Skriptsprache bietet.

Die Übungen finden in der [Windows PowerShell Umgebung](https://git-scm.com/downloads) als Administrator statt.

Ausserdem muss der Paket-Manager von Windows [winget](https://learn.microsoft.com/de-de/windows/package-manager/winget/#install-winget) installiert sein.

### Teil 1: winget Paket-Manager von Windows

Um diesen zu Testen installieren wir XML Notepad.

Öffnet Windows PowerShell, z.B. über das Windows Startmenü.

Führt folgenden Befehl aus:

winget install --id=Microsoft.XMLNotepad -e

Kontrolliert die Installation, in dem Ihr XML Notepad über das Windows Startmenü öffnet.

### Teil 2: Windows VM erstellen und XML Notepad installieren (von ChatGPT)

Dazu brauchen wir zuerst ein Boot Image für Hyper-V. Dieses kann z.B. mittels Azure Portal unter `Software` als ISO Image downgeladet werden.

Dann erstellen wir ein Installations Script `Install.ps1` mit folgendem Inhalt:

# Funktion zum Installieren von XML Notepad mit winget
function Install-XMLNotepad {
# Überprüfe, ob winget auf dem System verfügbar ist
if (-not (Get-Command "winget" -ErrorAction SilentlyContinue)) {
Write-Host "Error: Windows Package Manager (winget) is not installed or not available on this system."
return
}

# Installiere XML Notepad mit winget
winget install "Microsoft.XMLNotepad"
# Überprüfe, ob XML Notepad erfolgreich installiert wurde
if (Test-Path "$env:ProgramFiles\Microsoft XML Notepad\XMLNotepad.exe") {
Write-Host "XML Notepad wurde erfolgreich installiert."
} else {
Write-Host "Fehler: XML Notepad konnte nicht installiert werden."
}
}

# Installiere XML Notepad mit winget
Install-XMLNotepad

Anschliessend kann die VM Eingerichtet und Gestartet werden:


New-VM -Name "Windows10VM" -MemoryStartupBytes 4GB -BootDevice CD -SwitchName "Default Switch"
Add-VMDvdDrive -VMName "Windows10VM" -Path "C:\Users\Public\Documents\en-us_windows_10.iso"
Start-VM -Name "Windows10VM"

Get-VMConsole -Name "Windows10VM" | Wait-VM

$VM = Get-VM -Name "Windows10VM"
$VM | Start-VM
$VM | Get-VMConsole | Wait-VM

Eine Session zur VM aufgebaut werden und das Installationsscript `Install.ps1` ausgeführt werden.

$Session = New-PSSession -VMName "Windows10VM"
Copy-Item -Path $ScriptPath -ToSession $Session -Destination "C:\"
Invoke-Command -Session $Session -ScriptBlock { Invoke-Expression -Command "C:\$(Split-Path -Leaf $using:ScriptPath)" }
Remove-PSSession -Session $Session

### Alternative: Script von ChatGPT

ChatGPT bei weiteren Versuchen schlug ChatGPT das Script [CreateVM.ps1](CreateVM.ps1) vor.

Kann wie nachfolgende beschrieben Gestartet werden und erzeugt eine VM in Hyper-V ohne Boot Disk.

PowerShell.exe -ExecutionPolicy Bypass -File "CreateVM.ps1"




1 change: 1 addition & 0 deletions 01-4-vagrant/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.vagrant/
41 changes: 41 additions & 0 deletions 01-4-vagrant/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Übung 01-3: Infrastruktur als Code mit Vagrant

![](https://www.vagrantup.com/_next/image?url=https%3A%2F%2Fwww.datocms-assets.com%2F58478%2F1667241891-vagrant-illustration.png&w=3840&q=75)

Quelle: https://www.vagrantup.com/
- - -

[Vagrant](https://www.vagrantup.com/) ist für jedermann als einfachste und schnellste Möglichkeit zum Erstellen einer virtualisierten Umgebung konzipiert.

Die Übungen finden in der [Windows PowerShell Umgebung](https://git-scm.com/downloads) als Administrator statt.

Ausserdem muss das Produkt [Vagrant](https://www.vagrantup.com/) installiert sein.

Erstelle eine Datei `Vagrantfile` mit folgendem Inhalt:

Vagrant.configure("2") do |config|
# Verwende die Windows-Box
config.vm.box = "gusztavvargadr/windows-10-22h2-enterprise"
# Konfiguriere den Provider, zum Beispiel VirtualBox oder VMware
config.vm.provider "hyperv" do |vb|
vb.memory = "4096" # Setze den Arbeitsspeicher auf 4 GB
vb.cpus = 4 # Verwende 4 CPUs
end
# Führe das PowerShell-Skript aus
config.vm.provision "shell", inline: <<-SHELL
# Installiere XML Notepad über winget
winget install -e --id Microsoft.XMLNotepad
SHELL
end

Starte die VM mittels

vagrant up

Es sollte eine VM mit installiertem XML Notepad erstellt werden. User und Password sind `vagrant`.

Nach erfolgten Tests kann die VM wieder zerstört werden.

vagrant destroy
17 changes: 17 additions & 0 deletions 01-4-vagrant/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Vagrant.configure("2") do |config|
# Verwende die Windows-Box
config.vm.box = "gusztavvargadr/windows-10-22h2-enterprise"

# Konfiguriere den Provider, zum Beispiel VirtualBox oder VMware
config.vm.provider "hyperv" do |vb|
vb.memory = "4096" # Setze den Arbeitsspeicher auf 4 GB
vb.cpus = 4 # Verwende 4 CPUs
end

# Führe das PowerShell-Skript aus
config.vm.provision "shell", inline: <<-SHELL
# Installiere XML Notepad über winget
winget install -e --id Microsoft.XMLNotepad
SHELL
end

0 comments on commit a756a08

Please sign in to comment.