Printing a testpage from PowerShell

Todays challenge comes from trying to troubleshoot printers om AX 2012.

Some of the printing flows are not that straight forward and might require printing from a service account. If you do not want to log in as that user to the UI or mabee you are not able to you can use PowerShell to do a test print from the account. This functionality is not available directly in Windows but I found a PS function that does this which I modified a bit (original links below).

This function uses WMIC to list all printers and then send a test print to the printer. You can also use it without modifying the $printername by adding the parameter -printername.

Function out-TestPage
{
Param(
  [string]$printername = “\\printserver\printername”)

  $Printers = Get-CimInstance -ClassName Win32_Printer
  $Printer = $Printers | Where-Object Name -eq "$printername"
 Invoke-CimMethod -MethodName printtestpage -InputObject ($printer)
 Write-Host "Printing to $($printer).Name"
}

To run this as the service account, simply launch PowerShell och Powershell ISE as the account and run the Function Block. Then call the function using out-TestPage -printername “[the name of your printer]”

Links
Use PowerShell to Send Test Page to a Printer – Scripting Blog (microsoft.com)
[SOLVED] Print Test Page from PS – Printers & Scanners – Spiceworks

Build Pipelines with D365FO version 10.0.21

Hi all

Yesterday Microsoft released the PEAP version of 10.0.21 and on the new VM images they have decided to update a whole lot of stuff (Yay!!).

The new VMs are based on Windows Server 2019, SQL Server 2019 and Visual Server 2019 which means that your existing pipelines in Azure Devops will not work any more if you do not do some minor changes. I basically made some adjustments to this article by Joris de Gruyter and added a final step to mitigate a SQL server issue

  • In the Build Solution change MSBuild Version to MSBuild 16.0
  • In the Database Sync step change MSBuild Verson to MSBuild 16.0
  • In the Execute Tests step change Test Platform version till Visual Studio 2019
  • In the script “C:\DynamicsSDK\DeploySSRSReports.ps1” on line 127, change
    Restart-Service -Name “ReportServer” -Force
    to
    Restart-Service -Name “SQLServerReportingServices” -Force
That´s it... the SQL issue will most certanly be fixed in the released version of the VMs        

Links
Updating The Legacy Pipeline for Visual Studio 2017 (codecrib.com)

Package Management in Windows

One of my main envies of Linux in the past years have been Apt-Get – the solution for managing installs and updates of software packages. There have been a couple of different solutions available for Windows historically. The best known one is a third party solution called Chokolatey that you can install and use on Windows and which has a huge repository of software available. The issue I had with Chokolatey was that is was not built-in to Windows… It felt a bit off having to install software to be able to install software.

A couple of years ago Microsoft included OneGet in Microsoft Powershell and I tried is a couple of times but being a bit lazy as I am I always felt it was a bit over-complicated. I never got the hang of having to install an trust providors and repositories. Since I mainly do this once when I reinstall a computer I never really found it worth the time to learn how to do this.

Fast forward to May 2020 when Microsoft introduced Winget. It was first made available as an install from Windows Store or directly from Github and from around May 2021 it is included in Windows as a default with no extra install required.

Yesterday when I reinstalled my computer I thought I would give is a shot.

To find software you use winget search. You can for instance type “winget search microsoft.” (Note the . at the end) to see all Microsoft software in the repository.

When you see the list of Microsoft packages you see that a lot of the regular downloadable packages such as Powershell and OneDrive. You will also find Microsoft Office, Teams and Visual Studio. All of Microsoft’s redistributable packges for supporting .NET and C++ are also available if you have any pre-requirement packages. A lot of the software might require a license which you will have to provide… when you for instance install Office you log into it as usual to provide your license informaton.

To install software you use the command winget install. You could for instance use winget install PowerToys to install Microsoft PowerToys or winget install “Microsoft vscode-insiders” to install Visual Studio Code Insider version. Note that you can use any of the information in the search results to identify which package should be installed.

Winget also handles the update of packages. To upgrade a specific package you can use winget upgrade [package name] or just use winget upgrade to upgrade all packages.

I really like what I see so far… 🙂

That is all for today

Understanding $env:PSModulePath

OK… So this is probably old news for most of you but I thought I would document this here mostly for me.

When you start a powershell prompt whether it being the old and battletested version 5.1, the brand new 7.x or if you are running the cool new windows terminal or my favorite VS Code you will get a couple of environemnt variables set by default. One of these are $env:PSModulePath. The problem is that they look a little different depending on which application you look in.

PowerShell 5.1:
C:\Users\[UserName]\Documents\WindowsPowerShell\Modules;
C:\Program Files\WindowsPowerShell\Modules;
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

One folder for the current user and 2 system wide folders

Powershell 7:
C:\Users\[UserName]\Documents\PowerShell\Modules;
C:\Program Files\PowerShell\Modules;
c:\program files\powershell\7\Modules;
C:\Program Files\WindowsPowerShell\Modules;
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

A couple of notable differences. PS7 uses PowerShell in your documents folder while PS5 uses WindowsPowerShell. If you have set up OneDrive with known folder redirection for My Documents the first path will instead start with C:\Users\[UserName]\[OneDrive folder]\Documents. Lastly version 7 also migrates the system wide folders for version 5.1 and adds them to the variable so your old Modules will be available there as well. You will see four system folders and one for the user. You might have issues with the OneDrive storage if you do not set the folder to “Always keep on this device”

If you are running VS Code the Powershell Addin will add its own folder called c:\Users\[UserName]\.vscode-insiders\extensions\ms-vscode.powershell-preview-2020.9.0\modules


Another notable thing with VS Code Preview is that you have two different consoles for PowerShell. One called PowerShell and one called PowerShell Integrated console, the first one runs version 5.1 and the second runs 7.

The all new Windows Terminal of course also has the option to run both version

That was it for today…

Johan

Redeploying SSRS reports in D365FO without using Visual Studio

Since I am not a developer… especially not an X++ developer I am ususlly not entrusted with access to Visual Studio (this statement was more for dramatic effect but the truth is that I am trying to avoid it to not mess things up).

The main reason for this work around is that in some environments Visual Studio is not set up and to do that would require setting up the correct account, mapping workspaces and a whole lot more. This way is simpler:

  1. Log into the Dynamics Server
  2. Start PowerShell Elevated as Administrator
  3. Run the following command:

    K:\AosService\PackagesLocalDirectory\Plugins\AxReportVmRoleStartupTask\DeployAllReportsToSSRS.ps1 -PackageInstallLocation “K:\AosService\PackagesLocalDirectory”

That will redeploy all of the for Dynamics 365 for Operations

Links:
https://msdax.wordpress.com/2018/04/08/deploying-ssrs-reports-to-dynamics-ax-365/

RSAT: Failed to download or overwrite attachment files – Some files maybe in use.

I had an issue today with one of my testcases in RSAT. When running it manyally from the RSAT UI it worked but when trying to run it from a PowerShell script I got this error:

Failed to download or overwrite attachment files – Some files maybe in use.

For some reason the test case uploaded duplicate files to Azure DevOps. When I ran the test in RSAT it uses the local file generated from RSAT and everything worked but when I ran it from the console it downloaded the files from DevOps, got the duplicates and for some reason failed.

The solution was to permanently delete the test case from DevOps and resync it from BPM.

Beware that that will give it another ID which will change the order of the tests.

Getting started with VS Code – for a developer wannabe – Part 1

I am a developer… in the same was as I am a gamer and a harmonica player… I really, really really want to be one… I am not good at it, but I really want to be one. I have over the years tried my hand at Powershell, Yaml, BAT- files and even good old kix scripts and much lite an amateur golfer I try to tell my self that if I just have the right tool I will really get good at this… (I really want to believe that 🙂 )

When it comes to PowerShell I have been an avid user of PowerShell ISE but since Microsoft is not doing much of anything with ISE anymore and they are pushing VS Code more and more for everything I thought what the heck… This is my noob guide to getting started with VS Code (for someone that has only used ISE).

In this post I will show you how I set it up and I will probably follow up wit other posts on how I got started.

First you need to download and install VS Code itself:

  1. Go here to download
  2. I checked the options
    Add “Open with Code” action to Windows Explorer file context meny
    Add “Open with Code” action to Windows Explorer directory context meny
    Register Code as an editor for supported file types
  3. Finish the install and Launch VS Code

When you start VS Code for the first time it will look like this. To get started with PowerShell there is at least one plugin you will need… PowerShell (unexpected right). This is used for providing snippets and high lighting code

4. Click the small cubes at the bottom of the toolbar on the left

5. Type PowerShell in the search bar, select it and press the small green install

The plugin even makes VS Code look a bit like ISE. I usually set it back to Dark (that is what coders do 🙂 )

That is it for today. I will continue with more tips and tricks for getting started with VS Code.

Bye

Johan

Downloading Microsoft Ignite Sessions

This year I was not able to visit Microsoft Ignite 2019 but since there are more than 1900 session available for on demand viewing (and I would not have had time to watch the sessions I wanted anyway) that doesn’t really matter. I can always download them and watch them at home. If you hesitate on downloading all of them you can find your sessions/products/speakers of interest on the Ignite site 

To bulk download them you will need a script. I use this one but there are others out there that you can use. For this particular script to work you will need a tool called youtube-dl (which in turn requires Microsoft Visual C++ 2010 Redistributable Package (x86) ) and ffmpeg.exe. both tools will be downloaded by the script. In order to run scripts you download (this is done your own risk… only run scripts you trust) you will also need to set the execution policy in PowerShell

I do it for just the specific window that I use:
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

To download the session I use these script lines:

.\Get-EventSession.ps1 -DownloadFolder 'D:\Microsoft Ignite 2019\' -Product "*Finance*" -Event Ignite -NoSlidedecks
.\Get-EventSession.ps1 -DownloadFolder 'D:\Microsoft Ignite 2019\' -Product "Power Platform" -Event Ignite -NoSlidedecks
.\Get-EventSession.ps1 -DownloadFolder 'D:\Microsoft Ignite 2019\' -Speaker "Jeffrey Snover" -Event Ignite -NoSlidedecks
.\Get-EventSession.ps1 -DownloadFolder 'D:\Microsoft Ignite 2019\' -Speaker "Mark Russinovich" -Event Ignite -NoSlidedecks
.\Get-EventSession.ps1 -DownloadFolder 'D:\Microsoft Ignite 2019\' -Product "Not Product Specific" -Event Ignite -NoSlidedecks

There are a lot of other filter possibilities in the script that you can explore… now, get downloading

Johan

Links:
https://gallery.technet.microsoft.com/Ignite-2016-Slidedeck-and-296df316#content

Error in Environment Reprovisioning tool

I have been working with an upgrade of Dynamics 365 for Operations now for a while and found an issue when it comes to Environment Reprovisioning tool, also known as Retail Retargeting Tool. This is a tool used to fix the retail conponents in an environment after you have done a database copy to the environment from another environment. We did this as part of the upgrade to Platfor Update 10.

When we ran the tool from the command prompt in the environemnt we gor the following error:

The step completed
Executing step: 3
GlobalUpdate script for service model: RetailServer on machine: localhost
Run RetargetRetailServer.ps1
RetargetRetailServer.ps1 failed.
The step failed
The step: 3 is in failed state, you can use rerunstep command to debug the step explicitly
at Microsoft.Dynamics.AX.AXUpdateInstallerBase.RunbookExecutor.executeRunbookStepList(RunbookData runbookData, List`1 runbookStepList, String updatePackageFilePath, Boolean silent, String stepID, ExecuteStepMode executeStepMode, Boolean versionCheck, Boolean restore, Parameters parameters)
at Microsoft.Dynamics.AX.AXUpdateInstallerBase.RunbookExecutor.executeRunbook(RunbookData runbookData, String updatePackageFilePath, Boolean silent, String stepID, ExecuteStepMode executeStepMode, Boolean versionCheck, Boolean restore, Parameters parameters)
at Microsoft.Dynamics.AX.AXUpdateInstallerBase.AXUpdateInstallerBase.execute(String runbookID, Boolean silent, String updatePackageFilePath, IRunbookExecutor runbookExecutor, Boolean versionCheck, Boolean restore)
at Microsoft.Dynamics.AX.AXUpdateInstaller.Program.InstallUpdate(String[] args)
at Microsoft.Dynamics.AX.AXUpdateInstaller.Program.Main(String[] args)

So this error was not that obvious to me… So I took a look at the loggs for the Deployable package which are located in the RunbookWorkingFolder (located in the folder where you extracted the package). There I found this:

Exception : System.Management.Automation.RuntimeException: The servicing data of this box has not been migrated yet, please rerun this tool with axlocaladmin
TargetObject : The servicing data of this box has not been migrated yet, please rerun this tool with axlocaladmin

This made me think… The older environments always had an account called axlocaladmin but the newer ones does not. After some digging around in the script called RetargetRetailServer.ps1 I found this (on line 302) :

if($env:UserName -ne 'axlocaladmin')
{
    $errorMessage = "The servicing data of this box has not been migrated yet, please rerun this tool with axlocaladmin"
    Log-TimedMessage $errorMessage
    throw $errorMessage
}

I simply changed it to:

if($env:UserName -ne 'adminXXXXXXXXXX')
{
    $errorMessage = "The servicing data of this box has not been migrated yet, please rerun this tool with axlocaladmin"
    Log-TimedMessage $errorMessage
    throw $errorMessage
    
}

where adminXXXXXXXXXX is the account name for my user and the i re-ran:

AXUpdateInstaller.exe execute -runbookid=env-reprovision -rerunstep=3

And presto… it worked 🙂

Note that this is the reprovisioning tool from 8/30/2017, I have not looked at any other verisons.

/Johan

Database Syncing

Hi…

Today we are doing a DB upgrade in Dynamics 365 for Operations. One of the steps in the process includes installing a hotfix which in turn triggers a database sync. The Sync failed so I tried to find a way to run it manually and since the environment is a TEST environment it does not have Visual Studio. Powershell to the rescue:

I:\AosService\WebRoot\bin\Microsoft.Dynamics.AX.Deployment.Setup.exe -bindir "G:\AosService\PackagesLocalDirectory" -metadatadir "G:\AosService\PackagesLocalDirectory" -sqluser "axdbadmin" -sqlpwd "[PasswordForAxDBAdmin]" -sqlserver "Servername.database.windows.net" -sqldatabase "[SQLAzureDB]" -setupmode "sync" -syncmode "fullall" -isazuresql "true"

 

Links:
https://community.dynamics.com/ax/b/axilitynet/archive/2016/01/28/ax7-database-synchronization-w-o-visual-studio