I am writing a script to stop a couple of services and change their Start Mode type to Disabled. The issue is that it works fine from a remote computer in the PowerShell Console, but if I program it in a script file and run it from the same remote computer it fails. It only fails on a Windows 2008 server, it works fine on Windows 2003. The error is:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
My code is:
Is there some change I need to make on the Windows 2008 server to get past this.. I find it out it works from a PowerShell Window but not a script file.
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
My code is:
Code:
$services = "browser","spooler"
$server = "hou150w8ahv002"
$cred = Get-Credential
#Disable services and write results
ForEach ($service in $services)
{
$svc = gwmi -ComputerName $server win32_service -Filter "name='$service'"
$return1 = $svc.StopService()
$return2 = $svc.ChangeStartMode("Disabled")
$svc = gwmi -ComputerName $server win32_service -Credential $cred -Filter "name='$service'"
Write-Host "Service $($svc.DisplayName) $($svc.State) on $server."
Write-Host "Service $($svc.DisplayName) $($svc.StartMode) on $server."
}
Is there some change I need to make on the Windows 2008 server to get past this.. I find it out it works from a PowerShell Window but not a script file.