locked
Remote script execution - Avoid log-in prompt RRS feed

  • Question

  • I am trying to run commands(Ex:delete a file) with alternative credentials(explicit user name and password which is hard coded in the script) in windows 2012 Azure virtual machine. After searching google, I used following code which does the job..

    $securePassword = ConvertTo-SecureString "MyPassword" -AsPlainText -force
    $credential = New-Object System.Management.Automation.PsCredential("10.0.0.4\adminUserName",$securePassword)
    $session = New-PSSession -computername 10.0.0.4 -credential $cred

    $command = {Remove-Item C:\inetpub\wwwroot\deleteme.htm -Force} 
    $res = Invoke-Command -session $session -scriptblock $command

    When i run above code it is again prompting log-in prompt to enter user name and password which i want to avoid. I don't know what is wrong here and how to avoid this. Please help.

    thanks

    Tuesday, October 25, 2016 11:32 AM

Answers

  • Hi Satyah,

    you have a typo in there: You store the credential object in $credential, but call new-pssession with $cred.

    # Typo
    $session = New-PSSession -computername 10.0.0.4 -credential $cred
    
    # No Typo
    $session = New-PSSession -computername 10.0.0.4 -credential $credential

    Cheers,
    Fred

    PS: You can use Encrypt-Credential (from the gallery) to protect your credentials somewhat better.


    There's no place like 127.0.0.1


    • Edited by FWN Tuesday, October 25, 2016 12:04 PM
    • Proposed as answer by Richard MuellerMVP Tuesday, October 25, 2016 3:35 PM
    • Marked as answer by Richard MuellerMVP Tuesday, November 1, 2016 3:51 PM
    Tuesday, October 25, 2016 12:03 PM