SCVMM / Hyper-V connect using PHP

Published on Author Code Father

SCVMM forms part of Microsoft’s System Center line of management and reporting tools, alongside previously established tools such as Microsoft Operations Manager and Microsoft Systems Management Server. SCVMM is designed for management of large numbers of Virtual Servers based on MicrosoftVirtual Server and HyperV, and was released for enterprise customers in October 2007.  A standalone version for small and medium business customers is available.

scm_curl.php

<?php

class scmCurl{

public $username = ”;
public $password = ”;
public $panelurl = ‘http://india.com/’;

private $cookie = ”;

public function listVM(){
return $this->getMachines();
}
public function startVM($id=0){
if(empty($id)){
$id = $this->getMachines();
$id = $id[0];
}
$baseurl = $this->panelurl.’LiveStatusService.asmx/StartVM’;
$cookiepage = $this->curlit($baseurl,false,true,true,'{“machineID”:”‘.$id.'”}’);
}
public function pauseVM($id=0){
if(empty($id)){
$id = $this->getMachines();
$id = $id[0];
}
$baseurl = $this->panelurl.’LiveStatusService.asmx/PauseVM’;
$cookiepage = $this->curlit($baseurl,false,true,true,'{“machineID”:”‘.$id.'”}’);
}
public function stopVM($id=0){
if(empty($id)){
$id = $this->getMachines();
$id = $id[0];
}
$baseurl = $this->panelurl.’LiveStatusService.asmx/StopVM’;
$cookiepage = $this->curlit($baseurl,false,true,true,'{“machineID”:”‘.$id.'”}’);
}
private function getCookie(){
$baseurl = $this->panelurl.’Default.aspx’;

$formpage = $this->curlit($baseurl,false,false,false,false,false);

preg_match_all(‘#<input(.*type=.hidden.*)/>#isU’,$formpage,$array,PREG_PATTERN_ORDER);
$postargs=”;
foreach($array[1] as $arrayunique){
$arrayunique1=”;
$arrayunique2=”;
preg_match_all(‘#name=”(.*)”#isU’,$arrayunique,$arrayunique1);
preg_match_all(‘#value=”(.*)”#isU’,$arrayunique,$arrayunique2);
$postargs = $postargs.$arrayunique1[1][0].’=’.urlencode($arrayunique2[1][0]).’&’;
}

$postargs = $postargs.’rdoGrpPublicPrivate=rdoNoStoreCred&chkRememberChoice=on&loginControl%24UserName=’.urlencode($this->username).’&loginControl%24Password=’.urlencode($this->password).’&loginControl%24LoginButton=Se+connecter’;

$cookiepage = $this->curlit($baseurl,true,true,false,$postargs,false);

preg_match_all(‘#Set-Cookie:(.*);#isU’,$cookiepage,$cookie);
$this->cookie = $cookie[1][0];
return 1;
}

private function curlit($url=”,$header=false,$post=false,$json=false,$postfields=false,$getcookie=true){
if(empty($url)){
return -1;
}
$ch = curl_init();
// — If there’s no cookies, creating one
if($getcookie==true){
if(empty($this->cookie)){
$this->getCookie();
}
curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
}

if($post==true){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
}
$httpheader = array();
if(!empty($postfields) || $postfields==”){
$httpheader[]=’Content-length:’.strlen($postfields);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
}
if($json==true){
$httpheader[]=’Content-Type:application/json; charset=utf-8′;
}

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
curl_setopt($ch, CURLOPT_HEADER, $header);

$return = curl_exec($ch);

curl_close($ch);
return $return;
}

private function getMachines(){
$baseurl = $this->panelurl.’LiveStatusService.asmx/GetMachineList’;

$machinespage = $this->curlit($baseurl,false,true,true,”);
$jsoned = json_decode($machinespage,true);
$machinesid = array();
foreach($jsoned[‘Machines’] as $machines){
$machinesid[] = $machines[‘ID’];
}
return $machinesid;
}

}

?>

usage.php

<?php
include_once(‘scm_curl.php’);

$scmCurl = new scmCurl();
$scmCurl->username = ”;
$scmCurl->password = ”;
/*if($_GET[‘action’]==’start’){
$scmCurl->startVM();
}elseif($_GET[‘action’]==’pause’){
$scmCurl->pauseVM();
}*/
$scmCurl->listVM();
?>

Comments

comments