Listing 1: Systeminformationen einsammeln $info = Invoke-LabCommand -ActivityName 'Systeminformationen sammeln' -ScriptBlock { [PSCustomObject]@{ Computername = $env:COMPUTERNAME OS_Version = (Get-CimInstance Win32_OperatingSystem).Caption OS_Build = (Get-CimInstance Win32_OperatingSystem).BuildNumber Uptime_Tage = ((Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime).Days RAM_GB = [math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB, 2) Freier_Platz_C = [math]::Round((Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace / 1GB, 2) Prozessoren = (Get-CimInstance Win32_Processor).NumberOfCores } } -ComputerName (Get-LabVM) -PassThru Listing 2: IIS einspielen Invoke-LabCommand -ActivityName 'IIS konfigurieren' -ScriptBlock { # IIS-Rolle installieren $features = @( 'Web-Server', 'Web-WebServer', 'Web-Common-Http', 'Web-Default-Doc', 'Web-Dir-Browsing', 'Web-Http-Errors', 'Web-Static-Content', 'Web-Health', 'Web-Http-Logging', 'Web-Performance', 'Web-Stat-Compression', 'Web-Security', 'Web-Filtering', 'Web-Mgmt-Tools', 'Web-Mgmt-Console' ) foreach ($feature in $features) { Write-Host "Installiere Feature: $feature" Install-WindowsFeature -Name $feature -IncludeManagementTools } # Standard-Website stoppen Import-Module WebAdministration Stop-Website -Name "Default Web Site" # Benutzerdefinierte Website erstellen New-Website -Name "ContosoApp" ` -PhysicalPath "C:\inetpub\wwwroot" ` -Port 80 ` -Force Write-Host "IIS erfolgreich konfiguriert" } -ComputerName (Get-LabVM -Role WebServer) Listing 3: Datenbank und Tabellen anlegen Invoke-LabCommand -ActivityName 'Datenbank erstellen' -ScriptBlock { $query = @" CREATE DATABASE [$dbName]; GO USE [$dbName]; GO CREATE TABLE Customers ( CustomerID INT PRIMARY KEY IDENTITY(1,1), Name NVARCHAR(100) NOT NULL, Email NVARCHAR(100), CreatedDate DATETIME2 DEFAULT GETDATE() ); CREATE TABLE Orders ( OrderID INT PRIMARY KEY IDENTITY(1,1), CustomerID INT FOREIGN KEY REFERENCES Customers(CustomerID), OrderDate DATETIME2 DEFAULT GETDATE(), TotalAmount DECIMAL(10,2) ); "@ Invoke-Sqlcmd -Query $query -ServerInstance $env:COMPUTERNAME Write-Host "Datenbank $dbName erstellt" Listing 4: Datenbankbenutzer und Berechtigungen anlegen Invoke-LabCommand -ActivityName 'Datenbankbenutzer erstellen' -ScriptBlock { $query = @" IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE name = '$dbUser') BEGIN CREATE LOGIN [$dbUser] WITH PASSWORD = '$dbPassword'; END GO USE [$dbName]; GO IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = '$dbUser') BEGIN CREATE USER [$dbUser] FOR LOGIN [$dbUser]; END GO ALTER ROLE db_datareader ADD MEMBER [$dbUser]; ALTER ROLE db_datawriter ADD MEMBER [$dbUser]; GO "@ Invoke-Sqlcmd -Query $query -ServerInstance $env:COMPUTERNAME Write-Host "Benutzer $dbUser mit Berechtigungen erstellt" Listing 5: Prüfen der Konfiguration Invoke-LabCommand -ActivityName 'Datenbankverbindung testen' -ScriptBlock { $connectionString = "Server=$env:COMPUTERNAME;Database=$dbName;User Id=$dbUser; Password=$dbPassword;" try { $connection = New-Object System.Data.SqlClient.SqlConnection($connectionString) $connection.Open() Write-Host "Datenbankverbindung erfolgreich" -ForegroundColor Green $connection.Close() } catch { Write-Error "Verbindung fehlgeschlagen: $_" } } -ComputerName $sqlServer -Variable (Get-Variable dbName, dbUser, dbPassword)