Git/Open-NavWindowsClient.ps1
function Open-NavWindowsClient { Param( [Parameter(Mandatory=$false)] [string]$ServerInstance = '', [Parameter(Mandatory=$false)] [ValidateSet('Table','Page','Report','Codeunit','Query','XMLPort')] [string]$ObjectType, [Parameter(Mandatory=$false)] [int]$ObjectID, [Parameter(Mandatory=$false)] [ValidateSet('Hidden','Minimized','Maximized','Normal')] [string]$WindowStyle = 'Normal', [Parameter(Mandatory=$false)] [string]$ContainerName = '', [Parameter(Mandatory=$false)] [System.Management.Automation.PSCredential]$Credential ) if ($ContainerName -ne '' -and $ServerInstance -eq '') { $ServerInstance = 'NAV' } if ($ServerInstance -eq '') { $ServerInstance = Select-ServiceTier } if ($ServerInstance -eq '') { return } if ($ContainerName -eq '') { $ClientPath = Get-ClientPathForServerInstace $ServerInstance $ServerName = 'localhost' } else { $ClientPath = (Get-ChildItem "C:\ProgramData\NavContainerHelper\Extensions\$ContainerName\Program Files\*\RoleTailored Client").FullName $ServerName = $ContainerName } $ClientUserSettingsFile = Join-Path (Create-TempDirectory) 'ClientUserSettings.config' Add-Content -Path $ClientUserSettingsFile -Value (Get-ClientUserSettingsContentForServerInstance $ServerInstance -ContainerName $ContainerName) if ($ObjectID -gt 0) { $ConnectionString = "dynamicsnav://$ServerName" + ":7046/$ServerInstance//Run$ObjectType" + "?$ObjectType=$ObjectID" } else { $ConnectionString = "dynamicsnav://$ServerName" + ":7046/$ServerInstance//" } $Process = Start-Process -FilePath (Join-Path $ClientPath 'Microsoft.Dynamics.Nav.Client.exe') -ArgumentList ($ConnectionString, ('-settings:"{0}"' -f $ClientUserSettingsFile)) -WindowStyle $WindowStyle -PassThru if ($Credential -ne $null) { #send Y to surpress prompt about connecting to unusual service tier when connecting to container if ($ContainerName -ne '') { [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") sleep -Seconds 3 Add-Type ' using System; using System.Runtime.InteropServices; public class SFW { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetForegroundWindow(IntPtr hWnd); }' [SFW]::SetForegroundWindow($Process.MainWindowHandle) sleep -Seconds 1 [System.Windows.Forms.SendKeys]::SendWait($Credential.UserName) [System.Windows.Forms.SendKeys]::SendWait('{TAB}') [System.Windows.Forms.SendKeys]::SendWait([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password))) [System.Windows.Forms.SendKeys]::SendWait('{ENTER}') } } $Process } Export-ModuleMember -Function Open-NavWindowsClient |