PowerShell - Invoke-WmiMethod : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

0

I have 2 workgroup computers. Powershell remoting is enabled in one of the computer. Other computer can't be accessed directly to enable the powershell remoting. So i am running a script on the Host Machine to enable the remoting on a remote machine remotely. Following is the script i am using:

param(
    [parameter(Mandatory = $true)]
    [string]$RemoteMachineIPaddress,
    [parameter(Mandatory = $true)]
    [string]$SystemIPaddress
)

try{

$DisplayName = "Allow ICMPv4-In"

$secpasswd = ConvertTo-SecureString "Password" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ("Admin02", $secpasswd)

Invoke-WmiMethod -ComputerName $RemoteMachineIPaddress -Namespace root\cimv2 -Class Win32_Process -Name Create -Credential $Cred -Impersonation 3 -EnableAllPrivileges -ArgumentList "powershell Start-Process powershell -Verb runAs -ArgumentList 'Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force'"
Invoke-WmiMethod -ComputerName $RemoteMachineIPaddress -Namespace root\cimv2 -Class Win32_Process -Name Create -Credential $Cred -Impersonation 3 -EnableAllPrivileges -ArgumentList "powershell Start-Process powershell -Verb runAs -ArgumentList 'Install-PackageProvider -Name Nuget -MinimumVersion 2.8.5.201 -Force'"
Invoke-WmiMethod -ComputerName $RemoteMachineIPaddress -Namespace root\cimv2 -Class Win32_Process -Name Create -Credential $Cred -Impersonation 3 -EnableAllPrivileges -ArgumentList "powershell Start-Process powershell -Verb runAs -ArgumentList 'Start-Service WinRM -Force'"
Invoke-WmiMethod -ComputerName $RemoteMachineIPaddress -Namespace root\cimv2 -Class Win32_Process -Name Create -Credential $Cred -Impersonation 3 -EnableAllPrivileges -ArgumentList "powershell Start-Process powershell -Verb runAs -ArgumentList 'Set-Item WSMan:\localhost\Client\TrustedHosts -Value $SystemIPaddress -Force'"
Invoke-WmiMethod -ComputerName $RemoteMachineIPaddress -Namespace root\cimv2 -Class Win32_Process -Name Create -Credential $Cred -Impersonation 3 -EnableAllPrivileges -ArgumentList "powershell Start-Process powershell -Verb runAs -ArgumentList 'Enable-PSRemoting -Force -SkipNetworkProfileCheck'"
Invoke-WmiMethod -ComputerName $RemoteMachineIPaddress -Namespace root\cimv2 -Class Win32_Process -Name Create -Credential $Cred -Impersonation 3 -EnableAllPrivileges -ArgumentList "powershell Start-Process powershell -Verb runAs -ArgumentList 'Restart-Service winrm'"

return $true
}
catch
{
return $false
}

When i run this script from the host machine i get the Access Denied error

Invoke-WmiMethod : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

I am running the above script in Admin Mode and also i have provided the Remote machine Admin Credentials to the Script.

powershell
com
wmi
powershell-remoting
access-denied
asked on Stack Overflow Oct 16, 2019 by Harshith R

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0