从python运行powershell函数不起作用

时间:2017-02-21 18:59:56

标签: python powershell

我试图导入一个powershell脚本以使用python来使用它的函数,但我使用的语法似乎已关闭。

p = subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", '-Command', '&{". .\powerview.ps1"; & Invoke-CustomShareFinder}'])

按照说明操作:How to run a Powershell function through a Python script

错误:

. .\powerview.ps1
& : The term 'Invoke-CustomShareFinder' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:32
+ &{". .\powerview.ps1"; & Invoke-CustomShareFinder}
+                          ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Invoke-CustomShareFinder:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

基本上是这个脚本,但我重命名了它的功能: https://github.com/PowerShellEmpire/PowerTools/blob/master/PowerView/powerview.ps1

有什么想法吗?

编辑:

从powershell手动导入和启动时,该功能正常工作:

PS C:\Users\gcaille\Documents\my_program> . .\powerview.ps1
PS C:\Users\gcaille\Documents\my_program> Invoke-CustomShareFinder
\\server\ADMIN$         - Remote Admin
\\server\C$     - Default share
\\server\IPC$   - Remote IPC

1 个答案:

答案 0 :(得分:0)

使用其他方法解决了我的问题:

p = subprocess.Popen([r'powershell.exe',
                  '. .\powerview.ps1',
                  '; & Invoke-CustomShareFinder'])
p.communicate()