<# --------------------------------------------------------------------------- Module......: Utilities.psm1 Projekt.....: Veeam Enterprise Installer Version.....: 2.0.0 Beschreibung Allgemeine Hilfsfunktionen. Enthält: • Dateioperationen • Hashprüfung • Speicherprüfung • Registry Funktionen • Service Funktionen • Netzwerkprüfung • Versionsvergleich ---------------------------------------------------------------------------#> Set-StrictMode -Version Latest #region Variablen $Script:UtilityVersion = "2.0.0" #endregion ############################################################ # Test-PathExists ############################################################ function Test-PathExists { <# .SYNOPSIS Prüft ob ein Pfad existiert. #> param( [Parameter(Mandatory)] [string]$Path ) return ( Test-Path ` -Path $Path ) } ############################################################ # New-Folder ############################################################ function New-Folder { <# .SYNOPSIS Erstellt einen Ordner. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { New-Item ` -Path $Path ` -ItemType Directory ` -Force | Out-Null } return $Path } ############################################################ # Remove-FolderSafe ############################################################ function Remove-FolderSafe { <# .SYNOPSIS Löscht Ordner sicher. #> param( [Parameter(Mandatory)] [string]$Path ) if(Test-Path $Path) { Remove-Item ` -Path $Path ` -Recurse ` -Force } } ############################################################ # Copy-FileSafe ############################################################ function Copy-FileSafe { <# .SYNOPSIS Kopiert Dateien mit Prüfung. #> param( [Parameter(Mandatory)] [string]$Source, [Parameter(Mandatory)] [string]$Destination ) if(!(Test-Path $Source)) { throw ` "Quelldatei nicht gefunden: $Source" } $TargetFolder = Split-Path ` $Destination ` -Parent if( ![string]::IsNullOrWhiteSpace($TargetFolder) -and !(Test-Path $TargetFolder) ) { New-Folder ` -Path $TargetFolder } Copy-Item ` -Path $Source ` -Destination $Destination ` -Force return ( Test-Path $Destination ) } #Beginn PART 2 ############################################################ # Get-FileHashValue ############################################################ function Get-FileHashValue { <# .SYNOPSIS Ermittelt den Hash einer Datei. #> param( [Parameter(Mandatory)] [string]$Path, [ValidateSet( "SHA1", "SHA256", "SHA384", "SHA512" )] [string]$Algorithm = "SHA256" ) if(!(Test-Path -Path $Path -PathType Leaf)) { throw ` "Datei nicht gefunden: $Path" } ( Get-FileHash ` -Path $Path ` -Algorithm $Algorithm ).Hash } ############################################################ # Compare-FileHash ############################################################ function Compare-FileHash { <# .SYNOPSIS Vergleicht Dateihashes. #> param( [Parameter(Mandatory)] [string]$File1, [Parameter(Mandatory)] [string]$File2 ) $Hash1 = Get-FileHashValue ` -Path $File1 $Hash2 = Get-FileHashValue ` -Path $File2 return ( $Hash1 -eq $Hash2 ) } ############################################################ # Get-FileInformation ############################################################ function Get-FileInformation { <# .SYNOPSIS Liefert Dateiinformationen. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path -Path $Path -PathType Leaf)) { return $null } $File = Get-Item ` $Path [PSCustomObject]@{ Name = $File.Name FullName = $File.FullName SizeMB = [math]::Round( $File.Length / 1MB, 2 ) Created = $File.CreationTime Modified = $File.LastWriteTime Hash = Get-FileHashValue ` -Path $Path } } ############################################################ # Get-DiskInformation ############################################################ function Get-DiskInformation { <# .SYNOPSIS Liefert Laufwerksinformationen. #> Get-CimInstance ` Win32_LogicalDisk ` -Filter "DriveType=3" | Select-Object ` DeviceID, VolumeName, @{ Name="SizeGB" Expression={ [math]::Round( $_.Size / 1GB, 2 ) } }, @{ Name="FreeGB" Expression={ [math]::Round( $_.FreeSpace / 1GB, 2 ) } } } ############################################################ # Test-FreeSpace ############################################################ function Test-FreeSpace { <# .SYNOPSIS Prüft freien Speicherplatz. #> param( [Parameter(Mandatory)] [string]$Drive, [int]$MinimumGB = 20 ) $Disk = Get-CimInstance ` Win32_LogicalDisk ` -Filter "DeviceID='$Drive'" if($null -eq $Disk) { return $false } $FreeGB = $Disk.FreeSpace / 1GB return ( $FreeGB -ge $MinimumGB ) } #Beginn PART 3 ############################################################ # Get-RegistryValue ############################################################ function Get-RegistryValue { <# .SYNOPSIS Liest einen Registrywert. #> param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Name ) try { $Item = Get-ItemProperty ` -Path $Path ` -ErrorAction Stop return $Item.$Name } catch { return $null } } ############################################################ # Test-RegistryPath ############################################################ function Test-RegistryPath { <# .SYNOPSIS Prüft Registrypfad. #> param( [Parameter(Mandatory)] [string]$Path ) return ( Test-Path ` -Path $Path ) } ############################################################ # Get-InstalledSoftware ############################################################ function Get-InstalledSoftware { <# .SYNOPSIS Listet installierte Programme. #> $RegistryPaths = @( "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" ) foreach($RegistryPath in $RegistryPaths) { if(Test-Path $RegistryPath) { Get-ItemProperty ` $RegistryPath | Where-Object { $_.DisplayName } | Select-Object ` DisplayName, DisplayVersion, Publisher, InstallDate } } } ############################################################ # Test-SoftwareInstalled ############################################################ function Test-SoftwareInstalled { <# .SYNOPSIS Prüft ob Software installiert ist. #> param( [Parameter(Mandatory)] [string]$Name ) $Software = Get-InstalledSoftware foreach($Entry in $Software) { if( $Entry.DisplayName ` -like "*$Name*" ) { return $true } } return $false } ############################################################ # Get-WindowsFeatureState ############################################################ function Get-WindowsFeatureState { <# .SYNOPSIS Prüft Windows Features. #> param( [Parameter(Mandatory)] [string]$FeatureName ) try { Get-WindowsFeature ` -Name $FeatureName } catch { return $null } } ############################################################ # Test-WindowsFeatureInstalled ############################################################ function Test-WindowsFeatureInstalled { <# .SYNOPSIS Prüft Feature Status. #> param( [Parameter(Mandatory)] [string]$FeatureName ) $Feature = Get-WindowsFeatureState ` -FeatureName $FeatureName if($null -eq $Feature) { return $false } return ( $Feature.InstallState ` -eq "Installed" ) } #Beginn PART 4 ############################################################ # Get-ServiceInformation ############################################################ function Get-ServiceInformation { <# .SYNOPSIS Liefert Informationen zu einem Dienst. #> param( [Parameter(Mandatory)] [string]$Name ) try { Get-Service ` -Name $Name ` -ErrorAction Stop } catch { return $null } } ############################################################ # Test-ServiceExists ############################################################ function Test-ServiceExists { <# .SYNOPSIS Prüft ob Dienst vorhanden ist. #> param( [Parameter(Mandatory)] [string]$Name ) return ( $null -ne ( Get-ServiceInformation ` -Name $Name ) ) } ############################################################ # Test-ServiceRunning ############################################################ function Test-ServiceRunning { <# .SYNOPSIS Prüft Dienststatus. #> param( [Parameter(Mandatory)] [string]$Name ) $Service = Get-ServiceInformation ` -Name $Name if($null -eq $Service) { return $false } return ( $Service.Status ` -eq "Running" ) } ############################################################ # Start-ServiceSafe ############################################################ function Start-ServiceSafe { <# .SYNOPSIS Startet Dienst sicher. #> param( [Parameter(Mandatory)] [string]$Name ) if(Test-ServiceExists $Name) { Start-Service ` -Name $Name ` -ErrorAction Stop } } ############################################################ # Stop-ServiceSafe ############################################################ function Stop-ServiceSafe { <# .SYNOPSIS Stoppt Dienst sicher. #> param( [Parameter(Mandatory)] [string]$Name ) if(Test-ServiceExists $Name) { Stop-Service ` -Name $Name ` -Force ` -ErrorAction Stop } } ############################################################ # Set-ServiceStartupType ############################################################ function Set-ServiceStartupType { <# .SYNOPSIS Ändert Starttyp. #> param( [Parameter(Mandatory)] [string]$Name, [ValidateSet( "Automatic", "Manual", "Disabled" )] [Parameter(Mandatory)] [string]$StartupType ) Set-Service ` -Name $Name ` -StartupType $StartupType } ############################################################ # Test-PendingReboot ############################################################ function Test-PendingReboot { <# .SYNOPSIS Prüft ob Neustart erforderlich ist. #> $Paths = @( "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending", "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" ) foreach($Path in $Paths) { if(Test-Path $Path) { return $true } } return $false } #Beginn PART 5 ############################################################ # Test-NetworkConnection ############################################################ function Test-NetworkConnection { <# .SYNOPSIS Prüft Netzwerkverbindung. #> param( [Parameter(Mandatory)] [string]$ComputerName ) Test-Connection ` -ComputerName $ComputerName ` -Count 2 ` -Quiet } ############################################################ # Test-TCPPort ############################################################ function Test-TCPPort { <# .SYNOPSIS Prüft TCP Port Erreichbarkeit. #> param( [Parameter(Mandatory)] [string]$ComputerName, [Parameter(Mandatory)] [ValidateRange(1, 65535)] [int]$Port, [ValidateRange(100, 60000)] [int]$TimeoutMilliseconds = 5000 ) $Client = $null try { $Client = New-Object ` System.Net.Sockets.TcpClient $AsyncResult = $Client.BeginConnect( $ComputerName, $Port, $null, $null ) if(!$AsyncResult.AsyncWaitHandle.WaitOne($TimeoutMilliseconds, $false)) { return $false } $Client.EndConnect($AsyncResult) return $true } catch { return $false } finally { if($null -ne $Client) { $Client.Close() } } } ############################################################ # Resolve-DNSNameSafe ############################################################ function Resolve-DNSNameSafe { <# .SYNOPSIS DNS Auflösung. #> param( [Parameter(Mandatory)] [string]$Name ) try { Resolve-DnsName ` -Name $Name ` -ErrorAction Stop } catch { return $null } } ############################################################ # Get-NetworkInformation ############################################################ function Get-NetworkInformation { <# .SYNOPSIS Liefert Netzwerkinformationen. #> Get-NetIPAddress | Where-Object { $_.AddressFamily -eq "IPv4" } | Select-Object ` InterfaceAlias, IPAddress, PrefixLength } ############################################################ # Compare-Version ############################################################ function Compare-Version { <# .SYNOPSIS Vergleicht Versionsnummern. #> param( [Parameter(Mandatory)] [string]$Current, [Parameter(Mandatory)] [string]$Required ) try { $CurrentVersion = [version]$Current $RequiredVersion = [version]$Required return ( $CurrentVersion -ge $RequiredVersion ) } catch { return $false } } ############################################################ # Get-OperatingSystemVersion ############################################################ function Get-OperatingSystemVersion { <# .SYNOPSIS Liefert Windows Version. #> $OS = Get-CimInstance ` Win32_OperatingSystem [PSCustomObject]@{ Caption = $OS.Caption Version = $OS.Version Build = $OS.BuildNumber } } ############################################################ # Wait-Seconds ############################################################ function Wait-Seconds { <# .SYNOPSIS Kontrollierte Wartezeit. #> param( [int]$Seconds = 5 ) Start-Sleep ` -Seconds $Seconds } #Beginn PART 6 ############################################################ # Get-SystemInformation ############################################################ function Get-SystemInformation { <# .SYNOPSIS Liefert zentrale Systeminformationen. #> $OS = Get-CimInstance ` Win32_OperatingSystem $CPU = Get-CimInstance ` Win32_Processor $Memory = Get-CimInstance ` Win32_ComputerSystem [PSCustomObject]@{ ComputerName = $env:COMPUTERNAME User = $env:USERNAME Domain = $env:USERDOMAIN OperatingSystem = $OS.Caption Version = $OS.Version Build = $OS.BuildNumber CPU = $CPU.Name MemoryGB = [math]::Round( $Memory.TotalPhysicalMemory / 1GB, 2 ) PowerShell = $PSVersionTable.PSVersion.ToString() Time = Get-Date } } ############################################################ # Get-PowerShellInformation ############################################################ function Get-PowerShellInformation { [PSCustomObject]@{ Version = $PSVersionTable.PSVersion Edition = $PSVersionTable.PSEdition OS = [Environment]::OSVersion.VersionString } } ############################################################ # Request-Reboot ############################################################ function Request-Reboot { <# .SYNOPSIS Plant Neustart. #> param( [switch]$Force ) if($Force) { Restart-Computer ` -Force } else { Restart-Computer } } ############################################################ # Test-Elevation ############################################################ function Test-Elevation { <# .SYNOPSIS Prüft Administratorrechte. #> $Identity = [Security.Principal.WindowsIdentity]::GetCurrent() $Principal = New-Object ` Security.Principal.WindowsPrincipal( $Identity ) return ( $Principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) ) } ############################################################ # Get-UtilityInformation ############################################################ function Get-UtilityInformation { [PSCustomObject]@{ Module = "Utilities.psm1" Version = $Script:UtilityVersion Loaded = $true } } ############################################################ # Export Module Functions ############################################################ Export-ModuleMember ` -Function ` Test-PathExists, New-Folder, Remove-FolderSafe, Copy-FileSafe, Get-FileHashValue, Compare-FileHash, Get-FileInformation, Get-DiskInformation, Test-FreeSpace, Get-RegistryValue, Test-RegistryPath, Get-InstalledSoftware, Test-SoftwareInstalled, Get-WindowsFeatureState, Test-WindowsFeatureInstalled, Get-ServiceInformation, Test-ServiceExists, Test-ServiceRunning, Start-ServiceSafe, Stop-ServiceSafe, Set-ServiceStartupType, Test-PendingReboot, Test-NetworkConnection, Test-TCPPort, Resolve-DNSNameSafe, Get-NetworkInformation, Compare-Version, Get-OperatingSystemVersion, Wait-Seconds, Get-SystemInformation, Get-PowerShellInformation, Request-Reboot, Test-Elevation, Get-UtilityInformation ############################################################ # Ende Utilities.psm1 ############################################################