简体   繁体   English

"在 Python 脚本运行手册(Azure 自动化帐户)中使用子进程库运行 PowerShell 命令时遇到问题"

[英]Trouble running PowerShell command with subprocess library in Python script runbook (Azure automation account)

I'm new to Azure automation, and have a python script that runs fine on the local machine.我是 Azure 自动化的新手,并且有一个可以在本地计算机上正常运行的 python 脚本。 The script runs a PowerShell command to get sign in logs, and does some string parsing on them before writing to a storage account table.该脚本运行 PowerShell 命令以获取登录日志,并在写入存储帐户表之前对其进行一些字符串解析。 I was able to install all dependencies for the script via the "Python Packages" blade in the automation account.我能够通过自动化帐户中的“Python Packages”刀片安装脚本的所有依赖项。 Is there a way to add powershell.exe somewhere so that the subprocess library can be used to run the PowerShell command?有没有办法在某处添加 powershell.exe 以便可以使用子进程库来运行 PowerShell 命令? Alternatively, is there a library you can import to be able to run the PowerShell command or grab sign in logs from Azure AD without directly using the powershell.exe file?或者,是否可以导入一个库以运行 PowerShell 命令或从 Azure AD 获取登录日志,而无需直接使用 powershell.exe 文件?

Also, Microsoft.Powershell.Core is added in the Modules within the automation account.此外,Microsoft.Powershell.Core 已添加到自动化帐户的模块中。 Am I just using the wrong filename and should use something other than powershell.exe?我只是使用了错误的文件名,应该使用 powershell.exe 以外的其他东西吗? Is there a different module you have to install, or am I referencing it wrong?您必须安装不同的模块,还是我引用错误?

Here is the relevant portion of the script:这是脚本的相关部分:

import subprocess
import sys
import json
import azure.core
from azure.core.credentials import AzureNamedKeyCredential 
from azure.data.tables import TableServiceClient, TableClient 

data = subprocess.check_output(["powershell.exe", "Connect-AzureAD -AccountId placeholder@placeholder.com \n Get-AzureADAuditSignInLogs"]).decode(sys.stdout.encoding)

I have tested in my environment我已经在我的环境中测试过

I get the same error as yours if I use subprocess in my python script to run PowerShell command如果我在我的 python 脚本中使用 subprocess 来运行 PowerShell 命令,我会得到与你相同的错误

This is because the subprocess module is not able to find the powershell.exe path这是因为子进程模块无法找到 powershell.exe 路径

For the workaround, you can use either of the options:对于解决方法,您可以使用以下任一选项:

  1. You can use PowerShell script instead of python script您可以使用 PowerShell 脚本而不是 python 脚本<\/li>
  2. You can use MS Graph API in your python code instead of PowerShell commands您可以在 Python 代码中使用 MS Graph API 而不是 PowerShell 命令<\/li><\/ol>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用子进程在命令行中调用python脚本时运行困难 - Trouble running using subprocess to call python script in command line Azure 自动化运行手册 Python - webhook 数据 - Azure automation runbook Python - webhook data Azure自动化:将参数传递给python中的子Runbook - Azure automation : Pass parameters to child runbook in python Python 代码通过 azure 自动化运行手册连接 azure blob 存储 - Python code to connect azure blob storage via azure automation runbook 为什么我的 Azure Runbook 不能通过 Powershell 执行我的 Python 脚本 - Why won't my Azure Runbook execute my Python script via Powershell Python子进程库:从Python运行grep命令 - Python subprocess library: Running grep command from Python 将Powershell脚本作为python子进程运行时的字符编码 - Encoding of characters when running powershell script as python subprocess 在 Azure Runbook 上运行“mysqldump” - Running 'mysqldump' on an Azure Runbook Python - 在子进程中运行 powershell 命令 - Python - Running powershell commands in a subprocess 使用 subprocess 命令调用 R 脚本,但 r 脚本中的 Library() 命令阻止它运行 - Calling R script using subprocess command but Library() command in r script prevents it from running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM