#--- no need to change anything below this line.------------
# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}
if ($ServerInstance -eq "") {
# Get Servicelist
$Services = Get-Service | where-object -Property Name -like "MicrosoftDynamicsNAVServer$*" | Where-object -Property Name -Notlike "*VSS*" | Where-object -Property StartType -ne "Disabled"
if ($services.count -eq 0) {
write-host "No services found"
read-host "Press enter to quit script"
return
}
# Select which service to update
$menu = @{}
for ($i = 1; $i -le $services.count; $i++) {
Write-Host "$i. $($services[$i-1].name.split('`$')[1]) ($($services[$i-1].status))"
$menu.Add($i, ($services[$i - 1].name))
}
do {
[int]$answer = Read-Host 'Select the service to update (choose 0 to quit)'
}
until ($answer -le $services.count -and $answer -gt -1)
if ($answer -eq 0) {
write-host "No service selected"
read-host "Press enter to quit script"
return
}
$selection = $menu.Item($answer)
$Serverinstance = (Get-Service $selection).Name.split('$')[1]
}
write-host $ServerInstance
$OdataBaseURL="https://${WebUrlBase}:$OdataPort/$ServerInstance/"
$SoapBaseURL="https://${WebUrlBase}:$SOAPPort/$ServerInstance/"
$WSFederationEndPoint="https://login.microsoftonline.com/$TenantID/wsfed?wa=wsignin1.0%26wtrealm=$appid%26wreply=https://$WebURLBase/$ServerInstance/SignIn"
$ServiceLocation = (get-itemproperty -path HKLM:\SYSTEM\CurrentControlSet\Services\MicrosoftDynamicsNAVServer`$$serverinstance).ImagePath
if ($Servicelocation.indexof('"') -ge 0) {
$ServiceLocation = $ServiceLocation.split('"')[1]
}
$ServiceLocation = Split-path -path $ServiceLocation
if ([string]::IsNullOrWhitespace($ServiceLocation)) {
write-host "Could not locate $ServerInstance"
$ServerInstance = ""
read-host "Press enter to quit script"
return
}
if ([string]::IsNullOrEmpty($CertificateThumbprint)) {
Import-Module WebAdministration
$CertificateThumbprint = (Get-ChildItem IIS:SSLBindings | where-object port -eq $IISPort).thumbprint
Write-Host "Thumbprint found in IIS on port ${IISPort}:${CertificateThumbPrint}"
if ([string]::IsNullOrWhitespace($CertificateThumbprint)) {
"No certificate found on port $IISPort"
return
}
}
get-childitem "$servicelocation\Microsoft.Dynamics.Nav.Management.dll" -recurse | Import-Module -verbose
get-childitem "$servicelocation\Microsoft.Dynamics.Nav.apps.Management.dll" -recurse | Import-Module -verbose
Get-Childitem "$ServiceLocation\NAVWebClientManagement.psm1" -recurse | Import-Module
$Version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$ServiceLocation\Microsoft.Dynamics.Nav.Server.exe").FileVersion
$CurrentVersion = ($Version).split("\.")[0] + "0"
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName ValidAudiences -KeyValue "$ApplicationID; https://api.businesscentral.dynamics.com"
if ($CurrentVersion -lt "220") {
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName ClientServicesFederationMetadataLocation -KeyValue "https://login.microsoftonline.com/$TenantID/FederationMetadata/2007-06/FederationMetadata.xml" # BC20, BC21
} else {
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName ADOpenIDMetadataLocation -KeyValue "https://login.microsoftonline.com/$TenantID/.well-known/openid-configuration" # BC22
}
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName AppIdUri -KeyValue $AppID
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName ClientServicesCredentialType -KeyValue $AuthenticationType
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName WSFederationLoginEndpoint -keyvalue $WSFederationEndpoint
Set-NavServerConfiguration -ServerInstance $ServerInstance -KeyName AzureActiveDirectoryClientId -KeyValue "00000000-0000-0000-0000-000000000000"
Set-NavServerConfiguration -ServerInstance $ServerInstance -KeyName ServicesCertificateThumbprint -KeyValue $CertificateThumbPrint
Set-NavServerConfiguration -ServerInstance $ServerInstance -KeyName PublicWebBaseURL -KeyValue "https://$WebURLBase/$ServerInstance/"
Set-NavServerConfiguration -ServerInstance $ServerInstance -keyname SOAPServicesEnabled -KeyValue False
Set-NavServerConfiguration -ServerInstance $ServerInstance -keyname SOAPServicesPort -keyvalue $SOAPPort
Set-NavServerConfiguration -ServerInstance $ServerInstance -keyname SOAPServicesSSLEnabled -KeyValue True
Set-NavServerConfiguration -ServerInstance $ServerInstance -keyname SOAPServicesEnabled -KeyValue True
Set-NavServerConfiguration -ServerInstance $ServerInstance -KeyName PublicSOAPBaseURL -KeyValue $SoapBaseURL
Set-NavServerConfiguration -ServerInstance $ServerInstance -keyname ODataServicesEnabled -KeyValue False
Set-NavServerConfiguration -ServerInstance $ServerInstance -keyname ODataServicesPort -keyvalue $OdataPort
Set-NavServerConfiguration -ServerInstance $ServerInstance -keyname ODataServicesSSLEnabled -KeyValue True
Set-NavServerConfiguration -ServerInstance $ServerInstance -keyname ODataServicesEnabled -KeyValue True
Set-NavServerConfiguration -ServerInstance $ServerInstance -keyName PublicOdataBaseURL -KeyValue $OdataBaseURL
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName DeveloperServicesEnabled -KeyValue False
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName DeveloperServicesPort -KeyValue 7149
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName DeveloperServicesSSLEnabled -KeyValue True
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName DeveloperServicesEnabled -KeyValue True
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName ApiServicesEnabled -KeyValue true
$ClientServicesPort=Get-NavServerConfiguration -ServerInstance $ServerInstance -keyName ClientServicesPort
$NAVWebServerInstance = (Get-NAVWebServerInstance | Where-Object ServerInstance -eq $ServerInstance).WebServerInstance
Set-NAVWebServerInstanceConfiguration -WebServerInstance $NAVWebServerInstance -KeyName ClientServicesCredentialType -KeyValue $AuthenticationType
Set-NAVWebServerInstanceConfiguration -WebServerInstance $NAVWebServerInstance -KeyName AadApplicationId -KeyValue $ApplicationID
Set-NAVWebServerInstanceConfiguration -WebServerInstance $NAVWebServerInstance -KeyName AadAuthorityUri -KeyValue "https://login.microsoftonline.com/$TenantID"
Set-NAVWebServerInstanceConfiguration -WebServerInstance $NAVWebServerInstance -KeyName ClientServicesPort -KeyValue $ClientServicesPort
Restart-NAVServerInstance -ServerInstance $ServerInstance