以pipe理员身份运行脚本,无需以pipe理员身份login

我的一个老伴侣常常说:“任何问题都是愚蠢的,愚蠢的是不要问”

我很新的Windows系统pipe理员(只有3/4个月),最近我开始在PowerShell 2.0的工作。

我正在pipe理一组Windows服务,其中一个将尝试执行“启动服务”命令作为PowerShell脚本的一部分。 此Windows服务作为pipe理员组的一部分在域帐户“ATG \ Boot”下运行。

但是,由于这个原因,启动服务/停止服务总是失败:

停止服务:由于以下错误,无法停止服务“SQL Server Reporting Services(MSSQLSERVER)(ReportServer)”:无法在计算机上打开ReportServer服务。 在行:1个字符:13 +停止服务<<<< 报告 + CategoryInfo:CloseError:(System.ServiceProcess.ServiceController:ServiceController)[停止服务],
ServiceCommandException + FullyQualifiedErrorId:CouldNotStopService,Microsoft.PowerShell.Commands.StopServiceCommand

这个错误被重现,当我从shell执行脚本,但正常运行时,我以“以pipe理员身份运行”选项运行脚本。

我想同情该用户是本地pipe理员组的一部分。

有任何想法吗?

UAC是否启用? 你可能不得不让脚本提升自己。 这可能意味着您的脚本将显示一个UAC提示。

这是“PowerShell uac提示”的第一个search命中。

https://stackoverflow.com/questions/1566969/showing-the-uac-prompt-in-powershell-if-the-action-requires-elevation

这是我用来阻止SQL Server服务的脚本。 通过在-runas开关中使用启动进程,一个单独的PowerShell进程将以pipe理员身份启动。 您将被提示inputUAC。

Start-Process powershell.exe -argumentlist '-noprofile -command get-service -Name "MSSQL`$R2" | ? {$_.Status -eq "Running"} | Stop-Service -Force' -verb runas 

我在这里有关于这个主题的博客文章。