Which is best method to authenticate the Remote machine

%3CLINGO-SUB%20id%3D%22lingo-sub-1471667%22%20slang%3D%22en-US%22%3EWhich%20is%20best%20method%20to%20authenticate%20the%20Remote%20machine%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1471667%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20of%20now%20I%26nbsp%3B%20tried%20using%20Wsman%20commands%20and%20it%20succeed%20in%20most%20cases.%3C%2FP%3E%3CP%3EHere%20is%20the%20sample%20code%20for%20that%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%20%20%20%20%24Password%20%3D%20ConvertTo-SecureString%20%24password%20-AsPlainText%20-Force%0A%0A%20%20%20%20%24Username%20%3D%20%24username%20%20%20%20%0A%0A%20%20%20%20%24Cred%20%3D%20New-Object%20System.Management.Automation.PSCredential%20(%24Username%2C%20%24Password)%0A%0A%20%20%20%20%24Session%20%3D%20New-PSSession%20-ComputerName%20%24Target.ip%20-Credential%20%24Cred%0A%0A%20%20%20%20if(-not(%24Session))%7B%0A%0A%20%20%20%20%20%20%20%20Remove-PSSession%20%24Session%0A%0A%20%20%20%20%20%20%20%20throw%20%22Access%20Denied.%20Invalid%20credentials.%22%0A%0A%20%20%20%20%7D%0A%0A%20%20%20%20Remove-PSSession%20%24Session%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20anyone%20suggest%20any%20better%20approach%20to%20authenticate%20the%20remote%20machine%20with%20credentials%20Above%20cases%20has%20a%20loop-hole%20that%20it%20will%20freeze%20when%20the%20connection%20is%20lost(no%20error%20comes%20up%20just%20hangs)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1471667%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPoweShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1472936%22%20slang%3D%22en-US%22%3ERe%3A%20Which%20is%20best%20method%20to%20authenticate%20the%20Remote%20machine%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1472936%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Dave11%2C%3C%2FP%3E%3CP%3EI%20don't%20think%3CSPAN%3E%26nbsp%3Bthat%20cross%20posting%20will%20give%20different%20or%20more%20detailed%20result%3A%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpowershell.org%2Fforums%2Ftopic%2Fwhich-is-best-method-to-authenticate-the-remote-machine%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpowershell.org%2Fforums%2Ftopic%2Fwhich-is-best-method-to-authenticate-the-remote-machine%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EMy%20advice%20is%20still%20the%20same%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3ERead%20%3CA%20href%3D%22https%3A%2F%2Fleanpub.com%2Fsecretsofpowershellremoting%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3ESecrets%20of%20PowerShell%20Remoting%3C%2FA%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20that%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

As of now I  tried using Wsman commands and it succeed in most cases.

Here is the sample code for that:

 

    $Password = ConvertTo-SecureString $password -AsPlainText -Force

    $Username = $username    

    $Cred = New-Object System.Management.Automation.PSCredential ($Username, $Password)

    $Session = New-PSSession -ComputerName $Target.ip -Credential $Cred

    if(-not($Session)){

        Remove-PSSession $Session

        throw "Access Denied. Invalid credentials."

    }

    Remove-PSSession $Session

 

Can anyone suggest any better approach to authenticate the remote machine with credentials Above cases has a loop-hole that it will freeze when the connection is lost(no error comes up just hangs)

1 Reply

Hello Dave11,

I don't think that cross posting will give different or more detailed result: https://powershell.org/forums/topic/which-is-best-method-to-authenticate-the-remote-machine/

 

My advice is still the same:

Read Secrets of PowerShell Remoting.

 

Hope that helps.