Skip to content

Instantly share code, notes, and snippets.

@dylanmei
Created November 17, 2013 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanmei/7515652 to your computer and use it in GitHub Desktop.
Save dylanmei/7515652 to your computer and use it in GitHub Desktop.
WinRM's equivalent of Hello World in Go, from the XML sample in on the front page of MSDN's WS-Management documentation.
package main
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
)
var user = "vagrant"
var pass = "vagrant"
func main() {
command := `<s:Envelope
xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
>
<s:Header>
<a:To>http://RemoteComputer:80/wsman</a:To>
<w:ResourceURI s:mustUnderstand="true">
http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_logicaldisk
</w:ResourceURI>
<a:ReplyTo>
<a:Address s:mustUnderstand="true">
http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
</a:Address>
</a:ReplyTo>
<a:Action s:mustUnderstand="true">
http://schemas.xmlsoap.org/ws/2004/09/transfer/Get
</a:Action>
<w:MaxEnvelopeSize s:mustUnderstand="true">153600</w:MaxEnvelopeSize>
<a:MessageID>uuid:4ED2993C-4339-4E99-81FC-C2FD3812781A</a:MessageID>
<w:Locale xml:lang="en-US" s:mustUnderstand="false"/>
<w:SelectorSet>
<w:Selector Name="DeviceId">c:</w:Selector>
</w:SelectorSet>
<w:OperationTimeout>PT60.000S</w:OperationTimeout>
</s:Header>
<s:Body />
</s:Envelope>`
request, _ := http.NewRequest("POST",
"http://localhost:5985/wsman", bytes.NewBufferString(command))
request.Header.Add("Content-Type", "application/soap+xml;charset=UTF-8")
request.Header.Add("Authorization", "Basic "+
base64.StdEncoding.EncodeToString([]byte(user+":"+pass)))
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
log.Fatal(err)
}
fmt.Println("HTTP Status", response.Status)
for key, value := range response.Header {
fmt.Println(" ", key, ":", value)
}
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
fmt.Printf("%s", body)
}
@dylanmei
Copy link
Author

Should print something like...

Status 200
   Content-Type : [application/soap+xml;charset=UTF-8]
   Server : [Microsoft-HTTPAPI/2.0]
   Date : [Sun, 17 Nov 2013 16:46:51 GMT]
   Content-Length : [2411]
<s:Envelope>....</s:Envelope>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment