20 jul

HyperV PowerShell Rest Service

ipmo 'virtualmachinemanager\virtualmachinemanager.psd1'
 
# Create a listener on port 8000
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add('http://+:8080/') 
$listener.Start()
Get-VMMServer vmm.redemapa.agro
'Listening ...'
 
# Run until you send a GET request to /end
while ($true) {
    $context = $listener.GetContext() 
    # Capture the details about the request
    $request = $context.Request
    # Setup a place to deliver a response
    $response = $context.Response
    # Break from loop if GET request sent to /end
    if ($request.Url -match '/end') { 
        break 
    } else {
        # Split request URL to get command and options
        $requestvars = ([String]$request.Url).split("/");        
        switch($requestvars[3])
        {
            "propriedadesCustomizadasGeral" 
            {
                # Get the class name and server name from the URL and run get-WMIObject
                $result = & {
                    foreach ($propriedade in $(Get-SCCustomProperty | Where-Object -Property Name -notlike "Custom*"  | Where-Object -Property Name -ne "SupportedVmGenerationForVmRole"))
                    {
                        foreach ($objeto in $(Get-VM))
                        {
                            $temp = Get-SCCustomPropertyValue -InputObject $objeto -CustomProperty $propriedade;
                            If ($temp.Name -ne $null)
                            {
                                [pscustomobject] @{
                                    Property = $temp.Name
                                    Value = $temp.Value
                                }
                            }
                        }
                    }
                }
                # Convert the returned data to JSON and set the HTTP content type to JSON
                $message = $result | ConvertTo-Json; 
                $response.ContentType = 'application/json';
            }
            "propriedadesCustomizadasVM"
            {
                $result =  &{foreach ($propriedade in $(Get-SCCustomProperty | Where-Object -Property Name -notlike "Custom*"  | Where-Object -Property Name -ne "SupportedVmGenerationForVmRole"))
                {
                    $temp = Get-SCCustomPropertyValue -InputObject (Get-VM $requestvars[4])[0] -CustomProperty $propriedade;
                    If ($temp.Name -ne $null)
                    {
                        [pscustomobject] @{
                            Property = $temp.Name
                            Value = $temp.Value
                        }
                    }
                 } }
                 $message = $result | ConvertTo-Json; 
                 $response.ContentType = 'application/json';
            }
            "VM"
            {
                $result =  &{get-vm $requestvars[4]}
                $message = $result | ConvertTo-Json; 
                $response.ContentType = 'application/json';
            }
            "VMs"
            {
                $result =  &{get-vm | select-object name,BiosGuid, vmid ,hostname, ComputerName,StatusString, @{Name="IPv4Addresses"; Expression={ $_.VirtualNetworkAdapters.IPv4Addresses}}}
                $message = $result | ConvertTo-Json; 
                $response.ContentType = 'application/json';
            }
            default
            {
                # If no matching subdirectory/route is found generate a 404 message
                $message = "This is not the page you're looking for.";
                $response.ContentType = 'text/html' ;
            }
        }
       # Convert the data to UTF8 bytes
       [byte[]]$buffer = [System.Text.Encoding]::UTF8.GetBytes($message)
 
       # Set length of response
       $response.ContentLength64 = $buffer.length
 
       # Write response out and close
       $output = $response.OutputStream
       $output.Write($buffer, 0, $buffer.length)
       $output.Close()
   }    
}
 
#Terminate the listener
$listener.Stop()