python - Python 和 Powershell 传递参数
问题描述
我正在努力编写 Python/PowerShell 脚本。我在 Django 中建立了一个网站,该网站从用户那里获取输入,然后调用 Python 函数,该函数后来调用 PowerShell 脚本。一切正常,直到我必须将带有空格的参数传递给powershell。
这是代码:
Python 函数
def getaduser_powershell(JsonParams):
args = ['-ID_Param', JsonParams['id'],
'-Domain_Param', JsonParams['domain'],
'-AccountType_Param', JsonParams['accountType'],
'-CreateOn_Param', JsonParams['createOn']
]
fetchedUser = json.loads(subprocess.check_output([
'powershell.exe',
('%s\\get-aduser.ps1') % script_path,
*args
]))
return fetchedUser
PowerShell功能:
param(
[Parameter(Mandatory = $true)][String]$ID_Param,
[Parameter(Mandatory = $true)][String]$Domain_Param,
[Parameter(Mandatory = $true)][String]$AccountType_Param,
[Parameter(Mandatory = $true)][String]$CreateOn_Param
)
try {
$employee = Get-ADUser -Identity $ID_Param -Server $Domain_Param -Properties SamAccountName, GivenName, Surname, EmailAddress, Title, Department, Company, C, Manager, EmployeeType
$fetchedEmployee = [PSCustomObject]@{
ID = $employee.SamAccountName
FirstName = $employee.GivenName
LastName = $employee.Surname
EmailAddress = $employee.EmailAddress
Title = $employee.Title
Department = $employee.Department
Company = $employee.Company
Country = $employee.C
EmployeeType = $employee.EmployeeType
Manager = $employee.Manager
ManagerID = $null
ManagerFirstName = $null
ManagerLastName = $null
Domain = $Domain_Param
CreateOn = $CreateOn_Param
AccountType = $AccountType_Param
}
if($null -ne $employee.Manager){
$manager = Get-ADuser -Identity $employee.Manager -Server $Domain_Param -Properties DisplayName
$fetchedEmployee.Manager = $manager.DisplayName
$fetchedEmployee.ManagerID = $manager.SamAccountName
$fetchedEmployee.ManagerFirstName = $manager.GivenName
$fetchedEmployee.ManagerLastName = $manager.Surname
}
return $fetchedEmployee | ConvertTo-Json
}
catch {
return $false | ConvertTo-Json
}
错误信息:
CalledProcessError at /NonPrimaryADAccounts/SecondaryMarket
Command '['powershell.exe', 'PathToScript\\powershell\\get-aduser.ps1', '-ID_Param', '0000000', '-Domain_Param', 'xxxxxx.xxx.xxx', '-AccountType_Param', 'Secondary Market', '-CreateOn_Param', 'xxxxxx.xxx.xxx']' returned non-zero exit status 1.
如果我使用不包含空格的字符串传递 -AccountType_Param,这两个函数都可以正常工作。我什至想过删除空格,但是我们在 AD 中有很多用户的姓氏分为两部分,所以我仍然需要以一种或另一种方式解决它。
我尝试使用不同的方法调用 PowerShell 函数,但它仍然给我相同的错误消息。这是我尝试过的示例:
fetchedUser = json.loads(subprocess.check_output([
'powershell.exe',
('%s\\get-aduser.ps1') % script_path,
(-Domain Param '%s' -AccountType_Param '%s' -AccountType '%s' -CreateOn_Param '%s') % (JsonParams['id'], JsonParams['domain'], JsonParams['accountType'], JsonParams['createOn'])
]))
不幸的是,我是 python 新手,所以也许只是一个我不知道的简单技巧。无论如何,我会感谢每一个提示!:)
解决方案
@PaluM。在评论中发布答案:
"\"{}\"".format(JsonParams['accountType'])
解释:
通常,如果您尝试提交包含空格的命令行参数,则必须用引号将它们封装起来,以便理解空格是参数的一部分,并且不会被解释为新标记的开头/争论。
推荐阅读
- google-cloud-platform - Cloudrun Force Override 标志未按预期工作
- python - Unittest 正在运行整个 python 文件而不是单个函数
- python - Shapely 已安装,但仍然收到 ModuleNotFoundError: No module named 'shapely'
- nginx - datahub 505 HTTP 版本不支持
- dart - 飞镖中的异步
- jupyter-notebook - 如何仅按百分比显示前 10 个热门地点?
- vue.js - Vuex + Vue 从服务器拉取状态一次
- qtwebengine - 使用专有编解码器构建 QTWebEngine 6.2.0
- ibm-cloud - Cloud Object Storage 存储桶(有什么意义/用途) IBM Cloud
- php - Woocommerce 订阅:隐藏“活动”订阅的取消按钮