我正在运行以下但无法解决为什么在致电New-Service时拒绝访问权限:
param(
[string]$serviceName,
[string]$exePath,
[string]$username,
[string]$password
)
"Attempting to install service '$serviceName' - '$exePath'"
$secpassword = convertto-securestring -String $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $secpassword)
$existingService = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
if ($existingService)
{
"'$serviceName' exists already. Stopping."
Stop-Service $serviceName
"Waiting 3 seconds to allow existing service to stop."
Start-Sleep -s 3
$existingService.Delete()
"Waiting 5 seconds to allow service to be un-installed."
Start-Sleep -s 5
}
"Installing the service."
New-Service -BinaryPathName $exePath -Name $serviceName -Credential $cred -DisplayName $serviceName -StartupType Automatic
"Installed the service."
"Starting the service."
Start-Service $serviceName
"Complete."
我已经仔细检查了我传递给脚本的凭据,当然是使用'adminstrator'和'workgroup \ administrator'作为用户名。我实际上是以管理员身份登录到计算机。
答案 0 :(得分:4)
您是否尝试使用提升的Powershell控制台?右键单击Powershell>以管理员身份运行。运行修改系统设置的脚本时,特别是在Windows 2012中,此问题会发生很多。