Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Access Denied with PowerShell Script

Status
Not open for further replies.

Shift838

IS-IT--Management
Jan 27, 2003
987
US
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:

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.
 
yes i did. what i found out was that the user id I used to authenticate to the server needs to be the same that I am using to run powershell, which I think is strange.

I tried to run powershell as Administrator and still got th error but did a Run As a different user and selected the same user ID i use to authenticate to the server and it worked. The ID used is a standard user on the workstation and a admin on the server.

very odd..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top