Convert Batch to PowerShell Calling Python Scripts
I have a Batch script that calls Python then runs a Python program.
Instead of using a Batch script to do that, I would like to use PowerShell.
I am not a programmer nor scripter and I have Googled and researched without any luck as to how to get the correct syntax.
In testing, I have used a few commands: invoke-item, invoke-command, set- and get-variable, and some others I have forgotten, etc.
Can someone please provide me with the correct PowerShell syntax to call first, the Python executable, then the Python script itself?
The abbreviated Batch script is embedded along with the Python script which works as it should.
Thanks in advance.
echo off
set winpy=C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64
if EXIST %winpy% (
"%winpy%python.exe" "%~dp0HBSS_XML_Parser_Driver_Action.py" )
Called Python script
import HBSS_Classes, os, HIPS_8_FW_XML_Parser_Action
hbss_parser = HBSS_Classes.HBSSXMLParser()
xmlfiles =
print('############################')
print('# UNDER ACTIVE DEVELOPMENT #')
print('############################n')
print("HBSS XML PARSER - DEVELOPED BY WADE, TIMOTHY J.")
print("This CLI application will create human readable CSV spreadsheets
fromn""XML files directly exported from the McAfee ePolicy Orchestrator.")
print("nSupported products:n")
for each_entry in hbss_parser.product_dict:
print(" " + each_entry)
print(" HOSTIPS_8000_FW")
print("nThe following files will be checked against supported "
"XML file types:n")
for each_file in os.listdir():
if each_file.endswith('.xml'):
xmlfiles.append(each_file)
print(each_file)
date_check = input("nFor HIPS 8 FW Policies only, would you also like to create an additional n.CSV file containing only new rules created/modified since a given date? n(For all other policies, enter N): Y/N ")
if (date_check.lower().startswith('y')):
date_input = input("nFile will only contain new rules created/modified sincen MM-DD-YYYY? (include hyphens when entering date value): ")
sincedate = True
else:
sincedate = False
begin_check = input("nBegin parsing to CSV? Y/N ")
if (begin_check.lower().startswith('y')):
hbss_parser.currentDirXMLListBLDR()
hbss_parser.hbssObjectListBLDR()
hbss_parser.hbssObjParseToCSV()
dirfiles = os.listdir()
for xmlfile in xmlfiles:
if (xmlfile[:-4] + '_CSV.csv') in dirfiles:
pass
else:
try:
args = ['HIPS_8_FW_XML_Parser_Action.py', xmlfile]
HIPS_8_FW_XML_Parser_Action.main(args, True)
if sincedate:
args = ['HIPS_8_FW_XML_Parser_Action.py', xmlfile, date_input]
HIPS_8_FW_XML_Parser_Action.main(args, True)
except:
pass
else:
pass
batch python powershell-5.0
add a comment |
I have a Batch script that calls Python then runs a Python program.
Instead of using a Batch script to do that, I would like to use PowerShell.
I am not a programmer nor scripter and I have Googled and researched without any luck as to how to get the correct syntax.
In testing, I have used a few commands: invoke-item, invoke-command, set- and get-variable, and some others I have forgotten, etc.
Can someone please provide me with the correct PowerShell syntax to call first, the Python executable, then the Python script itself?
The abbreviated Batch script is embedded along with the Python script which works as it should.
Thanks in advance.
echo off
set winpy=C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64
if EXIST %winpy% (
"%winpy%python.exe" "%~dp0HBSS_XML_Parser_Driver_Action.py" )
Called Python script
import HBSS_Classes, os, HIPS_8_FW_XML_Parser_Action
hbss_parser = HBSS_Classes.HBSSXMLParser()
xmlfiles =
print('############################')
print('# UNDER ACTIVE DEVELOPMENT #')
print('############################n')
print("HBSS XML PARSER - DEVELOPED BY WADE, TIMOTHY J.")
print("This CLI application will create human readable CSV spreadsheets
fromn""XML files directly exported from the McAfee ePolicy Orchestrator.")
print("nSupported products:n")
for each_entry in hbss_parser.product_dict:
print(" " + each_entry)
print(" HOSTIPS_8000_FW")
print("nThe following files will be checked against supported "
"XML file types:n")
for each_file in os.listdir():
if each_file.endswith('.xml'):
xmlfiles.append(each_file)
print(each_file)
date_check = input("nFor HIPS 8 FW Policies only, would you also like to create an additional n.CSV file containing only new rules created/modified since a given date? n(For all other policies, enter N): Y/N ")
if (date_check.lower().startswith('y')):
date_input = input("nFile will only contain new rules created/modified sincen MM-DD-YYYY? (include hyphens when entering date value): ")
sincedate = True
else:
sincedate = False
begin_check = input("nBegin parsing to CSV? Y/N ")
if (begin_check.lower().startswith('y')):
hbss_parser.currentDirXMLListBLDR()
hbss_parser.hbssObjectListBLDR()
hbss_parser.hbssObjParseToCSV()
dirfiles = os.listdir()
for xmlfile in xmlfiles:
if (xmlfile[:-4] + '_CSV.csv') in dirfiles:
pass
else:
try:
args = ['HIPS_8_FW_XML_Parser_Action.py', xmlfile]
HIPS_8_FW_XML_Parser_Action.main(args, True)
if sincedate:
args = ['HIPS_8_FW_XML_Parser_Action.py', xmlfile, date_input]
HIPS_8_FW_XML_Parser_Action.main(args, True)
except:
pass
else:
pass
batch python powershell-5.0
“I am not a programmer or scripter...” then you must become one if you want to do programming and scripting. This seems like a good place to start. That is a really basic batch file. First, break it down and research until you know exactly what it is doing. Next, research until you figure out how to replicate that behavior in powershell. Someone may come along and simply build a script for you, but research and self-learning is usually encouraged. This is not an insurmountable task. The actual python script is not relevant here.
– Appleoddity
Dec 17 '18 at 5:48
Specific questions about what exactly the batch file is doing, demonstrating your own research and understanding of it, would be far more effective here. Followed up by more questions about doing specific things in Powershell. It’s the whole “teach a man to fish” thing. :)
– Appleoddity
Dec 17 '18 at 5:52
The batch script calls the Python executable which executes the Python script that displays the options to parse an XML file into a CSV file. The variable statements are optional and included only for varying Python installations. As stated, I have tried several different types of PowerShell options with minimal success. As stated, I have tried several different types of PowerShell options with minimal success. The invoke-item command presents the Python prompt and in a separate window the actual Python script displaying the options but the resultant CSV file is not created.
– user714888
Dec 20 '18 at 4:24
I have tried other PowerShell commands (invoke-command, start-process) with varying success in executing the files but none worked in varying combinations of creating the CSV output file. In actuality, the XML is not being printed in the newer test scripts, basically, the XML file(s) is not being queued to be parsed. I am studying, learning, reading, and viewing training materials now to learn PowerShell but requesting assistance now so I can put it to use immediately for work. I initially was not going to post the Python script either but did in case someone did, however, want to see it.
– user714888
Dec 20 '18 at 4:24
So instead of using the batch script to effectively provide the GUI for processing the Python files, I want the GUI use PowerShell. Specific questions: 1. What are the proper commands to use to create the PowerShell that performs the same functions as the batch script? 2. How do I get the XML to print in the GUI so that it is seen as the file to be parsed? 3. Do you have any training materials you recommend? Thank you
– user714888
Dec 20 '18 at 4:24
add a comment |
I have a Batch script that calls Python then runs a Python program.
Instead of using a Batch script to do that, I would like to use PowerShell.
I am not a programmer nor scripter and I have Googled and researched without any luck as to how to get the correct syntax.
In testing, I have used a few commands: invoke-item, invoke-command, set- and get-variable, and some others I have forgotten, etc.
Can someone please provide me with the correct PowerShell syntax to call first, the Python executable, then the Python script itself?
The abbreviated Batch script is embedded along with the Python script which works as it should.
Thanks in advance.
echo off
set winpy=C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64
if EXIST %winpy% (
"%winpy%python.exe" "%~dp0HBSS_XML_Parser_Driver_Action.py" )
Called Python script
import HBSS_Classes, os, HIPS_8_FW_XML_Parser_Action
hbss_parser = HBSS_Classes.HBSSXMLParser()
xmlfiles =
print('############################')
print('# UNDER ACTIVE DEVELOPMENT #')
print('############################n')
print("HBSS XML PARSER - DEVELOPED BY WADE, TIMOTHY J.")
print("This CLI application will create human readable CSV spreadsheets
fromn""XML files directly exported from the McAfee ePolicy Orchestrator.")
print("nSupported products:n")
for each_entry in hbss_parser.product_dict:
print(" " + each_entry)
print(" HOSTIPS_8000_FW")
print("nThe following files will be checked against supported "
"XML file types:n")
for each_file in os.listdir():
if each_file.endswith('.xml'):
xmlfiles.append(each_file)
print(each_file)
date_check = input("nFor HIPS 8 FW Policies only, would you also like to create an additional n.CSV file containing only new rules created/modified since a given date? n(For all other policies, enter N): Y/N ")
if (date_check.lower().startswith('y')):
date_input = input("nFile will only contain new rules created/modified sincen MM-DD-YYYY? (include hyphens when entering date value): ")
sincedate = True
else:
sincedate = False
begin_check = input("nBegin parsing to CSV? Y/N ")
if (begin_check.lower().startswith('y')):
hbss_parser.currentDirXMLListBLDR()
hbss_parser.hbssObjectListBLDR()
hbss_parser.hbssObjParseToCSV()
dirfiles = os.listdir()
for xmlfile in xmlfiles:
if (xmlfile[:-4] + '_CSV.csv') in dirfiles:
pass
else:
try:
args = ['HIPS_8_FW_XML_Parser_Action.py', xmlfile]
HIPS_8_FW_XML_Parser_Action.main(args, True)
if sincedate:
args = ['HIPS_8_FW_XML_Parser_Action.py', xmlfile, date_input]
HIPS_8_FW_XML_Parser_Action.main(args, True)
except:
pass
else:
pass
batch python powershell-5.0
I have a Batch script that calls Python then runs a Python program.
Instead of using a Batch script to do that, I would like to use PowerShell.
I am not a programmer nor scripter and I have Googled and researched without any luck as to how to get the correct syntax.
In testing, I have used a few commands: invoke-item, invoke-command, set- and get-variable, and some others I have forgotten, etc.
Can someone please provide me with the correct PowerShell syntax to call first, the Python executable, then the Python script itself?
The abbreviated Batch script is embedded along with the Python script which works as it should.
Thanks in advance.
echo off
set winpy=C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64
if EXIST %winpy% (
"%winpy%python.exe" "%~dp0HBSS_XML_Parser_Driver_Action.py" )
Called Python script
import HBSS_Classes, os, HIPS_8_FW_XML_Parser_Action
hbss_parser = HBSS_Classes.HBSSXMLParser()
xmlfiles =
print('############################')
print('# UNDER ACTIVE DEVELOPMENT #')
print('############################n')
print("HBSS XML PARSER - DEVELOPED BY WADE, TIMOTHY J.")
print("This CLI application will create human readable CSV spreadsheets
fromn""XML files directly exported from the McAfee ePolicy Orchestrator.")
print("nSupported products:n")
for each_entry in hbss_parser.product_dict:
print(" " + each_entry)
print(" HOSTIPS_8000_FW")
print("nThe following files will be checked against supported "
"XML file types:n")
for each_file in os.listdir():
if each_file.endswith('.xml'):
xmlfiles.append(each_file)
print(each_file)
date_check = input("nFor HIPS 8 FW Policies only, would you also like to create an additional n.CSV file containing only new rules created/modified since a given date? n(For all other policies, enter N): Y/N ")
if (date_check.lower().startswith('y')):
date_input = input("nFile will only contain new rules created/modified sincen MM-DD-YYYY? (include hyphens when entering date value): ")
sincedate = True
else:
sincedate = False
begin_check = input("nBegin parsing to CSV? Y/N ")
if (begin_check.lower().startswith('y')):
hbss_parser.currentDirXMLListBLDR()
hbss_parser.hbssObjectListBLDR()
hbss_parser.hbssObjParseToCSV()
dirfiles = os.listdir()
for xmlfile in xmlfiles:
if (xmlfile[:-4] + '_CSV.csv') in dirfiles:
pass
else:
try:
args = ['HIPS_8_FW_XML_Parser_Action.py', xmlfile]
HIPS_8_FW_XML_Parser_Action.main(args, True)
if sincedate:
args = ['HIPS_8_FW_XML_Parser_Action.py', xmlfile, date_input]
HIPS_8_FW_XML_Parser_Action.main(args, True)
except:
pass
else:
pass
batch python powershell-5.0
batch python powershell-5.0
asked Dec 17 '18 at 5:00
user714888user714888
32
32
“I am not a programmer or scripter...” then you must become one if you want to do programming and scripting. This seems like a good place to start. That is a really basic batch file. First, break it down and research until you know exactly what it is doing. Next, research until you figure out how to replicate that behavior in powershell. Someone may come along and simply build a script for you, but research and self-learning is usually encouraged. This is not an insurmountable task. The actual python script is not relevant here.
– Appleoddity
Dec 17 '18 at 5:48
Specific questions about what exactly the batch file is doing, demonstrating your own research and understanding of it, would be far more effective here. Followed up by more questions about doing specific things in Powershell. It’s the whole “teach a man to fish” thing. :)
– Appleoddity
Dec 17 '18 at 5:52
The batch script calls the Python executable which executes the Python script that displays the options to parse an XML file into a CSV file. The variable statements are optional and included only for varying Python installations. As stated, I have tried several different types of PowerShell options with minimal success. As stated, I have tried several different types of PowerShell options with minimal success. The invoke-item command presents the Python prompt and in a separate window the actual Python script displaying the options but the resultant CSV file is not created.
– user714888
Dec 20 '18 at 4:24
I have tried other PowerShell commands (invoke-command, start-process) with varying success in executing the files but none worked in varying combinations of creating the CSV output file. In actuality, the XML is not being printed in the newer test scripts, basically, the XML file(s) is not being queued to be parsed. I am studying, learning, reading, and viewing training materials now to learn PowerShell but requesting assistance now so I can put it to use immediately for work. I initially was not going to post the Python script either but did in case someone did, however, want to see it.
– user714888
Dec 20 '18 at 4:24
So instead of using the batch script to effectively provide the GUI for processing the Python files, I want the GUI use PowerShell. Specific questions: 1. What are the proper commands to use to create the PowerShell that performs the same functions as the batch script? 2. How do I get the XML to print in the GUI so that it is seen as the file to be parsed? 3. Do you have any training materials you recommend? Thank you
– user714888
Dec 20 '18 at 4:24
add a comment |
“I am not a programmer or scripter...” then you must become one if you want to do programming and scripting. This seems like a good place to start. That is a really basic batch file. First, break it down and research until you know exactly what it is doing. Next, research until you figure out how to replicate that behavior in powershell. Someone may come along and simply build a script for you, but research and self-learning is usually encouraged. This is not an insurmountable task. The actual python script is not relevant here.
– Appleoddity
Dec 17 '18 at 5:48
Specific questions about what exactly the batch file is doing, demonstrating your own research and understanding of it, would be far more effective here. Followed up by more questions about doing specific things in Powershell. It’s the whole “teach a man to fish” thing. :)
– Appleoddity
Dec 17 '18 at 5:52
The batch script calls the Python executable which executes the Python script that displays the options to parse an XML file into a CSV file. The variable statements are optional and included only for varying Python installations. As stated, I have tried several different types of PowerShell options with minimal success. As stated, I have tried several different types of PowerShell options with minimal success. The invoke-item command presents the Python prompt and in a separate window the actual Python script displaying the options but the resultant CSV file is not created.
– user714888
Dec 20 '18 at 4:24
I have tried other PowerShell commands (invoke-command, start-process) with varying success in executing the files but none worked in varying combinations of creating the CSV output file. In actuality, the XML is not being printed in the newer test scripts, basically, the XML file(s) is not being queued to be parsed. I am studying, learning, reading, and viewing training materials now to learn PowerShell but requesting assistance now so I can put it to use immediately for work. I initially was not going to post the Python script either but did in case someone did, however, want to see it.
– user714888
Dec 20 '18 at 4:24
So instead of using the batch script to effectively provide the GUI for processing the Python files, I want the GUI use PowerShell. Specific questions: 1. What are the proper commands to use to create the PowerShell that performs the same functions as the batch script? 2. How do I get the XML to print in the GUI so that it is seen as the file to be parsed? 3. Do you have any training materials you recommend? Thank you
– user714888
Dec 20 '18 at 4:24
“I am not a programmer or scripter...” then you must become one if you want to do programming and scripting. This seems like a good place to start. That is a really basic batch file. First, break it down and research until you know exactly what it is doing. Next, research until you figure out how to replicate that behavior in powershell. Someone may come along and simply build a script for you, but research and self-learning is usually encouraged. This is not an insurmountable task. The actual python script is not relevant here.
– Appleoddity
Dec 17 '18 at 5:48
“I am not a programmer or scripter...” then you must become one if you want to do programming and scripting. This seems like a good place to start. That is a really basic batch file. First, break it down and research until you know exactly what it is doing. Next, research until you figure out how to replicate that behavior in powershell. Someone may come along and simply build a script for you, but research and self-learning is usually encouraged. This is not an insurmountable task. The actual python script is not relevant here.
– Appleoddity
Dec 17 '18 at 5:48
Specific questions about what exactly the batch file is doing, demonstrating your own research and understanding of it, would be far more effective here. Followed up by more questions about doing specific things in Powershell. It’s the whole “teach a man to fish” thing. :)
– Appleoddity
Dec 17 '18 at 5:52
Specific questions about what exactly the batch file is doing, demonstrating your own research and understanding of it, would be far more effective here. Followed up by more questions about doing specific things in Powershell. It’s the whole “teach a man to fish” thing. :)
– Appleoddity
Dec 17 '18 at 5:52
The batch script calls the Python executable which executes the Python script that displays the options to parse an XML file into a CSV file. The variable statements are optional and included only for varying Python installations. As stated, I have tried several different types of PowerShell options with minimal success. As stated, I have tried several different types of PowerShell options with minimal success. The invoke-item command presents the Python prompt and in a separate window the actual Python script displaying the options but the resultant CSV file is not created.
– user714888
Dec 20 '18 at 4:24
The batch script calls the Python executable which executes the Python script that displays the options to parse an XML file into a CSV file. The variable statements are optional and included only for varying Python installations. As stated, I have tried several different types of PowerShell options with minimal success. As stated, I have tried several different types of PowerShell options with minimal success. The invoke-item command presents the Python prompt and in a separate window the actual Python script displaying the options but the resultant CSV file is not created.
– user714888
Dec 20 '18 at 4:24
I have tried other PowerShell commands (invoke-command, start-process) with varying success in executing the files but none worked in varying combinations of creating the CSV output file. In actuality, the XML is not being printed in the newer test scripts, basically, the XML file(s) is not being queued to be parsed. I am studying, learning, reading, and viewing training materials now to learn PowerShell but requesting assistance now so I can put it to use immediately for work. I initially was not going to post the Python script either but did in case someone did, however, want to see it.
– user714888
Dec 20 '18 at 4:24
I have tried other PowerShell commands (invoke-command, start-process) with varying success in executing the files but none worked in varying combinations of creating the CSV output file. In actuality, the XML is not being printed in the newer test scripts, basically, the XML file(s) is not being queued to be parsed. I am studying, learning, reading, and viewing training materials now to learn PowerShell but requesting assistance now so I can put it to use immediately for work. I initially was not going to post the Python script either but did in case someone did, however, want to see it.
– user714888
Dec 20 '18 at 4:24
So instead of using the batch script to effectively provide the GUI for processing the Python files, I want the GUI use PowerShell. Specific questions: 1. What are the proper commands to use to create the PowerShell that performs the same functions as the batch script? 2. How do I get the XML to print in the GUI so that it is seen as the file to be parsed? 3. Do you have any training materials you recommend? Thank you
– user714888
Dec 20 '18 at 4:24
So instead of using the batch script to effectively provide the GUI for processing the Python files, I want the GUI use PowerShell. Specific questions: 1. What are the proper commands to use to create the PowerShell that performs the same functions as the batch script? 2. How do I get the XML to print in the GUI so that it is seen as the file to be parsed? 3. Do you have any training materials you recommend? Thank you
– user714888
Dec 20 '18 at 4:24
add a comment |
1 Answer
1
active
oldest
votes
The best way I can think of to get you on the right track is to break down the operation of the batch file in to it's relevant components. This is a very straight forward batch file that executes the python environment and tells it to run a specific script. From looking at the python script it appears the batch file is meant to be run from a folder that contains a python script and one or more .xml files.
echo off
Echo off simply tells Windows to not output each command in the batch file to the screen. It's a cleaner way to run batch files and is present in almost all batch files. This is not necessary with Powershell.
set winpy=C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64
This command creates a variable named winpy
which is equal to the path where your python executable resides.
if EXIST %winpy% (
This is the beginning of an IF statement that first checks if the path you specified for winpy
actually exists. If it does, then it runs the code between the two parenthesis.
"%winpy%python.exe" "%~dp0HBSS_XML_Parser_Driver_Action.py" )
This is the line that does the actual work. It starts the python.exe
executable with a command line parameter of your current path and the name of the script.
%~dp0
expands to the drive and path that you ran the batch file in.
Therefore, if you name this file mybatch.bat
and place it in c:test
then these are examples of the command it is running:
Example 1:
If you have a command prompt window open and you are currently in the c:usersyournamedownloads
folder and you type in c:testmybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:usersyournamedownloadsHBSS_XML_Parser_Driver_Action.py"
Example 2:
If you have a command prompt window open and you are currently in the c:test
folder and you type in mybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:testHBSS_XML_Parser_Driver_Action.py"
An equivalent Powershell v3 or newer script would look like this:
#Create the $winpy variable with the path to python.exe
$winpy = "C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64"
#Test if the $winpy path exists
if (test-path $winpy) {
#Execute python.exe and start the script named 'HBSS_XML_Parser_Driver_Action.py' located in the same directory the powershell script is run from
& "$($winpy)python.exe" "$PSScriptRootHBSS_XML_Parser_Driver_Action.py"
}
Hi Appleoddity,
– user714888
Dec 23 '18 at 2:22
Hi Appleoddity. Thanks for the script. However, for some odd reason, the XML file is still not being printed and parsed similarly to the same issue I would have during my testing. The XML file is in the same location folder as the script but still not being read to produce the output file.
– user714888
Dec 23 '18 at 2:30
Using your Example: Example 1 does not print the XML file. Example 2 does work by printing the XML file.
– user714888
Dec 23 '18 at 2:54
Let me clear that up, I am using the batch examples and not the PowerShell ones.
– user714888
Dec 23 '18 at 18:26
Shoot Appleoddity, I owe you a ginormous apology and a beer!! It works perfectly.
– user714888
Jan 7 at 7:52
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1385139%2fconvert-batch-to-powershell-calling-python-scripts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The best way I can think of to get you on the right track is to break down the operation of the batch file in to it's relevant components. This is a very straight forward batch file that executes the python environment and tells it to run a specific script. From looking at the python script it appears the batch file is meant to be run from a folder that contains a python script and one or more .xml files.
echo off
Echo off simply tells Windows to not output each command in the batch file to the screen. It's a cleaner way to run batch files and is present in almost all batch files. This is not necessary with Powershell.
set winpy=C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64
This command creates a variable named winpy
which is equal to the path where your python executable resides.
if EXIST %winpy% (
This is the beginning of an IF statement that first checks if the path you specified for winpy
actually exists. If it does, then it runs the code between the two parenthesis.
"%winpy%python.exe" "%~dp0HBSS_XML_Parser_Driver_Action.py" )
This is the line that does the actual work. It starts the python.exe
executable with a command line parameter of your current path and the name of the script.
%~dp0
expands to the drive and path that you ran the batch file in.
Therefore, if you name this file mybatch.bat
and place it in c:test
then these are examples of the command it is running:
Example 1:
If you have a command prompt window open and you are currently in the c:usersyournamedownloads
folder and you type in c:testmybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:usersyournamedownloadsHBSS_XML_Parser_Driver_Action.py"
Example 2:
If you have a command prompt window open and you are currently in the c:test
folder and you type in mybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:testHBSS_XML_Parser_Driver_Action.py"
An equivalent Powershell v3 or newer script would look like this:
#Create the $winpy variable with the path to python.exe
$winpy = "C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64"
#Test if the $winpy path exists
if (test-path $winpy) {
#Execute python.exe and start the script named 'HBSS_XML_Parser_Driver_Action.py' located in the same directory the powershell script is run from
& "$($winpy)python.exe" "$PSScriptRootHBSS_XML_Parser_Driver_Action.py"
}
Hi Appleoddity,
– user714888
Dec 23 '18 at 2:22
Hi Appleoddity. Thanks for the script. However, for some odd reason, the XML file is still not being printed and parsed similarly to the same issue I would have during my testing. The XML file is in the same location folder as the script but still not being read to produce the output file.
– user714888
Dec 23 '18 at 2:30
Using your Example: Example 1 does not print the XML file. Example 2 does work by printing the XML file.
– user714888
Dec 23 '18 at 2:54
Let me clear that up, I am using the batch examples and not the PowerShell ones.
– user714888
Dec 23 '18 at 18:26
Shoot Appleoddity, I owe you a ginormous apology and a beer!! It works perfectly.
– user714888
Jan 7 at 7:52
add a comment |
The best way I can think of to get you on the right track is to break down the operation of the batch file in to it's relevant components. This is a very straight forward batch file that executes the python environment and tells it to run a specific script. From looking at the python script it appears the batch file is meant to be run from a folder that contains a python script and one or more .xml files.
echo off
Echo off simply tells Windows to not output each command in the batch file to the screen. It's a cleaner way to run batch files and is present in almost all batch files. This is not necessary with Powershell.
set winpy=C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64
This command creates a variable named winpy
which is equal to the path where your python executable resides.
if EXIST %winpy% (
This is the beginning of an IF statement that first checks if the path you specified for winpy
actually exists. If it does, then it runs the code between the two parenthesis.
"%winpy%python.exe" "%~dp0HBSS_XML_Parser_Driver_Action.py" )
This is the line that does the actual work. It starts the python.exe
executable with a command line parameter of your current path and the name of the script.
%~dp0
expands to the drive and path that you ran the batch file in.
Therefore, if you name this file mybatch.bat
and place it in c:test
then these are examples of the command it is running:
Example 1:
If you have a command prompt window open and you are currently in the c:usersyournamedownloads
folder and you type in c:testmybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:usersyournamedownloadsHBSS_XML_Parser_Driver_Action.py"
Example 2:
If you have a command prompt window open and you are currently in the c:test
folder and you type in mybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:testHBSS_XML_Parser_Driver_Action.py"
An equivalent Powershell v3 or newer script would look like this:
#Create the $winpy variable with the path to python.exe
$winpy = "C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64"
#Test if the $winpy path exists
if (test-path $winpy) {
#Execute python.exe and start the script named 'HBSS_XML_Parser_Driver_Action.py' located in the same directory the powershell script is run from
& "$($winpy)python.exe" "$PSScriptRootHBSS_XML_Parser_Driver_Action.py"
}
Hi Appleoddity,
– user714888
Dec 23 '18 at 2:22
Hi Appleoddity. Thanks for the script. However, for some odd reason, the XML file is still not being printed and parsed similarly to the same issue I would have during my testing. The XML file is in the same location folder as the script but still not being read to produce the output file.
– user714888
Dec 23 '18 at 2:30
Using your Example: Example 1 does not print the XML file. Example 2 does work by printing the XML file.
– user714888
Dec 23 '18 at 2:54
Let me clear that up, I am using the batch examples and not the PowerShell ones.
– user714888
Dec 23 '18 at 18:26
Shoot Appleoddity, I owe you a ginormous apology and a beer!! It works perfectly.
– user714888
Jan 7 at 7:52
add a comment |
The best way I can think of to get you on the right track is to break down the operation of the batch file in to it's relevant components. This is a very straight forward batch file that executes the python environment and tells it to run a specific script. From looking at the python script it appears the batch file is meant to be run from a folder that contains a python script and one or more .xml files.
echo off
Echo off simply tells Windows to not output each command in the batch file to the screen. It's a cleaner way to run batch files and is present in almost all batch files. This is not necessary with Powershell.
set winpy=C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64
This command creates a variable named winpy
which is equal to the path where your python executable resides.
if EXIST %winpy% (
This is the beginning of an IF statement that first checks if the path you specified for winpy
actually exists. If it does, then it runs the code between the two parenthesis.
"%winpy%python.exe" "%~dp0HBSS_XML_Parser_Driver_Action.py" )
This is the line that does the actual work. It starts the python.exe
executable with a command line parameter of your current path and the name of the script.
%~dp0
expands to the drive and path that you ran the batch file in.
Therefore, if you name this file mybatch.bat
and place it in c:test
then these are examples of the command it is running:
Example 1:
If you have a command prompt window open and you are currently in the c:usersyournamedownloads
folder and you type in c:testmybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:usersyournamedownloadsHBSS_XML_Parser_Driver_Action.py"
Example 2:
If you have a command prompt window open and you are currently in the c:test
folder and you type in mybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:testHBSS_XML_Parser_Driver_Action.py"
An equivalent Powershell v3 or newer script would look like this:
#Create the $winpy variable with the path to python.exe
$winpy = "C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64"
#Test if the $winpy path exists
if (test-path $winpy) {
#Execute python.exe and start the script named 'HBSS_XML_Parser_Driver_Action.py' located in the same directory the powershell script is run from
& "$($winpy)python.exe" "$PSScriptRootHBSS_XML_Parser_Driver_Action.py"
}
The best way I can think of to get you on the right track is to break down the operation of the batch file in to it's relevant components. This is a very straight forward batch file that executes the python environment and tells it to run a specific script. From looking at the python script it appears the batch file is meant to be run from a folder that contains a python script and one or more .xml files.
echo off
Echo off simply tells Windows to not output each command in the batch file to the screen. It's a cleaner way to run batch files and is present in almost all batch files. This is not necessary with Powershell.
set winpy=C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64
This command creates a variable named winpy
which is equal to the path where your python executable resides.
if EXIST %winpy% (
This is the beginning of an IF statement that first checks if the path you specified for winpy
actually exists. If it does, then it runs the code between the two parenthesis.
"%winpy%python.exe" "%~dp0HBSS_XML_Parser_Driver_Action.py" )
This is the line that does the actual work. It starts the python.exe
executable with a command line parameter of your current path and the name of the script.
%~dp0
expands to the drive and path that you ran the batch file in.
Therefore, if you name this file mybatch.bat
and place it in c:test
then these are examples of the command it is running:
Example 1:
If you have a command prompt window open and you are currently in the c:usersyournamedownloads
folder and you type in c:testmybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:usersyournamedownloadsHBSS_XML_Parser_Driver_Action.py"
Example 2:
If you have a command prompt window open and you are currently in the c:test
folder and you type in mybatch.bat
to execute this batch file, the batch file will run the following command:
"C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64python.exe" "c:testHBSS_XML_Parser_Driver_Action.py"
An equivalent Powershell v3 or newer script would look like this:
#Create the $winpy variable with the path to python.exe
$winpy = "C:UsersAIODUDE.AIODUDE-PCDownloadsWPy-3710python-3.7.1.amd64"
#Test if the $winpy path exists
if (test-path $winpy) {
#Execute python.exe and start the script named 'HBSS_XML_Parser_Driver_Action.py' located in the same directory the powershell script is run from
& "$($winpy)python.exe" "$PSScriptRootHBSS_XML_Parser_Driver_Action.py"
}
answered Dec 20 '18 at 5:26
AppleoddityAppleoddity
7,21521124
7,21521124
Hi Appleoddity,
– user714888
Dec 23 '18 at 2:22
Hi Appleoddity. Thanks for the script. However, for some odd reason, the XML file is still not being printed and parsed similarly to the same issue I would have during my testing. The XML file is in the same location folder as the script but still not being read to produce the output file.
– user714888
Dec 23 '18 at 2:30
Using your Example: Example 1 does not print the XML file. Example 2 does work by printing the XML file.
– user714888
Dec 23 '18 at 2:54
Let me clear that up, I am using the batch examples and not the PowerShell ones.
– user714888
Dec 23 '18 at 18:26
Shoot Appleoddity, I owe you a ginormous apology and a beer!! It works perfectly.
– user714888
Jan 7 at 7:52
add a comment |
Hi Appleoddity,
– user714888
Dec 23 '18 at 2:22
Hi Appleoddity. Thanks for the script. However, for some odd reason, the XML file is still not being printed and parsed similarly to the same issue I would have during my testing. The XML file is in the same location folder as the script but still not being read to produce the output file.
– user714888
Dec 23 '18 at 2:30
Using your Example: Example 1 does not print the XML file. Example 2 does work by printing the XML file.
– user714888
Dec 23 '18 at 2:54
Let me clear that up, I am using the batch examples and not the PowerShell ones.
– user714888
Dec 23 '18 at 18:26
Shoot Appleoddity, I owe you a ginormous apology and a beer!! It works perfectly.
– user714888
Jan 7 at 7:52
Hi Appleoddity,
– user714888
Dec 23 '18 at 2:22
Hi Appleoddity,
– user714888
Dec 23 '18 at 2:22
Hi Appleoddity. Thanks for the script. However, for some odd reason, the XML file is still not being printed and parsed similarly to the same issue I would have during my testing. The XML file is in the same location folder as the script but still not being read to produce the output file.
– user714888
Dec 23 '18 at 2:30
Hi Appleoddity. Thanks for the script. However, for some odd reason, the XML file is still not being printed and parsed similarly to the same issue I would have during my testing. The XML file is in the same location folder as the script but still not being read to produce the output file.
– user714888
Dec 23 '18 at 2:30
Using your Example: Example 1 does not print the XML file. Example 2 does work by printing the XML file.
– user714888
Dec 23 '18 at 2:54
Using your Example: Example 1 does not print the XML file. Example 2 does work by printing the XML file.
– user714888
Dec 23 '18 at 2:54
Let me clear that up, I am using the batch examples and not the PowerShell ones.
– user714888
Dec 23 '18 at 18:26
Let me clear that up, I am using the batch examples and not the PowerShell ones.
– user714888
Dec 23 '18 at 18:26
Shoot Appleoddity, I owe you a ginormous apology and a beer!! It works perfectly.
– user714888
Jan 7 at 7:52
Shoot Appleoddity, I owe you a ginormous apology and a beer!! It works perfectly.
– user714888
Jan 7 at 7:52
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1385139%2fconvert-batch-to-powershell-calling-python-scripts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
“I am not a programmer or scripter...” then you must become one if you want to do programming and scripting. This seems like a good place to start. That is a really basic batch file. First, break it down and research until you know exactly what it is doing. Next, research until you figure out how to replicate that behavior in powershell. Someone may come along and simply build a script for you, but research and self-learning is usually encouraged. This is not an insurmountable task. The actual python script is not relevant here.
– Appleoddity
Dec 17 '18 at 5:48
Specific questions about what exactly the batch file is doing, demonstrating your own research and understanding of it, would be far more effective here. Followed up by more questions about doing specific things in Powershell. It’s the whole “teach a man to fish” thing. :)
– Appleoddity
Dec 17 '18 at 5:52
The batch script calls the Python executable which executes the Python script that displays the options to parse an XML file into a CSV file. The variable statements are optional and included only for varying Python installations. As stated, I have tried several different types of PowerShell options with minimal success. As stated, I have tried several different types of PowerShell options with minimal success. The invoke-item command presents the Python prompt and in a separate window the actual Python script displaying the options but the resultant CSV file is not created.
– user714888
Dec 20 '18 at 4:24
I have tried other PowerShell commands (invoke-command, start-process) with varying success in executing the files but none worked in varying combinations of creating the CSV output file. In actuality, the XML is not being printed in the newer test scripts, basically, the XML file(s) is not being queued to be parsed. I am studying, learning, reading, and viewing training materials now to learn PowerShell but requesting assistance now so I can put it to use immediately for work. I initially was not going to post the Python script either but did in case someone did, however, want to see it.
– user714888
Dec 20 '18 at 4:24
So instead of using the batch script to effectively provide the GUI for processing the Python files, I want the GUI use PowerShell. Specific questions: 1. What are the proper commands to use to create the PowerShell that performs the same functions as the batch script? 2. How do I get the XML to print in the GUI so that it is seen as the file to be parsed? 3. Do you have any training materials you recommend? Thank you
– user714888
Dec 20 '18 at 4:24