Advertisement
Guest User

NVX IP Set

a guest
Mar 14th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # import the module
  2. Import-Module PSCrestron
  3.  
  4. # script folder
  5. if ($PSScriptRoot)
  6.     {$here = $PSScriptRoot}
  7. else
  8.     {$here = $PWD}
  9.  
  10. # credentials
  11. $user = 'admin'
  12. $pass = 'admin'
  13.  
  14. # ---------------------------------------------------------
  15. function Set-StaticIP
  16. # ---------------------------------------------------------
  17. {
  18.     [CmdletBinding()]
  19.     param
  20.     (
  21.         [Parameter(Mandatory=$true)]
  22.         [object]$Device,
  23.  
  24.         [Parameter(Mandatory=$true)]
  25.         [object]$Static
  26.     )
  27.  
  28.     try
  29.     {
  30.         # open a socket
  31.         $s = Open-CrestronSession $Device.Device -Secure -Username $user -Password $pass
  32.        
  33.         # set the IP address
  34.         Invoke-CrestronSession $s "IPA 0 $($Static.IPA)" | Out-Null
  35.  
  36.         # set the IP mask
  37.         Invoke-CrestronSession $s "IPM 0 $($Static.IPM)" | Out-Null
  38.  
  39.         # set the default router
  40.         Invoke-CrestronSession $s "DEFR 0 $($Static.Gateway)" | Out-Null
  41.  
  42.         # set the DNS server
  43.         Invoke-CrestronSession $s "ADDD $($Static.DNS1)" | Out-Null
  44.         Invoke-CrestronSession $s "ADDD $($Static.DNS2)" | Out-Null
  45.  
  46.         # set the domain
  47.         Invoke-CrestronSession $s "DOMAIN $($Static.Domain)" | Out-Null
  48.  
  49.         # set DHCP off
  50.         Invoke-CrestronSession $s 'DHCP 0 OFF' | Out-Null
  51.  
  52.         # set the hostname
  53.         Invoke-CrestronSession $s "HOST $($Static.Hostname)" | Out-Null
  54.  
  55.         # close the socket
  56.         Close-CrestronSession $s
  57.  
  58.         # reset the device but don't wait for it to recover
  59.         Reset-CrestronDevice $Device.Device -NoWait -Secure -Username $user -Password $pass | Out-Null
  60.     }
  61.     catch
  62.         {throw}
  63. }
  64.  
  65. # ---------------------------------------------------------
  66. # main program starts here
  67. # ---------------------------------------------------------
  68.  
  69. # result collection
  70. Write-Host "Starting update at $(Get-Date -Format G)..."
  71. $robj = @()
  72.  
  73. # import the spreadhseet
  74. Write-Host 'Importing the spreadsheet...'
  75. $wb = Join-Path $here 'Devices.xlsx'
  76. $ws = Import-Excel -Workbook $wb -Worksheet 'Devices'
  77.  
  78. # auto discover the devices in the subnet
  79. Write-Host 'Running Auto-Discovery...'
  80. $devs = Get-AutoDiscovery |
  81.     Where-Object Description -Match 'NVX' |
  82.     Select-Object -ExpandProperty IP |
  83.     Get-VersionInfo -Secure -Username $user -Password $pass |
  84.     Where-Object MACAddress -Match '[A-F\d\.]+'
  85.  
  86. # iterate the MAC addresses
  87. Write-Host 'Running each row...'
  88. foreach ($row in $ws)
  89. {
  90.     Write-Host "Setting static mode on $($row.MACAddress)..."
  91.     try
  92.     {
  93.         $dev = $devs | Where-Object MACAddress -eq $row.MACAddress
  94.         if ($dev)
  95.         {
  96.             if ($dev.Device -ne $row.IPA)
  97.                 {Set-StaticIP -Device $dev -Static $row}
  98.             else
  99.                 {Write-Warning 'Device already set to static.'}
  100.         }
  101.         else
  102.             {Write-Warning 'Failed to find matching device.'}
  103.     }
  104.     catch
  105.         {Write-Warning $_.Exception.GetBaseException().Message}
  106. }
  107.  
  108. # wait for the devices to recover
  109. Write-Host 'Waiting for all devices to recover...'
  110. $ws | Invoke-RunspaceJob -SharedVariables user,pass -ScriptBlock {
  111.     Reset-CrestronDevice $_.IPA -WaitOnly -Secure -Username $user -Password $pass |
  112.     Out-Null}
  113.  
  114. # complete message
  115. Write-Host "Completed at $(Get-Date -Format G)."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement