首页 > 解决方案 > 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 新手,所以也许只是一个我不知道的简单技巧。无论如何,我会感谢每一个提示!:)

标签: pythonpowershell

解决方案


@PaluM。在评论中发布答案:

"\"{}\"".format(JsonParams['accountType'])

解释:

通常,如果您尝试提交包含空格的命令行参数,则必须用引号将它们封装起来,以便理解空格是参数的一部分,并且不会被解释为新标记的开头/争论。


推荐阅读