Tips for optimizing scripts during spotty connectivity issues during automation?

Hey all, first post here

Ive developed a script that utilizes output from Configuration Manager. The script runs as a scheduled task from a dedicated “automation” server. I plan to move this to Azure Automation, but for now this is what I have to use.

The server that im running the script from has the Configuration Manager console installed on it because I had a lot of issues using Invoke-Command directly to the ConfigMgr primary site server. This simplified getting the ConfigurationManager powershell module to load.

Anyway,

  1. The first step is importing the ConfigurationManager module. Sometimes when loading it, I get an error. So I put the Import-Module cmdlet inside of a Do Until loop. The Until condition is if((Get-Module).Where({ $_.Name -eq ‘ConfigurationManager’ }))

  2. Then I have to create a New-PSDrive. Sometimes this runs into a “RPC connection” error. So once again I out this in a Do Until loop with the Until condition being if((Get-PSDrive).Where({ $_.Name -eq ‘SiteCode’ })) where the site code is the ConfigMgr site code for the primary site.

Once these steps are done, I can run the rest of my code with no issues.

My question is, is this the best way to handle this scenario? Obviously it meets my needs and works, but I was just curious if anyone had any best practices or ways I can improve how I handle this scenario. I still feel a little uncomfortable with how this is working because my script did error out ONE TIME and I havent been able to reproduce the issue. I fear it will happen again and want to try and make it easy to troubleshoot in the future. I know it’s hard to solve a problem if I dont know exactly why it errored out, but I figured I would just get the question out there in the event theres something I might be missing.

Thanks in advance!

I’ll start by asking what are the error message?

The error I get is ‘Import-Module: The RPM server is unavailable’. This CAN happen (not always) for the following two commands:

Import-Module -Name 'ConfigurationManager'
New-PSDrive -Name 'SiteName' -PSProvider 'CMSite' -Root 'primarysiteserver.domain.com' -Description 'some description'

The thing is… this error doesn’t always happen… just sometimes. And even when it does, at least for the Import-Module command, if I get the error… it doesn’t seem to make a difference… the module STILL is imported and I can begin running ConfigurationManager module commands.

If I get the error for the New-PSDrive command, then my script has to stop wholesale.

I’ve resorted to adding '-ErrorAction ‘SilentlyContinue’ on each command inside of each of their Do Until loops. At least the Until condition can verify that the module is actually loaded and that the PSDrive is actually in existence before the script continues.

Once again, this might be the best/only way to handle this, but I hate having to use -ErrorAction ‘SilentlyContinue’ on anything at all. I would prefer to use -ErrorAction ‘Stop’ and figure out a way to prevent the issue from happening in the first place. If I’m already handling this the best way possible, then that’s fine. I was just hoping there was something more explicit I could do. This feels… shoddy to some degree. I don’t like it. But I don’t have to like it if it works and it’s serving my needs I guess.

Hmm well with spotty connectivity you’re at the mercy of your connection quality. This becomes more of a networking issue rather than Powershell. You might be able to test whether or not RPC is reachable on the DP, but don’t have SCCM available for testing. RPC is on port 135 so a success for Test-NetConnection -ComputerName <DP server> -Port 135

Yeah fair enough. I agree that we should just fix the network spottiness, whether we solve that problem or not, this at least could be a case of building resiliency into my code.

So from a resiliency point of view, for this specific problem, would the Do Until loop be the way to go? Am I overthinking this?

If it solves the problem I would just stick with that approach. No need to make it overly complicated as this approach shouldn’t really impact the system in anyway. You may want to add something like a counter such as RetryCount and increment and have it fail on X number of attempts so you don’t risk getting stuck in an infinite loop.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.