Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

unable to remote to Server with expired computer password - VM issue from reverting to old snapshot

$
0
0

Test lab setting, we have vm snapshots for each version of software, we revert to an old image and need to rejoin domain as the computer password was changed. ok so far, now I want to script that rejoin. 

But I ran into an issue with the server being in that weird disjoined state, I get Access denied trying to invoke-command using a local admin account. I've checked all the remote setting I can think of, trustedhost * check - enable-pssremoting check, set-executionpolicy check.

If the server is in a workgroup or domain - invoke-command works fine, but in this odd state it does not. Anyone have a solution? I'm thinking a startup script to check the event log for id 3210 and the running it locally, but ideally I'd like it in a script I can run remotely.

Client is Windows 7 x64 Powershell 3.0, server is Windows 2008 R2, powershell 2.0

 

$server = "server1"

#read event log - look for trust relationship error

$localusername = "$server\theman"

$localPassword = "mypassword"| ConvertTo-SecureString -asPlainText -Force

$lcCred = New-Object System.Management.Automation.PSCredential ($localusername,$localpassword)

the top line here gives me the error 'access denied' only when the computer password is bad.

$el = Invoke-Command $server -Credential $lcCred -ScriptBlock{Get-EventLog -log System -After (Get-Date).AddMinutes(-3) | where {$_.EventID -eq 3210}}

if($el) {

 

    Invoke-Command $server -Credential $lcCred -ScriptBlock{  

 

    $DomainUsername = "domain\Administrator"

    $DomainPassword = "p&SSworD" | ConvertTo-SecureString -asPlainText -Force

    $DomainCred = New-Object System.Management.Automation.PSCredential ($Domainusername,$Domainpassword)

    Remove-Computer -Credential $DomainCred -Confirm:$false -Force

    sleep 2

    #join domain

    Add-Computer -DomainName $domain -Credential $DomainCred

    $source = "JoinDomainApp"

if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) {

    [System.Diagnostics.EventLog]::CreateEventSource($source, "Application")

}

     Write-EventLog -LogName system -EventId 3211 -Message "Computer rejoined to domain" -entrytype Information -Source $source -Category "None"

    Restart-Computer -Force

    }

}


Viewing all articles
Browse latest Browse all 6937

Trending Articles