WEMO Application

Showing results for 
Search instead for 
Do you mean 
Reply
Posts: 1
Registered: ‎06-24-2018

How to turn off a Wemo swich with simple powershell script.

Hallo All,

 

Appreciate the help. For a small testbed im looking for a simple powershell script to turn off a Wemo power switch. Nothing more.

 

I got it to work in the app and in a linux setup but based on some constraints of the other systems involved in the test, i am now limited to windows command line.

 

Something simple that i can copy past into a .ps script.

 

 

Also i cant really use any other libraries (the java option) as there are some limitations to what is allowed to run on the platform.

 

 

Kind Regards,

Sebas

Highlighted
Posts: 1
Registered: ‎09-02-2018

Re: How to turn off a Wemo swich with simple powershell script.

[ Edited ]

Decided to give it a try and it works.  I have a crypto miner that I'm now going to use this to turn on/off a box fan when the miner is runing.

 

 

function Wemo ($Ip, $Port=49153, $Method='Get', $State='None'){
    #$Ip     = '192.168.1.152'
    #$Port   = '49153'
    #$State  = "None"
    #$Method = "Get"
    $obj    = "BinaryState"

    If ($State.Tolower() -eq 'on') {$State = '1'}
    If ($State.Tolower() -eq 'off') {$State = '0'}
    If ($Method.Tolower() -eq 'get') {$Method = 'Get'}
    If ($Method.Tolower() -eq 'set') {$Method = 'Set'}


    $header = '"urn:Belkin:service:basicevent:1#'+$Method+$obj+'"'
    $body   = '<u:'+$Method+$obj+' xmlns:u="urn:Belkin:service:basicevent:1"><'+$obj+'>'+$State+'</'+$obj+'></u:'+$Method+$obj+'>'

    $uri = "http://"+$Ip+":"+$Port+"/upnp/control/basicevent1"

    $Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $Headers.Add('Content-type', 'text/xml; charset="utf-8"')
    $Headers.Add('SOAPACTION', $header)

    $data = '<?xml version="1.0" encoding="utf-8"?>'
    $data += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
    $data += '<s:Body>'+$body+'</s:Body></s:Envelope>' 

    $Error.Clear
    $result = Invoke-RestMethod -Uri $uri -Headers $Headers -Body $Data -Method Post
    
    switch ($Method) {
        'Set' {Return $result.Envelope.body.SetBinaryStateResponse.BinaryState}
        'Get' {return $result.Envelope.body.GetBinaryStateResponse.BinaryState}
        Default {Return $Error}
    }
}

$MinerSwitch = '192.168.1.120'
$GameRoomSwitch = '192.168.1.126'
$FanSwitch = '192.168.1.152'

$Result = Wemo -Ip $FanSwitch -Method 'Set' -State "On"

$Result = Wemo -Ip $FanSwitch -Method 'Get'

 

Posts: 6
Registered: ‎09-26-2018

Re: How to turn off a Wemo swich with simple powershell script.

 

Everytime I (re)start my pc, Windows tries to update. but turn off a Wemo swich??  It seems that it keeps restarting at 19%, and after doing so two times it concludes it couldn't update the pc, so it undoes the changes and starts windows. The updates have been failing for weeks now and it starts to get annoying and worrying