[英]Running Powershell script using inline admin credentials to start and stop windows services
I have access as an admin on a machine and I have to Start, Stop and Restart some of the window services using the Powershell script.我可以在机器上以管理员身份访问,并且必须使用 Powershell 脚本启动、停止和重新启动某些窗口服务。 We don't want to have UAC prompt while running the script because only one user would have access to that machine.
我们不希望在运行脚本时出现 UAC 提示,因为只有一个用户可以访问该机器。 Also due to some specific requirements, we have to have run that script file by adding the admin credentials inside it.
同样由于某些特定要求,我们必须通过在其中添加管理员凭据来运行该脚本文件。
Along with other solutions I have tried so far, the one close to what I am looking for is as follows连同到目前为止我尝试过的其他解决方案,接近我正在寻找的解决方案如下
$username = "Domain\user"
$password = ConvertTo-SecureString "myPassword" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
Set-Service -Name Spooler -Status Running -PassThru -Credential $psCred
But I am getting following error.但我收到以下错误。
Set-Service : A parameter cannot be found that matches parameter name 'Credential'.
设置服务:找不到与参数名称“凭据”匹配的参数。 At line:6 char:53 + ... t-Service -Name Spooler -Status Running -PassThru -Credential $psCred + ~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Set-Service], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetServiceCommand
在 line:6 char:53 + ... t-Service -Name Spooler -Status Running -PassThru -Credential $psCred + ~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Set-Service ], ParameterBindingException +fullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetServiceCommand
Update 1 (Trying suggestion)更新 1 (尝试建议)
Invoke-Command -credential $psCred -command {Set-Service -Name Spooler -Status Running -PassThru} -computername myComputerName
[myComputerName] Connecting to remote server myComputerName failed with the following error message : The client cannot connect to the destination specified in the request.
[myComputerName] 连接到远程服务器 myComputerName 失败并显示以下错误消息:客户端无法连接到请求中指定的目标。 Verify that the service on the destination is running and is accepting requests.
验证目标上的服务是否正在运行并且正在接受请求。 Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM.
查阅在目标上运行的 WS-Management 服务的日志和文档,最常见的是 IIS 或 WinRM。 If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".
如果目标是 WinRM 服务,则在目标上运行以下命令来分析和配置 WinRM 服务:“winrm quickconfig”。 For more information, see the about_Remote_Troubleshooting Help topic.
有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。 + CategoryInfo : OpenError: (myComputerName:String) [], PSRemotingTransportException + FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken
+ CategoryInfo : OpenError: (myComputerName:String) [], PSRemotingTransportException +fullyQualifiedErrorId :CannotConnect,PSSessionStateBroken
Update 2更新 2
I had to enabled PSRemoting on machine using command Enable-PSRemoting -Force -SkipNetworkProfileCheck
so Invoke-Command
can execute but after repeating the command Invoke-Command -credential $psCred -command {Set-Service -Name Spooler -Status Running -PassThru} -computername localhost
I got following error我必须使用命令
Enable-PSRemoting -Force -SkipNetworkProfileCheck
在机器上Enable-PSRemoting -Force -SkipNetworkProfileCheck
以便Invoke-Command
可以执行但在重复命令Invoke-Command -credential $psCred -command {Set-Service -Name Spooler -Status Running -PassThru} -computername localhost
我收到以下错误
[localhost] Connecting to remote server localhost failed with the following error message : Access is denied.
[localhost] 连接到远程服务器 localhost 失败并显示以下错误消息:访问被拒绝。 For more information, see the about_Remote_Troubleshooting Help topic.
有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。 + CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException + FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
+ CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException +fullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
Running the same set of command in elevated powershell window is working fine.在提升的 powershell 窗口中运行相同的命令集工作正常。
Can you please guide me in the right direction considering I am newbie in the Powershell world.考虑到我是 Powershell 世界的新手,您能否指导我朝着正确的方向发展。
As mentioned in the official documentation of Set-Service如Set-Service的官方文档中所述
Can you try like this?你可以这样试试吗?
$Cred = Get-Credential
$hostname = "$env:computername.$env:userdnsdomain"
Write-Host $hostname
Invoke-Command -ComputerName $hostname -Credential $Cred -ScriptBlock{ Start-Service -Name Spooler}
Also if you don't want to prompt for the credentials then you can store the credentials in the Windows credentials manager and get it from their in your PowerShell script.此外,如果您不想提示输入凭据,则可以将凭据存储在 Windows 凭据管理器中,并从它们的 PowerShell 脚本中获取。 Refer to this answer for using credential manager with PowerShell
请参阅此答案以将凭据管理器与 PowerShell 结合使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.