Home:ALL Converter>Running powershell scripts from Python without reimporting modules on every run

Running powershell scripts from Python without reimporting modules on every run

Ask Time:2015-07-22T05:45:21         Author:Luke D

Json Formatter

I am creating a Python script that calls a Powershell script script.ps1 that needs to import the Active-Directory module. However, every time I run the powershell script using check_output('powershell.exe -File script.ps1') it needs to re-import the active directory module for each run of script.ps1, which makes the run time take about 3 seconds longer then it needs to.

I was wondering then, if there was a way to keep the Powershell module imported (as if it were being ran directly from Powershell, and not from Python) so that I can use things like

if(-not(Get-Module -name ActiveDirectory)){
  Import-Module ActiveDirectory
}

to speed up execution time.

Author:Luke D,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31550169/running-powershell-scripts-from-python-without-reimporting-modules-on-every-run
yy