Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import the module
- Import-Module PSCrestron
- # script folder
- if ($PSScriptRoot)
- {$here = $PSScriptRoot}
- else
- {$here = $PWD}
- # credentials
- $user = 'admin'
- $pass = 'admin'
- # ---------------------------------------------------------
- function Set-StaticIP
- # ---------------------------------------------------------
- {
- [CmdletBinding()]
- param
- (
- [Parameter(Mandatory=$true)]
- [object]$Device,
- [Parameter(Mandatory=$true)]
- [object]$Static
- )
- try
- {
- # open a socket
- $s = Open-CrestronSession $Device.Device -Secure -Username $user -Password $pass
- # set the IP address
- Invoke-CrestronSession $s "IPA 0 $($Static.IPA)" | Out-Null
- # set the IP mask
- Invoke-CrestronSession $s "IPM 0 $($Static.IPM)" | Out-Null
- # set the default router
- Invoke-CrestronSession $s "DEFR 0 $($Static.Gateway)" | Out-Null
- # set the DNS server
- Invoke-CrestronSession $s "ADDD $($Static.DNS1)" | Out-Null
- Invoke-CrestronSession $s "ADDD $($Static.DNS2)" | Out-Null
- # set the domain
- Invoke-CrestronSession $s "DOMAIN $($Static.Domain)" | Out-Null
- # set DHCP off
- Invoke-CrestronSession $s 'DHCP 0 OFF' | Out-Null
- # set the hostname
- Invoke-CrestronSession $s "HOST $($Static.Hostname)" | Out-Null
- # close the socket
- Close-CrestronSession $s
- # reset the device but don't wait for it to recover
- Reset-CrestronDevice $Device.Device -NoWait -Secure -Username $user -Password $pass | Out-Null
- }
- catch
- {throw}
- }
- # ---------------------------------------------------------
- # main program starts here
- # ---------------------------------------------------------
- # result collection
- Write-Host "Starting update at $(Get-Date -Format G)..."
- $robj = @()
- # import the spreadhseet
- Write-Host 'Importing the spreadsheet...'
- $wb = Join-Path $here 'Devices.xlsx'
- $ws = Import-Excel -Workbook $wb -Worksheet 'Devices'
- # auto discover the devices in the subnet
- Write-Host 'Running Auto-Discovery...'
- $devs = Get-AutoDiscovery |
- Where-Object Description -Match 'NVX' |
- Select-Object -ExpandProperty IP |
- Get-VersionInfo -Secure -Username $user -Password $pass |
- Where-Object MACAddress -Match '[A-F\d\.]+'
- # iterate the MAC addresses
- Write-Host 'Running each row...'
- foreach ($row in $ws)
- {
- Write-Host "Setting static mode on $($row.MACAddress)..."
- try
- {
- $dev = $devs | Where-Object MACAddress -eq $row.MACAddress
- if ($dev)
- {
- if ($dev.Device -ne $row.IPA)
- {Set-StaticIP -Device $dev -Static $row}
- else
- {Write-Warning 'Device already set to static.'}
- }
- else
- {Write-Warning 'Failed to find matching device.'}
- }
- catch
- {Write-Warning $_.Exception.GetBaseException().Message}
- }
- # wait for the devices to recover
- Write-Host 'Waiting for all devices to recover...'
- $ws | Invoke-RunspaceJob -SharedVariables user,pass -ScriptBlock {
- Reset-CrestronDevice $_.IPA -WaitOnly -Secure -Username $user -Password $pass |
- Out-Null}
- # complete message
- Write-Host "Completed at $(Get-Date -Format G)."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement