Page 1 of 1
PowerShell Module
Posted: Sun 06 Oct 2013, 06:58
by riro
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
Re: PowerShell Module
Posted: Fri 25 Oct 2013, 07:26
by riro
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
}