用Python运行带参数的PS命令(引号和斜线)

2024-10-04 07:34:41 发布

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

我想用PowerShell从Python脚本更改当前密码。为此,我需要从下面的代码运行PowerShell命令。最好的办法是什么?在

import datetime
#generated password is always changing. There might be different quotations and slashes so it's tricky to put it into PowerShell command bellow
generated_password=r'/#\n\9{S;-l2H~'

#password to set is changing depending on date. Let's say today it has become 'xxx180220'
password_to_set="xxx"+datetime.datetime.now().strftime("%y%m%d")

#I'd like to run this PS command to change my generated password to value stored in password_to_set variable. How do I do it?
PowerShell.exe -ExecutionPolicy ByPass ([adsi]'WinNT://valueaddco/wadmin,user').ChangePassword('/#\n\9{S;-l2H~','xxx180220')

Tags: to脚本密码datetimeisitpassworddo
1条回答
网友
1楼 · 发布于 2024-10-04 07:34:41

我创建了新的1.ps文件:

echo "changing password from $($args[0]) to $($args[1])"
([adsi]'WinNT://valueaddco/wadmingolyaa1,user').ChangePassword($($args[0]), $($args[1]))

它可以用Python代码启动:

^{pr2}$

相关问题 更多 >