首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何以其他用户身份执行PS1文件?

如何以其他用户身份执行PS1文件?
EN

Stack Overflow用户
提问于 2015-09-09 11:14:38
回答 2查看 18.7K关注 0票数 0

我需要能够以不同的用户身份远程运行PowerShell脚本(通过Jenkins)。因为它将作为Jenkins作业执行,所以Get-Credential不是我的选择。下面是我创建的脚本,但它根本不起作用。

$uname='domain\username'
$pwd='password'
$passw=Convertto-SecureString -String $pwd -AsPlainText -force
$mycred=New-object -TypeName System.Management.Automation.PSCredential -ArgumentList $uname, $passw

Invoke-Command -FilePath "C:\test_scripts\fetchquery.ps1" -Authentication default -Credential $mycred -computername localhost
EN

回答 2

Stack Overflow用户

发布于 2015-09-09 17:02:16

创建凭据对象:

$username = 'domain\username'
$Password = 'password' | ConvertTo-SecureString -Force -AsPlainText
$credential = New-Object System.Management.Automation.PsCredential($username, $Password)

在您的代码中,您在localhost上执行它,因此使用保存的凭据启动Powershell会话:

Start-Process powershell -argumentlist '-executionpolicy','bypass','-file',"C:\test_scripts\fetchquery.ps1"' -Credential $credential 

并远程运行脚本(不要使用Localhost)

Invoke-Command -Computername 'Computer' `
-FilePath C:\test_scripts\fetchquery.ps1 -ArgumentList PowerShell `
-Cred $Credential
票数 2
EN

Stack Overflow用户

发布于 2016-08-17 01:17:55

试试这个:

Start-Process powershell -WindowStyle 'Hidden' -ArgumentList '-executionpolicy', 'bypass', '-file', $scriptFullName -Credential $C
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32470348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档