VMware Cloud Community
BaskervilleByte
Contributor
Contributor
Jump to solution

Run python script using Powershell loop...

Hi,

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

The python script zerologon_tester.py uses the following syntax:

.\zerologon_tester.py <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){

.\zerologon_tester.py $dc.name $dc.ipv4address

}

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

Many thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you try like this?

$result = python $PyProg $HN $IP


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik, you can run any command in a PS script that you could run at a CMD prompt.

So the code you show should work.

What is the problem?
Doesn't the PS script wait till the Phyton script completes?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BaskervilleByte
Contributor
Contributor
Jump to solution

Hi,

The python script runs, however the variables do not get added to the .py script syntax

Can I assume that this is not possible to mix Ps variables into a python script like this...

.\zerologon_tester.py $dc.name $dc.ipv4address

The error returned is from the Python script:

Tests whether a domain controller is vulnerable to the zerologon attack. Does not attempt to make any changes.

Note: dc-name should be the (NetBIOS) computer name of the domain controller.

Usage: zerologon_tester.py <dc-name> <dc-ip>

Runningpythonusingpowershellzerologonerrorscreenshot.jpg

Thanks!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I personally never mixed PS and python, but this article seems to show how you can pass arguments
How to: Pass Arguments to Python Script via Powershell - Stack Overflow (stackoverflow.com)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BaskervilleByte
Contributor
Contributor
Jump to solution

Hi,

Thanks for the article it did help

Also I did have a typo in one of my select objects

The following script works fine,

Runningpythonusingpowershellzerologonerrorscreenshot2.jpg

The next thing I would like to be able to do is pull the returning lines of output to either a .csv or .txt file

'Success! DC can be fully compromised by a Zerologon attack.'

or

'Attack failed. Target is probably patched.'

Any ideas on how I can do this?

Many, many thanks again!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you try like this?

$result = python $PyProg $HN $IP


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BaskervilleByte
Contributor
Contributor
Jump to solution

Thanks LucD

0 Kudos