PowerShell Module

Andra typer av bidrag | Other contributions
Post Reply
riro
3 stars
3 stars
Posts: 161
Joined: Wed 11 Jan 2012, 16:40
Contact:

PowerShell Module

Post by riro » Sun 06 Oct 2013, 06:58

Anyone using PowerShell ?

I do a lot of work in PowerShell, so why not use it to talk to SwitchKing..? :)

Download the module here:
http://www.snowland.se/powershell/switchking-posh/

Changelog
2013-10-06 First Release
2013-10-06 Updated with info on relative commands - viewtopic.php?f=10&t=1407
http://switchqueen.snowland.se/ - Every king needs his queen

riro
3 stars
3 stars
Posts: 161
Joined: Wed 11 Jan 2012, 16:40
Contact:

Re: PowerShell Module

Post by riro » Fri 25 Oct 2013, 07:26

I presented at the Swedish Powershell UserGroup Meeting yesterday... and as a small bonus I did some fun stuff with SwitchKing.

Here is some of the code (some nice examples to get started)

Code: Select all

# Ladda modulen
Import-Module "C:\Dropbox\Powershell UserGroup\SwitchKing.psm1"

# Skapa anslutning mot SK's demo miljö
$MySkConnection = Get-SwitchKingInfo -ServerName www.switchking.se -port 10800 -user user -Password pass
Get-SwitchKingDevices -SwitchKing $MySkConnection | Format-Table ID, Name, GroupName -AutoSize

# Släcka / Tända lampor i en grupp
Get-SwitchKingDevices -SwitchKing $MySkConnection | Where-Object { $_.GroupName -eq "Upstairs" } | Set-SwitchKingDevice -Command "turnon" -SwitchKing $MySkConnection

# Sätta värden i en datakälla
Get-SwitchKingDataSources -SwitchKing $MySkConnection | Where-Object { $_.Name -eq "Powershell Test" } | Add-SwitchKingDataSourceValue -Value 4 -SwitchKing $MySkConnection

# Mera värden i en datakälla
$DataSource = Get-SwitchKingDataSources -SwitchKing $MySkConnection | Where-Object { $_.Name -eq "Powershell Test" }
(1..10) | Foreach-Object {
    Add-SwitchKingDataSourceValue -Value (Get-Random -Minimum 1 -Maximum 100) -SwitchKing $MySkConnection -DataSource $DataSource    
    Start-Sleep -Seconds 1
}

# Rensa filen
Remove-Item "C:\Dropbox\Powershell UserGroup\demo.png"
# Hämta en liten graph
$DataSource = Get-SwitchKingDataSources -SwitchKing $MySkConnection | Where-Object { $_.Name -eq "PowerShell Test" }
Get-SwitchKingDataSourceGraph -SwitchKing $MySkConnection -minutesOfHistory 60 -DataSourceID $DataSource.ID -filePath "C:\Dropbox\Powershell UserGroup\demo.png"
# Öppna
. "C:\Dropbox\Powershell UserGroup\demo.png"


# Fun stuff... :)
$DataSource = Get-SwitchKingDataSources -SwitchKing $MySkConnection | Where-Object { $_.Name -eq "Phone" }

Function Test-PhoneOnline {
    Return Test-Path "C:\Dropbox\Powershell UserGroup\PhoneOnline"   
#    Return Test-Connection -ComputerName $PhoneIP -Quiet -Count 1
}

While ($true) {
    If (Test-PhoneOnline) {
        Write-Host "O" -NoNewline -ForegroundColor Yellow
        Add-SwitchKingDataSourceValue -Value "Online" -SwitchKing $MySkConnection -DataSource $DataSource | Out-Null
    } else {
        Write-Host "." -NoNewline -ForegroundColor Gray
        Add-SwitchKingDataSourceValue -Value "Offline" -SwitchKing $MySkConnection -DataSource $DataSource | Out-Null
    }
    Start-Sleep 5
}

http://switchqueen.snowland.se/ - Every king needs his queen

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests