Posted in Windows Powershell | No Comment | 1,478 views | 14/07/2012 21:49
Your script needs “Run as Administrator” privileges? Then you should use this function to check it.
1
2
3
4
5
6
7
8
9
| function Confirm-PoSHAdminPrivileges
{
$User = [Security.Principal.WindowsIdentity]::GetCurrent()
if((New-Object Security.Principal.WindowsPrincipal $User).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
{
$Result = "Validated"
}
$Result
} |
function Confirm-PoSHAdminPrivileges
{
$User = [Security.Principal.WindowsIdentity]::GetCurrent()
if((New-Object Security.Principal.WindowsPrincipal $User).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
{
$Result = "Validated"
}
$Result
}
If you get “Validated” as a output, then you can be sure about administrator privileges.
Posted in Windows Powershell | No Comment | 7,578 views | 14/07/2012 21:45
If you install Parallels Plesk before, you should be aware about “Welcome Banner” of Plesk. It’s pretty cool! I did same thing on Powershell and made it function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| function Get-PoSHWelcomeBanner
{
param ($Hostname, $Port)
# Get Hostname
if (!$Hostname -or $Hostname -eq "+")
{
$Hostname = "localhost"
}
else
{
$Hostname = @($Hostname.Split(","))[0]
}
# Get Port
if ($Port -ne "80")
{
$Port = ":$Port"
}
clear
Write-Host " "
Write-Host " Welcome to PoSH Server"
Write-Host " "
Write-Host " "
Write-Host " You can start browsing your webpage from:"
Write-Host " http://$Hostname$Port"
Write-Host " "
Write-Host " "
Write-Host " Thanks for using PoSH Server.."
Write-Host " "
Write-Host " "
Write-Host " "
} |
function Get-PoSHWelcomeBanner
{
param ($Hostname, $Port)
# Get Hostname
if (!$Hostname -or $Hostname -eq "+")
{
$Hostname = "localhost"
}
else
{
$Hostname = @($Hostname.Split(","))[0]
}
# Get Port
if ($Port -ne "80")
{
$Port = ":$Port"
}
clear
Write-Host " "
Write-Host " Welcome to PoSH Server"
Write-Host " "
Write-Host " "
Write-Host " You can start browsing your webpage from:"
Write-Host " http://$Hostname$Port"
Write-Host " "
Write-Host " "
Write-Host " Thanks for using PoSH Server.."
Write-Host " "
Write-Host " "
Write-Host " "
}
Seriously, this looks good :) It’s a good way to redirect people to web browser.
Posted in Windows Powershell | No Comment | 3,321 views | 14/07/2012 21:39
I’ve recently added PHP support on PoSHServer. Here is my example codes for PHP execution on PowerShell:
Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| $PHPCgiPath = "C:\Program Files (x86)\PHP\v5.3\php-cgi.exe" # WebPI Location
$TestPHPCgiPath = Test-Path $PHPCgiPath
if ($TestPHPCgiPath)
{
$Response.ContentType = "text/html"
$PHPQuery = Get-PoSHPHPQuery -Request $Request
$PHPContentOutput = Get-PoSHPHPContent -File $File -QueryString $PHPQuery -PHPCgiPath $PHPCgiPath
$PHPContentOutput = Set-PHPEncoding -PHPOutput $PHPContentOutput
$Response.StatusCode = [System.Net.HttpStatusCode]::OK
$LogResponseStatus = $Response.StatusCode
$Response = New-Object IO.StreamWriter($Response.OutputStream,[Text.Encoding]::UTF8)
$Response.WriteLine("$PHPContentOutput")
}
else
{
$Response.ContentType = "text/html"
$Response.StatusCode = [System.Net.HttpStatusCode]::NotFound
$LogResponseStatus = $Response.StatusCode
$Response = New-Object IO.StreamWriter($Response.OutputStream,[Text.Encoding]::UTF8)
$Response.WriteLine("$(. $PoSHModulePath\modules\phpcgierror.ps1)")
} |
$PHPCgiPath = "C:\Program Files (x86)\PHP\v5.3\php-cgi.exe" # WebPI Location
$TestPHPCgiPath = Test-Path $PHPCgiPath
if ($TestPHPCgiPath)
{
$Response.ContentType = "text/html"
$PHPQuery = Get-PoSHPHPQuery -Request $Request
$PHPContentOutput = Get-PoSHPHPContent -File $File -QueryString $PHPQuery -PHPCgiPath $PHPCgiPath
$PHPContentOutput = Set-PHPEncoding -PHPOutput $PHPContentOutput
$Response.StatusCode = [System.Net.HttpStatusCode]::OK
$LogResponseStatus = $Response.StatusCode
$Response = New-Object IO.StreamWriter($Response.OutputStream,[Text.Encoding]::UTF8)
$Response.WriteLine("$PHPContentOutput")
}
else
{
$Response.ContentType = "text/html"
$Response.StatusCode = [System.Net.HttpStatusCode]::NotFound
$LogResponseStatus = $Response.StatusCode
$Response = New-Object IO.StreamWriter($Response.OutputStream,[Text.Encoding]::UTF8)
$Response.WriteLine("$(. $PoSHModulePath\modules\phpcgierror.ps1)")
}
Function:
1
2
3
4
5
6
7
| function Get-PoSHPHPContent
{
param ($File, $QueryString, $PHPCgiPath)
$PHPOutput = &$PHPCgiPath -f $File $QueryString
$PHPOutput
} |
function Get-PoSHPHPContent
{
param ($File, $QueryString, $PHPCgiPath)
$PHPOutput = &$PHPCgiPath -f $File $QueryString
$PHPOutput
}
You can always download PoSHServer to see it live! :)
Posted in Windows Powershell | No Comment | 2,304 views | 27/05/2012 23:05
Yakın zaman içinde PoSH Server diye bir ürün çıkardım. PoSH Server kısaca Powershell üzerinde çalışan 30 KB’lık bir web server. Şuan için HTML, PHP ve Powershell içeriklerini http üzerinden sunabiliyor. PoSH Server, IIS’in kurulamadığı ya da daha portatif bir çözüm ile yayın yapmak istenilen sunucular için ideal bir üründür.
Kısaca PoSH Server özelliklerini maddeleyecek olursak:
1. Kimlik Doğrulama: Basic ve Windows Authentication kullanarak içeriklerinizi güvenlik altına alabilirsiniz.
2. IP Kısıtlama: HTTP yayınının bazı networkler üzerinden erişimini engelleyebilir ya da sadece belli ip adreslerinin erişimine açabilirsiniz.
3. İçerik Filtreleme: Bazı içeriklerin web sunucusu üzerinden görüntülenmesini veya indirilmesini engelleyebilirsiniz.
4. Gelişmiş Loglama: IIS standartlarında loglama sayesinde web sitenize gelen tüm istekler loglanmaktadır.
5. Log Parser: PoSH Server ile birlikte gelen ufak Log Parser sayesinde web loglarınızı görüntüleyebilirsiniz.
6. PHP desteği: WebPI üzerinden de kurabileceğiniz PHP 5.3 ve PHP 5.4’ü kullanarak, PoSH Server üzerinden PHP script çalıştırabilirsiniz.
7. Arkaplanda çalışma: Sunucunuzu restart etseniz bile PoSH Server restart sonrası çalışmaya devam edecektir.
PoSH Server, tüm bu özelliklerinin yanında; ufak boyutu, kolay kurulabilir olması ve açık kaynak kodlu olması sayesinde bir çok sistem yöneticisinin ilgisini çekebilecek bir üründür. Ayrıca Rest desteği sayesinde Powershell scriptlerinizi sunucunuzu yönetecek şekilde PoSH Server üzerinde kullanabilir ve işlem sonuçlarını XML olarak bastırabilirsiniz.
Kısa bir süre önce çıkmış olan SetLinuxVM Web Access de PoSH Server kullanarak hizmet vermektedir.
PoSH Server’ın web sitesine aşağıdaki adresten ulaşabilirsiniz.
Yine kurulum ve kullanımla ilgili sıkça sorulan soruları web sitesinde görebilirsiniz.
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.
Posted in Windows Powershell | No Comment | 2,240 views | 04/05/2012 01:35
If you need to create GUID on Powershell, just you can type:
[System.Guid]::NewGuid().toString() |
[System.Guid]::NewGuid().toString()
This command will give you something like this:
5f43a2ed-1dca-47d8-a0b8-18c7f0dbf03c
That’s all!
Posted in Virtual Machine Manager, Windows Powershell | 7 Comments | 7,165 views | 04/05/2012 01:02
I’m very proud to announce you the new version of SetLinuxVM! Today, SetLinuxVM is 1 year old!
After number of bug fixes, improvements and new functionalities, Set-LinuxVM reached to version 4.0 stable.
Fixed bugs:
1) Hyper-V LIC iso unmount issue.
2) Getting network card information issue.
3) Installation logic
New functionalities:
1) RestAPI support!
2) SetLinux Web Access: Web application for provisioning.
3) Built-in powershell web server (PoSHServer)
4) OutXML switch for XML results
General functionalities:
1) Unattended IP, Hostname and DNS configuration for Linux VMs.
2) Automatic Linux integration components installation.
3) Multi Distro Support: Debian, Ubuntu, Centos, Fedora, Redhat, Suse and CloudLinux!
4) Automatic CPanel and Parallels Plesk installation.
5) Linux VM Template support
6) Hyper-V support! You don’t need SCVMM to use this script.
7) Multiple Hyper-V and SCVMM host support.
8) Automatic Emulated NIC to Synthetic NIC support.
9) No need to internet connection (SSH access etc.) or additional changes on VM.
10) Custom Answer File support! You can execute your own scripts.
11) Hyper-V v2 and Hyper-V v3 support (fully works on Windows Server 2012 Beta)
12) System Center VMM 2008 and System Center VMM 2012 support
Screenshots:
Copyright (C) 2012 Yusuf Ozturk
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Download:
Donate: Thanks to support this open source project!
Usage:
1. First, you should allow signed Powershell scripts:
Set-ExecutionPolicy AllSigned |
Set-ExecutionPolicy AllSigned
2. Extract file and move “SetLinuxVM” directory under to “C:\Windows\System32\WindowsPowerShell\v1.0\Modules”.
3. Import as a module:
4. That’s it. To see how to use it:
Get-help Set-LinuxVM -full |
Get-help Set-LinuxVM -full
5. Follow to instructions.
How to start SetLinuxVM Web Access?
1. Just go to Powershell and type:
You can search in my blog for how to create Linux VM templates!
|