如何通过 Python 脚本运行 Powershell 函数 [英] How to run a Powershell function through a Python script

查看:43
本文介绍了如何通过 Python 脚本运行 Powershell 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Python 创建一个翻译器类型的程序(如果这很重要,这是 2.7).我想接收用户输入,翻译它,然后将他们的输出打印到屏幕上.那部分并不难,但我也想将其导出为文本文件.为此,我使用 Powershell 和 subprocess 模块.Powershell 脚本非常简短,它所做的只是要求用户将 Python 翻译复制并粘贴到输入中.然后它调用 New-Item 来创建一个文件,并为其提供 value 选项作为 Python 翻译.

I am trying to create a translator-type program in Python (this is 2.7, if that is important). I want to receive user input, translate it, and then print their output to the screen. That part is not difficult, but I also want to export it to a text file. I am using Powershell for that, with the subprocess module. The Powershell script is very short, and all it does is asks for the user to copy and paste the Python translation into an input. It then calls New-Item to create a file and gives it the value option as the Python translation.

Python 代码:

def translator:
    inquiry = raw_input("Leetspeak trans query: ") #Enter query
    inquiry = inquiry.lower() #Change all to lowercase, so that everything gets translated
    newPrint1 = "" #The new string that gets returned to them at the end
    level = raw_input("What type of 1337 do you want? 1 for basic, 2 for intermediate, \
    3 for intermediate-advanced, and 4 for ultimate.")

    if level == "1":
        from b4s1c_l33t import leetkey
    elif level == "2":
        from In73rm3d1473_1337 import leetkey
    elif level == "3": 
        from In7_4DV import leetkey
        from In7_4DV import combokey
    elif level == "4":
        from U17IM473_1337 import leetkey
        from U17IM473_1337 import combokey

    for char in inquiry:
        if char in leetkey:
            newPrint1 += leetkey[char]
        else: 
            newPrint1 += char #Checks to see if the char is in the single-char list, then appends it accordingly

    if int(level) >= 3:
        for item in combokey:
            if item in newPrint1:
                newPrint1 = newPrint1.replace(item, combokey[item])

    print newPrint1 #Print answer

    question = raw_input(r"Do you want to translate some more? Type Y or N  ") #Asks if they want to do more
    question = question.lower() #Changes it to lowercase, for sending through the if loop

    if question == "y" or question == "Y":
        translator() #If answer is yes, program calls the entire function again
    elif question != "y" and question != "n" and question != "Y" and question != "N":
        print "I didn't quite catch that."

    ps = raw_input("Would you like to export your leetness to a file? Type Y or N   ")

    if ps == "Y" or ps == "y": 
        import subprocess
        subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./1337Export.ps1\";", "&export"])

    else: 
       print r"0|<. 600|)|3`/3!"

translator() #calls the function once

Powershell 脚本:

Powershell script:

Function export(){
    $param = Read-Host("Copy-paste the translation from above here!   ")
    New-Item C:\Scripts\1337\1337ness.txt -type file -value $param
}

但我也知道,在我添加 Powershell 之前,该脚本运行良好,所以问题要么出在我对子进程模块的使用中,要么出在 Powershell 脚本本身中.我是使用 Powershell 的中等初学者,因此将不胜感激任何帮助.或者,如果有一种方法可以在 Python 本身中创建新文件并将数据写入其中,那将不胜感激.

But I also know that the script was working perfectly up until I added the Powershell to it, so the problem is either in my usage of the subprocess module or in the Powershell script itself. I am a somewhat-medium-beginner at using Powershell, so any help will be greatly appreciated. Either that, or if there is a way to create the new file and write data to it in Python itself, that would be greatly appreciated.

谢谢,预售

注意:在 Python 脚本中,leetkeycombokey 位于单独的文件中,它们是根据变量 level 的值导入的.

Note: in the Python script, the leetkey and combokey are in separate files that are imported based on the value of the variable level.

更新:我查看了此处的页面,subprocess Python 脚本中的代码是我在该页面中找到的.它没有用,而是抛出一个错误,说 export 函数不存在,这显然是……在 Powershell 中.再次感谢!

UPDATE: I looked at the page here, and the subprocess code in the Python script is what I found in that page. It did not work, but instead threw an error saying that the export function does not exist, which it obviously does... in Powershell. Thanks again!

推荐答案

您的参数构造已关闭.您想在 PowerShell 中运行以下命令行:

Your parameter construction is off. You want to run the following commandline in PowerShell:

. "./1337Export.ps1"; & export

这基本上意味着从当前工作目录中点源(IOW导入)脚本1337Export.ps1,然后调用(&)函数导出".

which basically means "dot-source (IOW import) the script 1337Export.ps1 from the current working directory, then call (&) the function export".

最简洁的方法是将语句放在脚本块中:

The cleanest way to do this is to put the statement in a scriptblock:

&{. "./1337Export.ps1"; & export}

并将该脚本块作为单个参数传递,因此您的 Python 语句应如下所示:

and pass that scriptblock as a single argument, so your Python statement should look like this:

subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", '-Command', '&{. "./1337Export.ps1"; & export}'])

当然,在执行 Python 脚本时,您需要确保 1337Export.ps1 确实存在于当前工作目录中.

Of course you need to make sure that 1337Export.ps1 actually exists in the current working directory when you execute the Python script.

这篇关于如何通过 Python 脚本运行 Powershell 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆