Posted in
Windows Powershell |
No Comment | 2,770 views | 22/05/2012 01:50
Well, new version of PoSH Server now supports PHP and running as a background job. But how did I do this? I’ve used scheduled jobs to start PoSH Server on every restart. Let’s see my code.
First I identify Task ID and Job Values:
1
2
3
4
5
| $TaskID = Get-Random -Maximum 10000
$TaskName = "PoSHServer-$TaskHostname-$TaskPort-$TaskID"
$CreateJobIDPath = $PoSHModulePath + "\jobs\job-" + $TaskID + ".txt"
$CreateJobIDValue = $HomeDirectory + ";" + $LogDirectory + ";" + $CustomConfig + ";" + $CustomJob
$CreateJobID = Add-Content -Path $CreateJobIDPath -Value $CreateJobIDValue |
$TaskID = Get-Random -Maximum 10000
$TaskName = "PoSHServer-$TaskHostname-$TaskPort-$TaskID"
$CreateJobIDPath = $PoSHModulePath + "\jobs\job-" + $TaskID + ".txt"
$CreateJobIDValue = $HomeDirectory + ";" + $LogDirectory + ";" + $CustomConfig + ";" + $CustomJob
$CreateJobID = Add-Content -Path $CreateJobIDPath -Value $CreateJobIDValue
Then I create scheduled job with these arguments:
1
2
3
4
5
6
7
8
9
10
| $CreateTask = schtasks /create /tn "$TaskName" /xml "$PoSHModulePath\jobs\template.xml" /NP
$ChangeTaskProcess = $true
while ($ChangeTaskProcess)
{
$ChangeTask = schtasks /change /tn "$TaskName" /tr "Powershell -Command &{Import-Module PoSHServer; Start-PoSHServer -Hostname '$Hostname' -Port '$Port' -JobID $TaskID}"
if ($ChangeTask)
{
$ChangeTaskProcess = $false
}
} |
$CreateTask = schtasks /create /tn "$TaskName" /xml "$PoSHModulePath\jobs\template.xml" /NP
$ChangeTaskProcess = $true
while ($ChangeTaskProcess)
{
$ChangeTask = schtasks /change /tn "$TaskName" /tr "Powershell -Command &{Import-Module PoSHServer; Start-PoSHServer -Hostname '$Hostname' -Port '$Port' -JobID $TaskID}"
if ($ChangeTask)
{
$ChangeTaskProcess = $false
}
}
Finally, I run Scheduled Job:
$RunTask = schtasks /run /tn "$TaskName" |
$RunTask = schtasks /run /tn "$TaskName"
You can download PoSH Server v1.6 to check rest of the codes.