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
}