diff --git a/demos/Apache/Apache/Apache.psm1 b/demos/Apache/Apache/Apache.psm1 new file mode 100644 index 0000000000..f64cb42c87 --- /dev/null +++ b/demos/Apache/Apache/Apache.psm1 @@ -0,0 +1,161 @@ + +#Region utility functions +Function GetApacheCmd{ + if (Test-Path "/usr/sbin/apache2ctl"){ + $cmd = "/usr/sbin/apache2ctl" + }elseif(Test-Path "/usr/sbin/httpd"){ + $cmd = "/usr/sbin/httpd" + }else{ + Write-Error "Unable to find httpd or apache2ctl program. Unable to continue" + exit -1 + } + $cmd +} + +Function GetApacheVHostDir{ + if (Test-Path "/etc/httpd/conf.d"){ + $VHostsDirectory = "/etc/httpd/conf.d/" + } + if (Test-Path "/etc/apache2/sites-enabled"){ + $VHostsDirectory = "/etc/apache2/sites-enabled" + }else{ + $VHostsDirectory = $null + } + $VHostsDirectory +} + +Function CleanInputString([string]$inputStr){ + $outputStr = $inputStr.Trim().Replace('`n','').Replace('\n','') + $outputStr +} + +#EndRegion utilty functions + +#Region Class specifications + + +Class ApacheVirtualHost{ + [string]$ServerName + [string]$DocumentRoot + [string]$VirtualHostIPAddress = "*" + [string[]]$ServerAliases + [string]$VirtualHostPort = "80" + [string]$ServerAdmin + [string]$CustomLogPath + [string]$ErrorLogPath + [string]$ConfigurationFile + + #region class constructors + #endregion + + #region class methods + Save($ConfigurationFile){ + if (!(Test-Path $this.DocumentRoot)){ New-Item -type Directory $this.DocumentRoot } + + $VHostsDirectory = GetApacheVHostDir + if (!(Test-Path $VHostsDirectory)){ + Write-Error "Specified virtual hosts directory does not exist: $VHostsDirectory" + exit 1 + } + $vHostDef = "`n" + $vHostDef += "DocumentRoot " + $this.DocumentRoot + "`n" + ForEach ($Alias in $this.ServerAliases){ + $vHostDef += "ServerAlias " + $Alias + "`n" + } + if ($this.ServerAdmin.Length -gt 1){$vHostDef += "ServerAdmin " + $this.ServerAdmin +"`n"} + if ($this.CustomLogPath -like "*/*"){$vHostDef += "CustomLog " + $this.CustomLogPath +"`n"} + if ($this.ErrorLogPath -like "*/*"){$vHostDef += "ErrorLog " + $this.ErrorLogpath +"`n"} + $vHostDef += "" + $filName = $ConfigurationFile + $VhostDef |out-file "${VHostsDirectory}/${filName}" -Force -Encoding:ascii + Write-Information "Restarting Apache HTTP Server" + Restart-ApacheHTTPServer + } + + #endregion +} + +#EndRegion Class Specifications + +Function New-ApacheVHost { + [CmdletBinding()] + param ( + [parameter (Mandatory = $true)][string]$ServerName, + [parameter (Mandatory = $true)][string]$DocumentRoot, + [string]$VirtualHostIPAddress, + [string[]]$ServerAliases, + [string]$VirtualHostPort, + [string]$ServerAdmin, + [string]$CustomLogPath, + [string]$ErrorLogPath + ) + + $newVHost = [ApacheVirtualHost]::new() + $newVHost.ServerName = $ServerName + $newVHost.DocumentRoot = $DocumentRoot + $newVHost.ServerAliases = $ServerAliases + if ($VirtualHostIPAddress){$newVHost.VirtualHostIPAddress = $VirtualHostIPAddress} + if ($VirtualHostPort){$newVHost.VirtualHostPort = $VirtualHostPort} + $newVHost.ServerAdmin = $ServerAdmin + $newVHost.CustomLogPath = $CustomLogPath + $newVHost.ErrorLogPath = $ErrorLogPath + $newVHost.Save("$ServerName.conf") +} + +Function Get-ApacheVHost{ + $cmd = GetApacheCmd + + $Vhosts = @() + $res = & $cmd -t -D DUMP_VHOSTS + + ForEach ($line in $res){ + $ServerName = $null + if ($line -like "*:*.conf*"){ + #$vhobject = New-VHostObj + $RMatch = $line -match "(?.*:[0-9]*)(?.*)\((?.*)\)" + $ListenAddress = $Matches.Listen.trim() + $ServerName = $Matches.ServerName.trim() + $ConfFile = $Matches.ConfFile.trim() + }else{ + if ($line.trim().split()[0] -like "*:*"){ + $ListenAddress = $line.trim().split()[0] + }elseif($line -like "*.conf*"){ + if ($line -like "*default*"){ + $ServerName = "_Default" + $ConfFile = $line.trim().split()[3].split(":")[0].Replace('(','') + }elseif($line -like "*namevhost*"){ + $ServerName = $line.trim().split()[3] + $ConfFile = $line.trim().split()[4].split(":")[0].Replace('(','') + } + } + } + + if ($ServerName -ne $null){ + $vHost = [ApacheVirtualHost]::New + $vHost.ServerName = $ServerName + $vHost.ConfFile = $ConfFile + $vHost.VirtualHostIPAddress = $ListenAddress.Split(":")[0] + $vHost.VirtualHostPort = $ListenAddress.Split(":")[1] + $Vhosts += $vHost + } + } + Return $Vhosts + } + +Function Restart-ApacheHTTPServer{ + [CmdletBinding()] + Param( + [switch]$Graceful + ) + + if ($Graceful -eq $null){$Graceful = $fase} + $cmd = GetApacheCmd + if ($Graceful){ + & $cmd -k graceful + }else{ + & $cmd -k restart + } + +} + + diff --git a/demos/Apache/apache-demo.ps1 b/demos/Apache/apache-demo.ps1 new file mode 100644 index 0000000000..d56088eec6 --- /dev/null +++ b/demos/Apache/apache-demo.ps1 @@ -0,0 +1,16 @@ +ipmo Apache + +#list Apache Modules +Get-ApacheModules |Where {$_.Module -like "*proxy*"}|Sort-Object Module + +#Graceful restart of Apache +Restart-ApacheHTTPServer -graceful + +#Enumerate current virtual hosts (web sites) +Get-ApacheVHost + +#Add a new virtual host +New-ApacheVHost -ServerName "mytestserver" -DocumentRoot /var/www/html/mystestserver -VirtualHostIPAddress * -VirtualHostPort 8090 + +#Enumerate new set of virtual hosts +Get-ApacheVHost diff --git a/demos/Apache/readme.md b/demos/Apache/readme.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/demos/SystemD/SystemD/SystemD.psm1 b/demos/SystemD/SystemD/SystemD.psm1 new file mode 100644 index 0000000000..2bf406a0df --- /dev/null +++ b/demos/SystemD/SystemD/SystemD.psm1 @@ -0,0 +1,17 @@ +Function Get-SystemDJournal { + [CmdletBinding()] + param ( + [Alias("args")][string]$journalctlParameters + ) + $cmd = "journalctl" + $Result = & $cmd $journalctlParameters -o json --no-pager + Try + { + $JSONResult = $Result|ConvertFrom-JSON + $JSONResult + } + Catch + { + $Result + } +} \ No newline at end of file diff --git a/demos/SystemD/journalctl-demo.ps1 b/demos/SystemD/journalctl-demo.ps1 new file mode 100644 index 0000000000..704ba558ad --- /dev/null +++ b/demos/SystemD/journalctl-demo.ps1 @@ -0,0 +1,6 @@ + +#list recent journal events +Get-SystemDJournal -args "-xe" + +#Drill into SystemD unit messages +Get-SystemDJournal -args "-xe" |where {$_._SYSTEMD_UNIT -like "*.service"} |ft _SYSTEMD_UNIT, MESSAGE |select-object -first 10 diff --git a/demos/SystemD/readme.md b/demos/SystemD/readme.md new file mode 100644 index 0000000000..4997d8fcdf --- /dev/null +++ b/demos/SystemD/readme.md @@ -0,0 +1,4 @@ +#Journalctl (JSON parsing) demo +Requires: +-SystemD-based system (RHEL/CentOS 7, Ubuntu 16.04) +-Privileges (launch powershell with sudo) \ No newline at end of file diff --git a/demos/modules/ApacheHTTPServer/ApacheVirtualHosts.psm1 b/demos/modules/ApacheHTTPServer/ApacheVirtualHosts.psm1 deleted file mode 100644 index 94710c8130..0000000000 --- a/demos/modules/ApacheHTTPServer/ApacheVirtualHosts.psm1 +++ /dev/null @@ -1,173 +0,0 @@ -$moduleRoot = Split-Path -Path $MyInvocation.MyCommand.Path - - -Function New-ApacheVHost { - [CmdletBinding()] - param ( - [parameter (Mandatory = $true)][string]$ServerName, - [string]$VHostsDirectory, - [parameter (Mandatory = $true)][string]$DocumentRoot, - [parameter (Mandatory = $true)][string]$VirtualHostIPAddress, - [string[]]$ServerAliases, - [parameter (Mandatory = $true)][string]$VirtualHostPort, - [string]$ServerAdmin, - [string]$CustomLogPath, - [string]$ErrorLogPath - ) - - if ($VHostsDirectory -notlike "*/*"){ - #Try to find default - if (Test-Path "/etc/httpd/conf.d"){ - $VHostsDirectory = "/etc/httpd/conf.d/" - } - if (Test-Path "/etc/apache2/sites-enabled"){ - $VHostsDirectory = "/etc/apache2/sites-enabled" - } - } - - if (!(Test-Path $VHostsDirectory)){ - Write-Error "Specified virtual hosts directory does not exist: $VHostsDirectory" - exit 1 - } - - $vHostDef = ".*:[0-9]*)(?.*)\((?.*)\)" - $VHostProps = $matches - $vhobject |Add-Member -type NoteProperty -name "ServerName" -value $matches.ServerName.Trim() - $vhobject |Add-Member -type NoteProperty -name "ListenAddress" -value $matches.Listen.Trim() - $vhobject |Add-Member -type NoteProperty -name "ConfFile" -value $matches.ConfFile.Trim() - $Vhosts += $vhobject - } - - } - - Return $Vhosts - -} - -Function Restart-ApacheHTTPServer{ - [CmdletBinding()] - Param( - [boolean]$Graceful - ) - - if ($Graceful -eq $null){$Graceful = $fase} - - $cmd = $null - if (Test-Path "/usr/sbin/apache2ctl"){ - $cmd = "/usr/sbin/apache2ctl" - }elseif(Test-Path "/usr/sbin/httpd"){ - $cmd = "/usr/sbin/httpd" - } - - if ($cmd -eq $null){ - Write-Error "Unable to find httpd/apache2ctl command" - exit -1 - } - - if ($Graceful){ - $cmd = $cmd += " -k graceful" - }else{ - $cmd = $cmd += " -k restart" - } - - Invoke-Expression $Cmd -} - -Function Get-ApacheModules{ - $cmd = $null - if (Test-Path "/usr/sbin/apache2ctl"){ - $cmd = "/usr/sbin/apache2ctl" - }elseif(Test-Path "/usr/sbin/httpd"){ - $cmd = "/usr/sbin/httpd" - } - - if ($cmd -eq $null){ - Write-Error "Unable to find httpd/apache2ctl command" - exit -1 - } - - $cmd = $cmd += " -M |grep -v Loaded" - - $ApacheModules = @() - function New-ModuleObj { - New-Object PSObject -Property @{} - } - - - $Results = Invoke-Expression $cmd - Foreach ($mod in $Results){ - $modObj = New-ModuleObj - $modObj | Add-Member -type NoteProperty -name "Module" -value $mod.trim() - $ApacheModules += $modObj - } - - Return $ApacheModules - -}