简体   繁体   English

使用内联管理员凭据运行 Powershell 脚本来启动和停止 Windows 服务

[英]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.

相关问题 如果 Powershell 未以管理员身份运行,则停止脚本 - Powershell stop script if it not running as admin Powershell 脚本使用 S.no 停止和启动 windows 服务 - Powershell script to Stop and start windows Service using S.no 如何使用PowerShell-Windows 2008和提示输入凭据在远程服务器上启动/停止服务? - How to start/stop a service on a remote server using PowerShell - Windows 2008 & prompt for credentials? Powershell 脚本停止/启动 IIS 在 Windows 10 - Powershell Script to stop/start IIS in Windows 10 在运行Powershell脚本之前提示域管理员凭据 - Prompt domain admin credentials before running Powershell script 运行 Batch/Powershell 脚本时无需输入管理员凭据 - Bypassing the need to enter Admin Credentials when running a Batch/Powershell script Powershell 脚本检查多台远程机器上的服务并在它们未运行时启动它们 - Powershell script to checking services on multiple remote machines and to start them if they are not running 以管理员身份运行时在Windows Powershell中为启动进程提供选项 - Giving options to Start-process in windows powershell when running as Admin Powershell停止服务,清理磁盘,然后启动服务 - Powershell Stop Services, defrg disks and then start services 运行 Azure Powershell 内联脚本 - Running Azure Powershell Inline Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM