0 votes

Script assigns skype ddi from available range then writes back to AD

#Reserved Numbers#
$ReservedNumbers =
    'tel:+441209200200;ext=200',
    'tel:+441209200222;ext=222',
    'tel:+441209200010;ext=010',
    'tel:+441209200055;ext=055',
    'tel:+441209200224;ext=224',
    'tel:+441209200080;ext=080',
    'tel:+441209200050;ext=050',
    'tel:+441209200086;ext=086',
    'tel:+441209200087;ext=087',
    'tel:+441209200088;ext=088',
    'tel:+441209200147;ext=147',
    'tel:+441209200249;ext=249',
    'tel:+441209200223;ext=223',
    'tel:+441209200295;ext=295',
    'tel:+441209200225;ext=225',
    'tel:+441209200227;ext=227',
    'tel:+441209200228;ext=228',
    'tel:+441209200229;ext=229',
    'tel:+441209200230;ext=230',
    'tel:+441209200245;ext=245',
    'tel:+441209200248;ext=248',
    'tel:+441209200271;ext=271'

#Variables
$NumberRange1 = 200000..200299
$NumberRange2 = 206734..206783
$Formattingstart= 'tel:+441209'
$FormattingEnd= ";ext="

#Build E.164 Numbers from ranges
function Get-Number {

       Param(
       [Parameter(Mandatory,ValueFromPipeline=$true)][validateLength(6,6)][validatePattern("[0-9]")][string[]]$number
       )
       Process{
        $number | % { "$($Formattingstart){0:d3}" -f $_} | % { "$_$($formattingend)$($_.ToString().Substring(14))" -f $_}
       }
       }

      $CompletedNumbersrange1 = $NumberRange1 | % {Get-Number -number $_} 
      $CompletedNumbersrange2 = $NumberRange2 | % {Get-Number -number $_} 


#Connect to Skype and retrieve Numbers in use
try {

$lyncServer = "Skype.domain.co.uk"

$sessionOptions = New-PSSessionOption -SkipRevocationCheck -SkipCACheck -SkipCNCheck
$session = New-PSSession -ConnectionUri https://$lyncServer/ocspowershell `
-SessionOption $sessionOptions -Authentication NegotiateWithImplicitCredential

Import-PSSession -session $session

$SkypeUsers = @()
$SkypeUsers = (Get-CsUser -Filter {lineuri -like '*'} -ResultSize UNLIMITED | select lineuri).lineuri
}
catch {}

Try{
    filter leftside{
        param(
                [Parameter(Position=0, Mandatory=$true,ValueFromPipeline = $true)]
                [ValidateNotNullOrEmpty()]
                [PSCustomObject]
                $obj
            )

            $obj|?{$_.sideindicator -eq '<='}

        }
    #Spare Numbers from range1
    $sparerange1 = Compare-Object $CompletedNumbersrange1 $SkypeUsers -IncludeEqual:$false -ExcludeDifferent:$false -PassThru:$true | leftside
    #Spare Numbers From Range2
    $sparerange2 = Compare-Object $CompletedNumbersrange2 $SkypeUsers -IncludeEqual:$false -ExcludeDifferent:$false -PassThru:$true | leftside


    #Combined spare numbers from both ranges
    $rangescombined = $sparerange1+$sparerange2

    #Exclude Reserved Numbers and remove whitespace and non E.164
    $excludereservedNumbers = Compare-Object $reservednumbers $rangescombined -IncludeEqual:$false -PassThru:$true
    [System.Collections.ArrayList]$ArraylistExcludeReservedNumbers = $excludereservedNumbers
    $NEWARRAY = $ArraylistExcludeReservedNumbers.Where({$_.trim() -ne "" -or $null})

    #Assign first spare number
    $nextnumber = $NEWARRAY[0]

    Set-CsUser -Identity "%fullname%" -LineUri "$nextnumber"

    Grant-CsHostedVoicemailPolicy -identity "%fullname%" -PolicyName "Tag:Office365UM" 

    Set-CsUser -Identity "%fullname%" -HostedVoiceMail $True -ErrorAction:Ignore

}
 catch{}

 try
{
    $Telephone = $Context.TargetObject.Get("telephoneNumber")
}
catch
{
    $Telephone = $NULL
}
if ($Telephone -eq $NULL)
{
    $Context.TargetObject.Put("telephoneNumber",$nextnumber)
}
else
{
    $Context.LogMessage("Have set the users Skype number to $nextnumber however a number was found in AD $telephone therefore have not updated AD please correct", "Information")
}
# Save changes
$Context.TargetObject.SetInfo()




    #Email count of spare numbers remaining if less than 10
    [System.Collections.ArrayList]$ArrayList =  $excludereservedNumbers
    $ArrayList.Remove("$nextnumber")
    $count = $ArrayList.Count

    IF ($count -lt "400")
    {
        $remainingnumbers = Send-MailMessage -Subject "test" -SmtpServer "exchange.domain.co.uk" -from "adaxes@domain.co.uk" -to "will@domain.co.uk" -Body " There are $count DDI numbers remaining "

        $remainingnumbers

    }


    remove-pssession $session
by (350 points)

1 Answer

0 votes
by (14.9k points)

Hello Will,

Unfortunately, we are not able to verify the entire script as we are not aware of the entire task/workflow. If you face errors/warnings executing the script, please, provide us with screenshots. Additionally, we found the following points in the script which will not work if executing it in Adaxes (e.g. in a custom command):

  • The percent (%) character used in PowerShell scripts executed in Adaxes is considered as the beginning of a value reference. If the character is used, for example, as an alias for the ForEach-Object cmdlet, the percent character must be escaped with another percent character. Finally, you should get two percent characters in a row (%%). For example:
$CompletedNumbersrange1 = $NumberRange1 | %% {Get-Number -number $_}
  • To create a new PowerShell session and then remove it after the required code is executed, the try-finally block should be used. Please, find the updated script below.
#Reserved Numbers#
$ReservedNumbers =
    'tel:+441209200200;ext=200',
    'tel:+441209200222;ext=222',
    'tel:+441209200010;ext=010',
    'tel:+441209200055;ext=055',
    'tel:+441209200224;ext=224',
    'tel:+441209200080;ext=080',
    'tel:+441209200050;ext=050',
    'tel:+441209200086;ext=086',
    'tel:+441209200087;ext=087',
    'tel:+441209200088;ext=088',
    'tel:+441209200147;ext=147',
    'tel:+441209200249;ext=249',
    'tel:+441209200223;ext=223',
    'tel:+441209200295;ext=295',
    'tel:+441209200225;ext=225',
    'tel:+441209200227;ext=227',
    'tel:+441209200228;ext=228',
    'tel:+441209200229;ext=229',
    'tel:+441209200230;ext=230',
    'tel:+441209200245;ext=245',
    'tel:+441209200248;ext=248',
    'tel:+441209200271;ext=271'

#Variables
$NumberRange1 = 200000..200299
$NumberRange2 = 206734..206783
$Formattingstart= 'tel:+441209'
$FormattingEnd= ";ext="

#Build E.164 Numbers from ranges
function Get-Number {

       Param(
       [Parameter(Mandatory,ValueFromPipeline=$true)][validateLength(6,6)][validatePattern("[0-9]")][string[]]$number
       )
       Process{
        $number | %% { "$($Formattingstart){0:d3}" -f $_} | %% { "$_$($formattingend)$($_.ToString().Substring(14))" -f $_}
       }
       }

      $CompletedNumbersrange1 = $NumberRange1 | %% {Get-Number -number $_} 
      $CompletedNumbersrange2 = $NumberRange2 | %% {Get-Number -number $_} 


#Connect to Skype and retrieve Numbers in use
try {

    $lyncServer = "Skype.domain.co.uk"

    $sessionOptions = New-PSSessionOption -SkipRevocationCheck -SkipCACheck -SkipCNCheck
    $session = New-PSSession -ConnectionUri https://$lyncServer/ocspowershell `
    -SessionOption $sessionOptions -Authentication NegotiateWithImplicitCredential

    Import-PSSession -session $session

    $SkypeUsers = @()
    $SkypeUsers = (Get-CsUser -Filter {lineuri -like '*'} -ResultSize UNLIMITED | select lineuri).lineuri


    Try{
        filter leftside{
            param(
                    [Parameter(Position=0, Mandatory=$true,ValueFromPipeline = $true)]
                    [ValidateNotNullOrEmpty()]
                    [PSCustomObject]
                    $obj
                )

                $obj|?{$_.sideindicator -eq '<='}

            }
        #Spare Numbers from range1
        $sparerange1 = Compare-Object $CompletedNumbersrange1 $SkypeUsers -IncludeEqual:$false -ExcludeDifferent:$false -PassThru:$true | leftside
        #Spare Numbers From Range2
        $sparerange2 = Compare-Object $CompletedNumbersrange2 $SkypeUsers -IncludeEqual:$false -ExcludeDifferent:$false -PassThru:$true | leftside


        #Combined spare numbers from both ranges
        $rangescombined = $sparerange1+$sparerange2

        #Exclude Reserved Numbers and remove whitespace and non E.164
        $excludereservedNumbers = Compare-Object $reservednumbers $rangescombined -IncludeEqual:$false -PassThru:$true
        [System.Collections.ArrayList]$ArraylistExcludeReservedNumbers = $excludereservedNumbers
        $NEWARRAY = $ArraylistExcludeReservedNumbers.Where({$_.trim() -ne "" -or $null})

        #Assign first spare number
        $nextnumber = $NEWARRAY[0]

        Set-CsUser -Identity "%fullname%" -LineUri "$nextnumber"

        Grant-CsHostedVoicemailPolicy -identity "%fullname%" -PolicyName "Tag:Office365UM" 

        Set-CsUser -Identity "%fullname%" -HostedVoiceMail $True -ErrorAction:Ignore

    }
     catch{}

     try
    {
        $Telephone = $Context.TargetObject.Get("telephoneNumber")
    }
    catch
    {
        $Telephone = $NULL
    }
    if ($Telephone -eq $NULL)
    {
        $Context.TargetObject.Put("telephoneNumber",$nextnumber)
    }
    else
    {
        $Context.LogMessage("Have set the users Skype number to $nextnumber however a number was found in AD $telephone therefore have not updated AD please correct", "Information")
    }
    # Save changes
    $Context.TargetObject.SetInfo()

    #Email count of spare numbers remaining if less than 10
    [System.Collections.ArrayList]$ArrayList =  $excludereservedNumbers
    $ArrayList.Remove("$nextnumber")
    $count = $ArrayList.Count

    IF ($count -lt "400")
    {
        $remainingnumbers = Send-MailMessage -Subject "test" -SmtpServer "exchange.domain.co.uk" -from "adaxes@domain.co.uk" -to "will@domain.co.uk" -Body " There are $count DDI numbers remaining "

        $remainingnumbers

    }
}
finally
{
    remove-pssession $session
}

Related questions

0 votes
1 answer

I'm getting a syntax error on this command and I can't figure out what I'm doing wrong: $criteria = New-AdmCriteria "user" {adm-CustomAttributeInt2 -empty $false -and ... AdmCriteria : The criteria is invalid. Syntax error at position 68. At line:1 char:13

asked Oct 17 by jmatthews (190 points)
0 votes
1 answer

I think this started happening back in August when we updated to the current version (3.13.18.106.0). We are not sure though because this only affects the web page ... We would prefer not to allow access through a firewall for this. Screenshot of the error:

asked Nov 23, 2020 by mark.it.admin (2.3k points)
0 votes
1 answer

Can you point me to documentation to upgrade from Adaxes 2019.2 ver 3.1.2.174230 to the current version please?

asked Jun 16, 2020 by msylvester (60 points)
0 votes
1 answer

The Adaxes web server running on IIS recently stopped working after a reboot. Nothing has changed, it was a routine restart no updates were installed, After the web server ... but from any web interface, it shows this error. Any help would be appreciated.

asked Nov 25 by ADuser (60 points)
0 votes
1 answer

I am trying to remove the administrator drop down on the reset home page and only leave self-service is this possible?

asked Jun 14 by Jeff.Briand (80 points)
3,589 questions
3,278 answers
8,303 comments
548,122 users