Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量从Python GUI传递到PowerShell_Python_Powershell - Fatal编程技术网

将变量从Python GUI传递到PowerShell

将变量从Python GUI传递到PowerShell,python,powershell,Python,Powershell,因此,我正在创建一个应用程序,它可以将打印机与后台运行PowerShell脚本的Python GUI连接起来。我想知道是否有一种方法可以将从Python小部件输入的变量传递到由Python调用的PowerShell脚本中。这个变量将是我可以在Python中指定的打印机名称,这样我就不必为每个打印机创建单独的脚本 我的Python代码调用PS脚本: def connect(): if self.printerOpts.get() == 'Chosen

因此,我正在创建一个应用程序,它可以将打印机与后台运行PowerShell脚本的Python GUI连接起来。我想知道是否有一种方法可以将从Python小部件输入的变量传递到由Python调用的PowerShell脚本中。这个变量将是我可以在Python中指定的打印机名称,这样我就不必为每个打印机创建单独的脚本

我的Python代码调用PS脚本:

def connect():               
        if self.printerOpts.get() == 'Chosen Printer':
            subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",'-ExecutionPolicy','Unrestricted', '.\'./ScriptName\';'])
param([string]$path)

Add-Printer -ConnectionName \\server\$path
将打印机连接到计算机的PS脚本:

Add-Printer -ConnectionName \\server\printer -AsJob

基本上,我想知道是否可以将一个变量从Python传递到PS脚本的“打印机”部分,这样我就不必为要添加的每个打印机创建不同的脚本。

更好的方法是完全在PowerShell中或完全在Python中

你所追求的是可行的。通过确保PowerShell脚本需要该变量,您可以使用与传递
-ExecutionPolicy Unrestricted
相同的方式传递它

我的Python是不存在的,所以如果那部分不起作用,请容忍

Python

myPrinter # string variable in Python with printer name
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",'-ExecutionPolicy','Unrestricted', '.\'./ScriptName\';','-printer',myPrinter])
PowerShell

param(
    $printer
)

Add-Printer -ConnectionName \\server\$printer -AsJob

我的工作方式是首先指定在PS脚本中以字符串形式传递变量:

def connect():               
        if self.printerOpts.get() == 'Chosen Printer':
            subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",'-ExecutionPolicy','Unrestricted', '.\'./ScriptName\';'])
param([string]$path)

Add-Printer -ConnectionName \\server\$path
我的PS脚本不需要此变量。在我的Python脚本中,我必须首先定义我的变量,该变量将path命名为字符串,然后将“path”输入到子流程函数的末尾

        path = "c"
        subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",'-ExecutionPolicy','Unrestricted', 'Script.ps1', path])

谢谢gmss0ulman。我能够应用更改,但PowerShell给出了一个错误“指定的服务器不存在”,并且打印机不再添加。@JDawg-Hmm。。传递给PowerShell的字符串是什么样子的?也许它应该是
$printer
而不是
\\server\$printer
@gmss0ulman我使用了你的PS脚本并对Python脚本做了一些其他更改,它成功了。如果你好奇的话,我会把我的答案贴在这个问题上。非常感谢你!