如何使用不同凭据运行powershell脚本

如何使用不同凭据运行powershell脚本,powershell,Powershell,我需要使用不同的用户名和密码运行powershell脚本(在远程服务器中) 我需要在单个命令行中传递凭据。有人能告诉我命令吗 scriptname: configure.ps1 username: xyz password: pwd123 这不是完全相同的事情,但是您不必拥有新代码:您可以将脚本作为计划任务运行,任务作为您想要使用的用户帐户运行 打开任务计划程序,单击“创建任务”,并在“常规”选项卡上添加任务的名称和有意义的描述 设置安全选项 在“触发器”选项卡上,单击“新建”并根据需要设置计

我需要使用不同的用户名和密码运行powershell脚本(在远程服务器中)

我需要在单个命令行中传递凭据。有人能告诉我命令吗

scriptname: configure.ps1
username: xyz
password: pwd123

这不是完全相同的事情,但是您不必拥有新代码:您可以将脚本作为计划任务运行,任务作为您想要使用的用户帐户运行

打开任务计划程序,单击“创建任务”,并在“常规”选项卡上添加任务的名称和有意义的描述

设置安全选项

在“触发器”选项卡上,单击“新建”并根据需要设置计时:

在“操作”选项卡上,单击“新建”,并在“程序/脚本”下使用以下语法运行PowerShell并调用脚本:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command "&{<path2script>.ps1; exit $LASTEXITCODE}"
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe-命令“&{.ps1;退出$LASTEXITCODE}”

在有关参数的提示下单击“是”。

我实际上正在尝试从Ansible control Linux服务器连接windows服务器。我遵循了你在我的剧本中建议的所有步骤

案例1:尝试使用我自己的用户名和密码,我能够在windows计算机上运行powershell脚本

案例2:尝试使用技术帐户时,我遇到了错误。错误详细信息粘贴在下面

fatal: [linux_server]: FAILED! => {"changed": true, "cmd": "$password = \"password" | ConvertTo-SecureString -asPlainText -Force\n $username = \"admin\\username\"\n $credential = New-Object System.Management.Automation.PSCredential($username,$password)\n cd D:\\Risk_AppServer\\test\\\n Invoke-Command -ComputerName windows_server -FilePath .\\sample_ps1.ps1 -credential $credential", "delta": "0:00:00.843081", "end": "2018-09-19 06:31:56.239923", "msg": "non-zero return code", "rc": 1, "start": "2018-09-19 06:31:55.396841", "stderr": "[windows_server] Connecting to remote server windows_server failed with the following error message : Acce\r\nss is denied. For more information, see the about_Remote_Troubleshooting Help topic.\r\n    + CategoryInfo          : OpenError: (windows_server:String) [], PSRemotingTransportException\r\n    + FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken", "stderr_lines": ["[windows_server] Connecting t**o remote server windows_server failed with the following error message : Acce", "ss is denied. For more information, see the about_Remote_Troubleshooting Help topic.", "    + CategoryInfo          : OpenError: (windows_server:String) [**], PSRemotingTransportException", "    + FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken"], "stdout": "", "stdout_lines": []}
请注意:使用技术帐户用户名和密码,我可以成功登录windows计算机