mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
add Samples folder
This commit is contained in:
1 parent
0c38f29d68
commit
73dc2fd4d4
5 files changed
+432
No files matched your search
@@ -0,0 +1,173 @@
|
|||||||
|
$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 = "<VirtualHost "
|
||||||
|
if ($VirtualHostIPAddress -ne $null){
|
||||||
|
$vHostDef += $VirtualHostIPAddress
|
||||||
|
}
|
||||||
|
if ($VirtualHostPort -ne $null){
|
||||||
|
$vHostDef += ":$VirtualHostPort"
|
||||||
|
}
|
||||||
|
$vHostDef += ">"
|
||||||
|
|
||||||
|
$vHostDef += "`n"
|
||||||
|
$vHostDef += "DocumentRoot " + $DocumentRoot + "`n"
|
||||||
|
$vHostDef += "ServerName " + $ServerName + "`n"
|
||||||
|
if ($ServerAliases -ne $null){
|
||||||
|
ForEach ($Alias in $ServerAliases){
|
||||||
|
$vHostDef += "ServerAlias " + $Alias + "`n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($ServerAdmin.Length -gt 1){
|
||||||
|
$vHostDef += "ServerAdmin " + $ServerAdmin +"`n"
|
||||||
|
}
|
||||||
|
if ($CustomLogPath -like "*/*"){
|
||||||
|
$vHostDef += "CustomLog " + $CustomLogPath +"`n"
|
||||||
|
}
|
||||||
|
if ($ErrorLogPath -like "*/*"){
|
||||||
|
$vHostDef += "ErrorLog " + $ErrorLogpath +"`n"
|
||||||
|
}
|
||||||
|
$vHostDef += "</VirtualHost>"
|
||||||
|
$filName = $ServerName + ".conf"
|
||||||
|
Write-Output "Writing $filName to $VHostsDirectory"
|
||||||
|
$VhostDef |out-file "$VHostsDirectory/$filName" -Force -Encoding:ascii
|
||||||
|
|
||||||
|
if (Test-Path "/usr/sbin/apache2ctl"){
|
||||||
|
$cmd = "/usr/sbin/apache2ctl"
|
||||||
|
}elseif(Test-Path "/usr/sbin/httpd"){
|
||||||
|
$cmd = "/usr/sbin/httpd"
|
||||||
|
}
|
||||||
|
$cmd += " -k restart"
|
||||||
|
write-output "Restarting Apache HTTP daemon"
|
||||||
|
Invoke-expression $cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
Function Get-ApacheVHosts{
|
||||||
|
$cmd = $null
|
||||||
|
if (Test-Path "/usr/sbin/apache2ctl"){
|
||||||
|
$cmd = "/usr/sbin/apache2ctl"
|
||||||
|
}elseif(Test-Path "/usr/sbin/httpd"){
|
||||||
|
$cmd = "/usr/sbin/httpd"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function New-VHostObj {
|
||||||
|
New-Object PSObject -Property @{}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if ($cmd -eq $null){
|
||||||
|
Write-Error "Unable to find httpd/apache2ctl command"
|
||||||
|
exit -1
|
||||||
|
}
|
||||||
|
|
||||||
|
$cmd = $cmd += " -t -D DUMP_VHOSTS"
|
||||||
|
$Vhosts = @()
|
||||||
|
$res = Invoke-expression $cmd
|
||||||
|
ForEach ($line in $res){
|
||||||
|
if ($line -like "*.conf*"){
|
||||||
|
$vhobject = New-VHostObj
|
||||||
|
$RMatch = $line -match "(?<Listen>.*:[0-9]*)(?<ServerName>.*)\((?<ConfFile>.*)\)"
|
||||||
|
$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
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
|
||||||
|
<Configuration>
|
||||||
|
<ViewDefinitions>
|
||||||
|
<View>
|
||||||
|
<Name>Default</Name>
|
||||||
|
<ViewSelectedBy>
|
||||||
|
<TypeName>CronJob</TypeName>
|
||||||
|
</ViewSelectedBy>
|
||||||
|
<TableControl>
|
||||||
|
<TableHeaders>
|
||||||
|
<TableColumnHeader>
|
||||||
|
<Label>Minute</Label>
|
||||||
|
<Width>10</Width>
|
||||||
|
<Alignment>Left</Alignment>
|
||||||
|
</TableColumnHeader>
|
||||||
|
<TableColumnHeader>
|
||||||
|
<Label>Hour</Label>
|
||||||
|
<Width>10</Width>
|
||||||
|
<Alignment>Left</Alignment>
|
||||||
|
</TableColumnHeader>
|
||||||
|
<TableColumnHeader>
|
||||||
|
<Label>DayOfMonth</Label>
|
||||||
|
<Width>10</Width>
|
||||||
|
<Alignment>Left</Alignment>
|
||||||
|
</TableColumnHeader>
|
||||||
|
<TableColumnHeader>
|
||||||
|
<Label>Month</Label>
|
||||||
|
<Width>10</Width>
|
||||||
|
<Alignment>Left</Alignment>
|
||||||
|
</TableColumnHeader>
|
||||||
|
<TableColumnHeader>
|
||||||
|
<Label>DayOfWeek</Label>
|
||||||
|
<Width>10</Width>
|
||||||
|
<Alignment>Left</Alignment>
|
||||||
|
</TableColumnHeader>
|
||||||
|
<TableColumnHeader>
|
||||||
|
<Label>Command</Label>
|
||||||
|
<Alignment>Left</Alignment>
|
||||||
|
</TableColumnHeader>
|
||||||
|
</TableHeaders>
|
||||||
|
<TableRowEntries>
|
||||||
|
<TableRowEntry>
|
||||||
|
<TableColumnItems>
|
||||||
|
<TableColumnItem>
|
||||||
|
<PropertyName>Minute</PropertyName>
|
||||||
|
</TableColumnItem>
|
||||||
|
<TableColumnItem>
|
||||||
|
<PropertyName>Hour</PropertyName>
|
||||||
|
</TableColumnItem>
|
||||||
|
<TableColumnItem>
|
||||||
|
<PropertyName>DayOfMonth</PropertyName>
|
||||||
|
</TableColumnItem>
|
||||||
|
<TableColumnItem>
|
||||||
|
<PropertyName>Month</PropertyName>
|
||||||
|
</TableColumnItem>
|
||||||
|
<TableColumnItem>
|
||||||
|
<PropertyName>DayOfWeek</PropertyName>
|
||||||
|
</TableColumnItem>
|
||||||
|
<TableColumnItem>
|
||||||
|
<PropertyName>Command</PropertyName>
|
||||||
|
</TableColumnItem>
|
||||||
|
</TableColumnItems>
|
||||||
|
</TableRowEntry>
|
||||||
|
</TableRowEntries>
|
||||||
|
</TableControl>
|
||||||
|
</View>
|
||||||
|
</ViewDefinitions>
|
||||||
|
</Configuration>
|
||||||
Binary file not shown.
@@ -0,0 +1,189 @@
|
|||||||
|
$crontabcmd = "/usr/bin/crontab"
|
||||||
|
|
||||||
|
#TODO fix after https://github.com/PowerShell/PowerShell/issues/932 is fixed
|
||||||
|
#class CronJob {
|
||||||
|
# [string] $Minute
|
||||||
|
# [string] $Hour
|
||||||
|
# [string] $DayOfMonth
|
||||||
|
# [string] $Month
|
||||||
|
# [string] $DayOfWeek
|
||||||
|
# [string] $Command
|
||||||
|
#}
|
||||||
|
|
||||||
|
# Internal helper functions
|
||||||
|
|
||||||
|
function Get-CronTab ([String] $user) {
|
||||||
|
$crontab = Invoke-CronTab -user $user -arguments "-l" -noThrow
|
||||||
|
if ($crontab -is [System.Management.Automation.ErrorRecord]) {
|
||||||
|
if ($crontab.Exception.Message.StartsWith("no crontab for ")) {
|
||||||
|
$crontab = $null
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw $crontab.Exception
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[string[]] $crontab
|
||||||
|
}
|
||||||
|
|
||||||
|
function ConvertTo-CronJob ([String] $crontab) {
|
||||||
|
$split = $crontab.split(" ", 6)
|
||||||
|
$cronjob = New-Object -TypeName PSObject -Property @{ #TODO: change to CronJob type
|
||||||
|
PSTypeName="CronJob";
|
||||||
|
Minute = $split[0];
|
||||||
|
Hour = $split[1];
|
||||||
|
DayOfMonth= $split[2];
|
||||||
|
Month =$split[3];
|
||||||
|
DayOfWeek = $split[4];
|
||||||
|
Command = $split[5]
|
||||||
|
}
|
||||||
|
$cronjob
|
||||||
|
}
|
||||||
|
|
||||||
|
function Invoke-CronTab ([String] $user, [String] $arguments, [Switch] $noThrow) {
|
||||||
|
If ($user -ne [String]::Empty) {
|
||||||
|
$arguments = "-u $UserName $arguments"
|
||||||
|
}
|
||||||
|
|
||||||
|
$cmd = "$crontabcmd $arguments 2>&1"
|
||||||
|
Write-Verbose $cmd
|
||||||
|
$output = Invoke-Expression $cmd
|
||||||
|
if ($LastExitCode -ne 0 -and -not $noThrow) {
|
||||||
|
$e = New-Object System.InvalidOperationException -ArgumentList $output.Exception.Message
|
||||||
|
throw $e
|
||||||
|
} else {
|
||||||
|
$output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Import-CronTab ([String] $user, [String[]] $crontab) {
|
||||||
|
$temp = New-TemporaryFile
|
||||||
|
[String]::Join([Environment]::NewLine,$crontab) | Set-Content $temp.FullName
|
||||||
|
Invoke-CronTab -user $user $temp.FullName
|
||||||
|
Remove-Item $temp
|
||||||
|
}
|
||||||
|
|
||||||
|
# Public functions
|
||||||
|
|
||||||
|
function Remove-CronJob {
|
||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Removes the exactly matching cron job from the cron table
|
||||||
|
.DESCRIPTION
|
||||||
|
Removes the exactly matching cron job from the cron table
|
||||||
|
.EXAMPLE
|
||||||
|
Get-CronJob | ? {%_.Command -like 'foo *'} | Remove-CronJob
|
||||||
|
.RETURNVALUE
|
||||||
|
None
|
||||||
|
.PARAMETER UserName
|
||||||
|
Optional parameter to specify a specific user's cron table
|
||||||
|
.PARAMETER Job
|
||||||
|
Cron job object returned from Get-CronJob
|
||||||
|
#>
|
||||||
|
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
|
||||||
|
param (
|
||||||
|
[Alias("u")][Parameter(Mandatory=$false)][String] $UserName,
|
||||||
|
[Alias("j")][Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSTypeName("CronJob")] $Job #TODO use CronJob type
|
||||||
|
)
|
||||||
|
process {
|
||||||
|
|
||||||
|
[string[]] $crontab = Get-CronTab -user $UserName
|
||||||
|
[string[]] $newcrontab = $null
|
||||||
|
$found = $false
|
||||||
|
|
||||||
|
foreach ($line in $crontab) {
|
||||||
|
$cronjob = ConvertTo-CronJob -crontab $line
|
||||||
|
if ((Compare-object $cronjob.psobject.properties $Job.psobject.properties) -eq $null) {
|
||||||
|
$found = $true
|
||||||
|
} else {
|
||||||
|
$newcrontab += $line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $found) {
|
||||||
|
$e = New-Object System.Exception -ArgumentList "Job not found"
|
||||||
|
throw $e
|
||||||
|
}
|
||||||
|
if ($pscmdlet.ShouldProcess($Job.Command,"Remove")) {
|
||||||
|
Import-CronTab -user $UserName -crontab $newcrontab
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function New-CronJob {
|
||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Create a new cron job
|
||||||
|
.DESCRIPTION
|
||||||
|
Create a new job in the cron table. Date and time parameters can be specified
|
||||||
|
as ranges such as 10-30, as a list: 5,6,7, or combined 1-5,10-15. An asterisk
|
||||||
|
means 'first through last' (the entire allowed range). Step values can be used
|
||||||
|
with ranges or with an asterisk. Every 2 hours can be specified as either
|
||||||
|
0-23/2 or */2.
|
||||||
|
.EXAMPLE
|
||||||
|
New-CronJob -Minute 10-30 -Hour 10-20/2 -DayOfMonth */2 -Command "/bin/bash -c 'echo hello' > ~/hello"
|
||||||
|
.RETURNVALUE
|
||||||
|
If successful, an object representing the cron job is returned
|
||||||
|
.PARAMETER UserName
|
||||||
|
Optional parameter to specify a specific user's cron table
|
||||||
|
.PARAMETER Minute
|
||||||
|
Valid values are 0 to 59. If not specified, defaults to *.
|
||||||
|
.PARAMETER Hour
|
||||||
|
Valid values are 0-23. If not specified, defaults to *.
|
||||||
|
.PARAMETER DayOfMonth
|
||||||
|
Valid values are 1-31. If not specified, defaults to *.
|
||||||
|
.PARAMETER Month
|
||||||
|
Valid values are 1-12. If not specified, defaults to *.
|
||||||
|
.PARAMETER DayOfWeek
|
||||||
|
Valid values are 0-7. 0 and 7 are both Sunday. If not specified, defaults to *.
|
||||||
|
.PARAMETER Command
|
||||||
|
Command to execute at the scheduled time and day.
|
||||||
|
#>
|
||||||
|
[CmdletBinding()]
|
||||||
|
param (
|
||||||
|
[Alias("u")][Parameter(Mandatory=$false)][String] $UserName,
|
||||||
|
[Alias("mi")][Parameter(Position=1)][String[]] $Minute = "*",
|
||||||
|
[Alias("h")][Parameter(Position=2)][String[]] $Hour = "*",
|
||||||
|
[Alias("dm")][Parameter(Position=3)][String[]] $DayOfMonth = "*",
|
||||||
|
[Alias("mo")][Parameter(Position=4)][String[]] $Month = "*",
|
||||||
|
[Alias("dw")][Parameter(Position=5)][String[]] $DayOfWeek = "*",
|
||||||
|
[Alias("c")][Parameter(Mandatory=$true,Position=6)][String] $Command
|
||||||
|
)
|
||||||
|
process {
|
||||||
|
# TODO: validate parameters, note that different versions of crontab support different capabilities
|
||||||
|
$line = "{0} {1} {2} {3} {4} {5}" -f [String]::Join(",",$Minute), [String]::Join(",",$Hour),
|
||||||
|
[String]::Join(",",$DayOfMonth), [String]::Join(",",$Month), [String]::Join(",",$DayOfWeek), $Command
|
||||||
|
[string[]] $crontab = Get-CronTab -user $UserName
|
||||||
|
$crontab += $line
|
||||||
|
Import-CronTab -User $UserName -crontab $crontab
|
||||||
|
ConvertTo-CronJob -crontab $line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-CronJob {
|
||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Returns the current cron jobs from the cron table
|
||||||
|
.DESCRIPTION
|
||||||
|
Returns the current cron jobs from the cron table
|
||||||
|
.EXAMPLE
|
||||||
|
Get-CronJob -UserName Steve
|
||||||
|
.RETURNVALUE
|
||||||
|
CronJob objects
|
||||||
|
.PARAMETER UserName
|
||||||
|
Optional parameter to specify a specific user's cron table
|
||||||
|
#>
|
||||||
|
[CmdletBinding()]
|
||||||
|
[OutputType([PSObject])]
|
||||||
|
param (
|
||||||
|
[Alias("u")][Parameter(Mandatory=$false)][String] $UserName
|
||||||
|
)
|
||||||
|
process {
|
||||||
|
$crontab = Get-CronTab -user $UserName
|
||||||
|
ForEach ($line in $crontab) {
|
||||||
|
if ($line.Trim().Length -gt 0)
|
||||||
|
{
|
||||||
|
ConvertTo-CronJob -crontab $line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
# Samples
|
||||||
Reference in new issue
Block a user