Skip to content

Instantly share code, notes, and snippets.

@fredhsu
Created January 28, 2014 02:03
Show Gist options
  • Save fredhsu/8661168 to your computer and use it in GitHub Desktop.
Save fredhsu/8661168 to your computer and use it in GitHub Desktop.
Example PowerShell script for talking to an Arista Switch via OMI
# Create an object which represents session options
# -SkipCACheck and -SkipCNCheck are used to skip SSL certificate checks
$so = New-CimSessionOption -SkipCACheck -SkipCNCheck -UseSsl
# Switch credentials
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential( "admin", $password )
# Create a session to the switch
$switch = "172.22.28.159"
$session = New-CimSession -CN $switch -port 5986 -Auth Basic `
-Credential $credentials -SessionOption $so
# Enumerate switch ports
$ethernetPorts = Get-CimInstance -ClassName CIM_EthernetPort `
-CimSession $s
# Print the ports
foreach( $ep in $ethernetPorts ) {
Write-Host $ep
$ep.speed
}
# An instance of CIM_EthernetPortAllocationSettingData has attributes
# associated with the ethernet port.
# Get the first ethernet port
$ethernetPorts = Get-CimInstance -ClassName CIM_EthernetPort `
-CimSession $session
$firstPort = $ethernetPorts[0]
# Get the CIM_EthernetPortAllocationSettingData associated with the
# first port
$setting = Get-CimAssociatedInstance -InputObject $firstPort `
-ResultClassName CIM_EthernetPortAllocationSettingData `
-CimSession $session
# CIM_EthernetPortAllocationSetting data can be used to change the
# VLAN mode of the port by setting its DesiredVLANEndpointMode attribute
# to the appropriate value
$trunkMode = 5 # Magic constant defined in the CIM schema
$setting = Get-CimAssociatedInstance -InputObject $firstPort `
-ResultClassName CIM_EthernetPortAllocationSettingData `
-CimSession $session
Set-CimInstance -InputObject $setting -Property `
@{ DesiredVLANEndpointMode = $trunkMode }
# Get the switch service instance which implements a number of
# convenience methods
$switchService = Get-CimInstance -ClassName Arista_SwitchService `
-CimSession $session
# CIM_NetworkVLAN represents a VLAN
# Get the VLAN whose VLANId is equal to 42
$vlan = Get-CimInstance -CimSession $session -ClassName CIM_NetworkVLAN `
| ? { $_.VLANId -eq 42 }
# Create an array of vlans with just one object
$vlans = [CimInstance[]]@($vlan)
# Disable the VLAN by calling switch service's DisableVLAN method.
# DisableVLAN method disables all VLANs that are passed in the parameter.
Invoke-CimMethod -InputObject $switchService -CimSession $session `
-MethodName "DisableVLAN" -Arguments @{ VLAN=$vlans }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment