Python和Powershell传递参数

2024-09-30 00:41:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力编写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_参数,这两个函数都可以正常工作。 我甚至想过删除空格,但我们在广告中有很多用户的姓氏分为两部分,所以我仍然需要以某种方式解决它

我尝试用不同的方法调用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新手,所以可能有一个简单的技巧我不知道。 不管怎样,我会非常感激你给我的每一个提示


Tags: 函数idparameterparamdomainemployeemanagermandatory
1条回答
网友
1楼 · 发布于 2024-09-30 00:41:05

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

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

说明:

In general, if you're attempting to submit command line arguments which contain spaces, you must encapsulate them with quotes, so that it is understood that the spaces are part of the argument, and are not interpreted as the start of a new token/argument.

相关问题 更多 >

    热门问题