我目前有一個讀取服務器列表的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和密碼,這些遠程計算機?
你能比「不工作」更具體嗎? :) –
對不起馬蒂亞斯 - 我很熱心地把所有的信息都寫進了這篇文章,我昨天晚上意識到我沒有給出任何有關不工作的細節。當我嘗試使用代碼登錄一個不在域中的服務器,我收到一個錯誤,說我使用了無效的ID或密碼。我知道ID和密碼是有效的。我在運行代碼後立即登錄到服務器,以驗證我是否擁有正確的信息。 – GeoCoder