在我的python脚本中使用powershell变量?

问题描述

我已经在powershell中创建了一个用户创建脚本,并且几乎可以用selenium在python中编写它的网站自动化部分。我的问题在于2的加入。我希望我的Python脚本使用我在powershell中输入的新用户凭据。

因此,希望PS脚本能够完全运行,但是在退出之前,它会启动我的python脚本,并使用信誉信息来建立他的网站配置文件。最近几天我做了很多研究,无法弄清楚。

谢谢!

解决方法

您可以通过只传递一次而不是保存密码明文来解决该问题。但是,如果启用Powershell日志记录,请检查将在这些日志中显示的内容。

$user1 = "test1"
$cred1 = "testpass1"

# you can also concatenate if necessary,adding all users/pws with some separators
$user2 = "test2"
$cred2 = "testpass2" 

$users=$user1+","+$user2 
$creds=$cred1+","+$cred2

PS > py .\path_to\create_web_profiles.py $users $creds  # make sure you use _py_ and not python / python3.

create_web_profiles.py:

import sys

users = sys.argv[1]
passwords = sys.argv[2]

def getusers(users,passwords):

    users=users.split(",")
    passwords=passwords.split(",")

    print('Usernames: ',users,'Passwords: ',passwords)
    for user,passw in zip(users,passwords):
        create_web_user(user,passw)

def create_web_user(user,passw):
    # your web functions come here
    print(user,passw)
    pass

getusers(users,passwords)

输出:

PS > py .\path_to\create_web_profiles.py $users $creds
Usernames:  ['test','test2'] Passwords:  ['tpass','tpass2']
test tpass
test2 tpass2