通过CMD执行Powershell脚本

时间:2014-08-18 12:07:35

标签: windows powershell command-line scripting

我有这个名为change.ps1的powershell脚本,这里是脚本的内容:

Param([string]$username, [string]$password)
Import-Module ActiveDirectory
$newpwd = ConvertTo-SecureString -String "$password" -AsPlainText –Force
Set-ADAccountPassword $username -NewPassword $newpwd –Reset

我现在尝试通过cmd.exe运行此脚本,这是我使用的命令(请注意,我传递了上面脚本中使用的两个参数):

powershell.exe C:\inetpub\htdocs\school_panel\scripts\change.ps1 -username "test" -password "Elgoog321"

现在,当我运行此命令时,我可以告诉脚本是在第二行工作时找到并执行的。我知道这是因为我可以看到正在导入的模块。但是,在我收到此错误消息后: http://puu.sh/aXuH3/b8db154625.png

如果有人可以提供一些帮助,我将非常感激。

此致

2 个答案:

答案 0 :(得分:0)

当您通过CMD执行powershell时,您需要对该PowerShell文件具有强大的执行权限。您需要使用 remotesigned 属性添加参数 -executionpolicy 。这是批处理文件的示例:

@echo off
echo Executing powershell...
cd "C:\directory\of\my\powershell"
Powershell.exe -executionpolicy remotesigned -File "drive:\Directory\of\my\powershell\mypowershell.ps1"

然后即使您不是管理员,也可以在CMD窗口内执行您的powershell而不会出现任何问题。

答案 1 :(得分:0)

您的图片似乎表示编码pb。也许您已使用错误的编码粘贴其他文件中的命令。 尝试在notepad ++中打开文件并将其转换为ANSI编码

相关问题