2016-02-27 129 views
1

我目前有一个读取服务器列表的VBScript,并尝试验证特定用户标识的密码。用户标识在本地服务器上。我正在检查密码未设置为默认值(我想确保它已更改为其他内容)。如何登录到远程服务器?

“服务器列表”可以是IP地址,主机名(如Rocky)或完全限定的DNS名称(如rocky.bigcompany.com)的混合。这些服务器是物理设备和虚拟设备的混合体,可能也可能不在域中。

我写的现有VBScript处理所有这些,并且正常工作。我正在尝试在Powershell中重新编写这个相同的程序,但它不工作。

下面是我在VBScript中的函数做什么,我想:

Function LoginToServer(Computer, username, password) 
    'this function will log into a server 

    On Error Resume next 

    Set locator = CreateObject("WbemScripting.SWbemLocator") 
    Set wmi = locator.ConnectServer(computer, "root\cimv2", username, password) 

    'check the error code and see if we logged in successfully 
    LoginRC = Err.Number 

    If LoginRC <> 0 Then 
     msg = "Could not log into server: " & CStr(computer) & " With ID: " & CStr(username) 
     lfo.lmsg "B", "WARN", msg 
    Else 
     msg = "Server: " & CStr(computer) & " Logged in successfully as: " & CStr(username) 
     lfo.lmsg "B", "INFO", msg 
    End If 

    wmi.Security_.ImpersonationLevel = 3 

    'return the code back to calleer 
    LoginToServer = LoginRC 
End Function 

&hellip;这里就是我试图在PowerShell中要做到:

Param($ComputerName = "LocalHost") 

$ErrorActionPreference = "Stop" 

# Actual Code starts here 
Write-Host "Attempting to ping server: $ComputerName" 
$IPResult = Test-Connection -ComputerName $ComputerName -Quiet 

if ($IPResult -eq "TRUE") { 
    Write-Host "Ping OK - now attempting to log in" 

    try { 
     $ID = "userid" 
     $PSW = "password" 

     $password = ConvertTo-SecureString $PSW -AsPlainText -Force 
     $cred = New-Object System.Management.Automation.PSCredential ($ID, $password) 
     $sesh = New-PSSession -ComputerName $ComputerName -Credential $cred 
    } catch { 
     Write-Host "Error caught" 
     $ErrorMessage = $_.Exception.Message 
     $FailedItem = $_.Exception.ItemName 
    } finally { 
     $Time = Get-Date 
     "$Time Computer: $ComputerName ERROR: $ErrorMessage ITEM: $FailedItem" | 
      Out-File c:\temp\TestCredScript.log -Append 
    } 
} else { 
    Write-Host "Could not ping server" 
} 

如何登录到使用PowerShell中的ID和密码,这些远程计算机?

+1

你能比“不工作”更具体吗? :) –

+0

对不起马蒂亚斯 - 我很热心地把所有的信息都写进了这篇文章,我昨天晚上意识到我没有给出任何有关不工作的细节。当我尝试使用代码登录一个不在域中的服务器,我收到一个错误,说我使用了无效的ID或密码。我知道ID和密码是有效的。我在运行代码后立即登录到服务器,以验证我是否拥有正确的信息。 – GeoCoder

回答

2

你的两个代码示例做了不同的事情。 VBScript代码通过WMI连接,而PowerShell代码尝试建立PowerShell会话。对于后者,您需要启用PowerShell Remoting,这可能不具备。

尽管您可能想要启用PSRemoting,但您也可以使用PowerShell中的WMI。 Get-WmiObject cmdlet允许您提供证书和模拟级别,因此您不需要首先建立与您需要使用VBScript(如果要使用显式证书)相关的连接。

例查询远程计算机上的Win32_Process类:

$computer = '...' 
$username = 'userid' 
$password = 'password' 

$pw = ConvertTo-SecureString $password -AsPlainText -Force 
$cred = New-Object Management.Automation.PSCredential ($username, $pw) 

Get-WmiObject -Computer $computer -Namespace 'root\cimv2' -Class Win32_Process -Impersonation 3 -Credential $cred 

进一步的信息参见here

+0

谢谢!你让我避免过早秃顶!我最初试图做这样的事情,但不能让它工作。我必须看看我保存的一些代码,但我想我试图将密码硬编码到PSCredential中,而不是先使用Convert-To-SecureString。我放弃了并尝试了其他方法,其他方法我最终都感到沮丧并发布了您看到的代码。我已经使用您提供的信息修改了我的脚本,现在它工作的很好!感谢您帮助新手Powershell脚本! – GeoCoder

+0

感谢您提醒我接受答案。我是新来张贴在这个论坛上,并仍然认为这样的事情。 – GeoCoder

相关问题