1

Hi i am new to power shell and i cant seem to get this script to run it is to remote execute a command using the command prompt on a computer in a workgroup here is the error i get the script is below i am running the script on a win 7 machine the machine i want to remote execute on is windows xp sp3 the fire wall is off and the com settings are set for default for authentication settings and identify for impersonate and help would be great

here is the Error Invoke-WmiMethod : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) At C:\Users\Kevin\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:57 char:40 + $newproc = Invoke-WmiMethod <<<< -class Win32_process -name Create ` + CategoryInfo : NotSpecified: (:) [Invoke-WmiMethod], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.InvokeWmiMethod

$command = Read-Host " Enter command to run" 
    $user = "\Administrator"
    $Domainname = $HostName +$user
    $login = Get-Credential $domainname

        [string]$cmd = "CMD.EXE /C " +$command 
                        } 
  process { 
        $newproc = Invoke-WmiMethod -class Win32_process -name Create `
            -ArgumentList ($cmd) -EnableAllPrivileges -ComputerName $HostName -authentication Packetprivacy -Impersonation 3 -Credential $login 
        if ($newproc.ReturnValue -eq 0 ) 
                { Write-host -foregroundcolor Green "Command $($command) Ran Sucessfully on $($HostName)"} 

1 Answer 1

1

I think this is because Get-Credential won't pass the password to the -credential of Invoke-WmiMethod. I do this exact thing by creating the credential password using "convertto-securestring"

I know it's not as secure as you have to put the password in as plain text, but if you're the only one using the script to do maintenance or such..it's no biggie.

Try this:

$command = Read-Host " Enter command to run" 
$pass = ConvertTo-SecureString "yourpassword" -Force -AsPlainText
$Domainname = 'Domain'
$user = '\administrator'
$login = $Domainname + $user
$cred = (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $login, $pass)

        [string]$cmd = "CMD.EXE /C " +$command 
                        } 
  process { 
        $newproc = Invoke-WmiMethod -class Win32_process -name Create `
            -ArgumentList ($cmd) -EnableAllPrivileges -ComputerName $HostName -authentication Packetprivacy -Impersonation 3 -Credential $cred
        if ($newproc.ReturnValue -eq 0 ) 
                { Write-host -foregroundcolor Green "Command $($command) Ran Sucessfully 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.