gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:53:59 24 4
gpt4 key购买 nike

我以管理员身份访问一台机器,我必须使用 Powershell 脚本启动、停止和重新启动一些窗口服务。我们不希望在运行脚本时出现 UAC 提示,因为只有一个用户可以访问该机器。此外,由于某些特定要求,我们必须通过在其中添加管理员凭据来运行该脚本文件。

连同我迄今为止尝试过的其他解决方案,最接近我正在寻找的解决方案如下

$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

但是我遇到了以下错误。

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

更新 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. 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. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic. + CategoryInfo : OpenError: (myComputerName:String) [], PSRemotingTransportException + FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken

更新 2

我必须使用命令 Enable-PSRemoting -Force -SkipNetworkProfileCheck 在机器上启用 PSRemoting,以便 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. For more information, see the about_Remote_Troubleshooting Help topic. + CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException + FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken

在提升的 powershell 窗口中运行同一组命令工作正常。

考虑到我是 Powershell 世界的新手,您能指导我正确的方向吗?

最佳答案

official documentation 中所述设置服务

你能这样试试吗?

$Cred = Get-Credential
$hostname = "$env:computername.$env:userdnsdomain"
Write-Host $hostname
Invoke-Command -ComputerName $hostname -Credential $Cred -ScriptBlock{ Start-Service -Name Spooler}

另外,如果您不想提示输入凭据,那么您可以将凭据存储在 Windows 凭据管理器中,然后从您的 PowerShell 脚本中获取。引用this answer用于将凭据管理器与 PowerShell 一起使用

关于powershell - 使用内联管理员凭据运行 Powershell 脚本以启动和停止 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59728575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com