1

嗨,我是 power shell 的新手,我似乎无法让这个脚本运行它是在工作组中的计算机上使用命令提示符远程执行命令这是我得到脚本的错误如下我正在运行脚本一台win 7机器我想远程执行的机器是windows xp sp3防火墙关闭并且com设置设置为默认的身份验证设置和识别模拟和帮助会很棒

这是错误 Invoke-WmiMethod :访问被拒绝。(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))在 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)"} 
4

1 回答 1

1

我认为这是因为 Get-Credential 不会将密码传递给 Invoke-WmiMethod 的 -credential。我通过使用“convertto-securestring”创建凭证密码来做到这一点

我知道它不像您必须以纯文本形式输入密码那样安全,但如果您是唯一使用该脚本进行维护的人,或者这样......这没什么大不了的。

尝试这个:

$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 
于 2012-06-21T16:07:34.970 回答