Die Grillplats gehen aber gut, da ich hier noch nichts in Powershell gefunden habe poste ich hier mal, was bei mir funktioniert (vmtl auch woanders).
fritz benutzername und Passwort wird einmalig abgefragt und verschlüsselt abgespeichert.
Werden keine Argumente für das Skript angegeben, werden nur die gefundenen Geräte ausgegeben.
Mit
.\fritz.ps1 GP3 setswitchon
kann man zB das Gerät mit dem Namen GP3 einschalten
.\fritz.ps1 GP3 setswitchoff
schaltet es zB aus
Habe ich mit den Grillplats getestet.
Funktioniert für Smartgateway oder Box, IP dann ggf ändern.
s.
https://fritz.support/resources/AHA-HTTP-Interface.pdf
$ps1path = split-path $MyInvocation.MyCommand.Path;$ps1name = $myInvocation.MyCommand.Name.Split(".")[0]
$credfn = $ps1path + "\" + $ps1name + "_" + $env:computername + "_" + $env:username + ".txt"
if (! (test-path $credfn)) {
$credential = Get-Credential (read-host "Enter once fritz Username and then Password")
$unsstr = ConvertTo-SecureString $credential.UserName -AsPlainText -Force
$unout = $unsstr | ConvertFrom-SecureString
$pwout = $credential.Password | ConvertFrom-SecureString
@($unout,$pwout) | Set-Content ($credfn)
}
$creds = Get-Content $credfn
$sstr = $creds[0] | ConvertTo-SecureString
$pscred = New-Object System.Management.Automation.PsCredential(" ", $sstr)
$username = $pscred.GetNetworkCredential().Password
$sstr = $creds[1] | ConvertTo-SecureString
$pscred = New-Object System.Management.Automation.PsCredential(" ", $sstr)
$password = $pscred.GetNetworkCredential().Password
$FritzboxIP = "192.168.168.168" # oder fritz.box
# 1. Challenge abrufen
$ChallengeUrl = "http://$FritzboxIP/login_sid.lua"
$ChallengeResponse = Invoke-RestMethod -Uri $ChallengeUrl
$Challenge = $ChallengeResponse.SessionInfo.Challenge
# 2. Hash aus Challenge und Passwort bilden
$pass = "$Challenge-$Password"
$passEnc = [System.Text.Encoding]::Unicode.GetBytes($pass)
$md5 = [System.Security.Cryptography.MD5]::Create()
$hash = $md5.ComputeHash($passEnc)
$hashString = ($hash | ForEach-Object { $_.ToString("x2") }) -join ""
# 3. Login durchführen und Session ID (SID) extrahieren
$LoginUrl = "http://$FritzboxIP/login_sid.lua?username=$username&response=$Challenge-$hashString"
#$LoginUrl = "http://$FritzboxIP/login_sid.lua?response=$Challenge-$hashString"
$LoginResponse = Invoke-RestMethod -Uri $LoginUrl
$SID = $LoginResponse.SessionInfo.SID
$deviceUrl = "$fritzboxip/webservices/homeautoswitch.lua?sid=$sid&switchcmd=getdevicelistinfos"
$devicesXml = Invoke-RestMethod -Uri $deviceUrl -Method Get
$devicesXml.devicelist.device | select name,productname
if ($args.count -eq 0 ) {"No Command, exit ...";exit}
$devicename = $args[0]
$command = $args[1]
$device = $devicesXml.devicelist.device | ? {$_.name -match $devicename}
$ain = $device[1].identifier
$resturl = "$fritzboxip/webservices/homeautoswitch.lua?ain=$ain&switchcmd=" + $command + "&sid=$sid"
$resturl
$resp = Invoke-RestMethod -Uri $resturl -Method Get
$resp