Run python script using Powershell loop...


I need to run a Python script within a powershell script but have no idea how to do this

The python script uses the following syntax:

.\ <DomainControllerHostname> <IP>

I want to use a powershell script such as the following and add the domain hostname and IP address into the python script using variables for the syntax, something as follows:

import-module activedirectory
$env:PATHEXT += ";.py"
$dchostname = get-addomaincontroller -filter * | select-object name,ipv4addpress
foreach ($dc in $dchostname){
.\ $ $dc.ipv4address



I also need to make sure that the next execution only follows the completions of the preceding one

Many thanks

Try use python3 <>.

python3 .\

Many thanks for your reply,

I found the following worked ok…

import-module activedirectory
$env:PATHEXT += ";.py";
$PyProg = 'C:\Users\username\Documents\Scripts\Test-ZeroLogon\'
$dchostname = get-addomaincontroller -filter * | select-object name,ipv4address
foreach ($dc in $dchostname){
write-host ''$$dc.ipv4address
$HN = $
$IP = $dc.ipv4address
python $PyProg $HN $IP

However I am now stumped as to output the returning text from the Python script

Any ideas on how I can do this?




Are you referring to capture the output ? you can save it in a variable like $r = python
You can even capture the output from foreach like below

$r = foreach($t in $List){