【问题标题】:Using powershell variables in my python script?在我的 python 脚本中使用 powershell 变量?
【发布时间】:2021-01-30 07:10:32
【问题描述】:

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

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

谢谢!

【问题讨论】:

  • [1] 来回共享数据的最简单方法是使用 JSON 或 XML。 [2] 这是一个用于 PoSh 的 selenium 模块……它可能会让你绕过使用 python。
  • 您还可以查看 Adam Driscoll 的 snek 模块。它允许您以某种方式同时使用 python 和 PowerShell。

标签: python powershell automation scripting


【解决方案1】:

您可以通过只传递一次而不是保存密码明文来解决它。不过,如果您启用 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

【讨论】:

  • 太棒了 :) 很高兴听到。
猜你喜欢
  • 2014-08-09
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 2020-05-18
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
相关资源
最近更新 更多