Apr 25 2019 03:43 PM
I am trying to run a script and i wanted to run it silently without asking for credential prompts.
Is there a way to get around using "$cred = Get-Credential" without a prompt
The script i am trying to use is
# Connect to Office 365
#
$username = ""
$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force
#$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList ($user, $password)
Import-Module MSOnline
$cred = Get-Credential
Connect-MSolService -credential $cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber -DisableNameChecking
Apr 25 2019 04:16 PM
Apr 25 2019 05:55 PM
using windows credential manager, create your credential and give it a name
Then, in PowerShell, Wherever you use
$cred = Get-Credential
which prompts you, replace that with
$cred =$(Get-StoredCredential -Target thenameyoustoredyourcredentialunder)
You’ll need to install-module CredentialManager
Apr 26 2019 01:12 AM - edited Apr 26 2019 01:14 AM
Solution
I have corrected your script (removed the unwanted call of Get-Credential and fixed the variable name creds)
$username = "admin@domain.com"
$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
Import-Module MSOnline
Connect-MSolService -Credential $psCred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell-liveid/ -Credential $psCred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber -DisableNameChecking
Apr 26 2019 12:25 PM
Apr 26 2019 12:42 PM - edited Apr 26 2019 12:47 PM
@TejCGS Try it without the –AsCredentialObject ? Does that make any difference? Also, I surrounded the expression with $() to force PowerShell to interpret it.
Apr 26 2019 09:13 PM
I have corrected your script :
$psCred = Get-StoredCredential -Target "CGSPS1"
Connect-MSolService -Credential $psCred
Sep 23 2019 07:55 PM
Jun 02 2020 12:23 PM
@Kevin MorganDid it ask you for login again at Connect-MSolService -Credential $psCred, for me its asking to login again, how do i avoid prompt as i am already passing cred
Jun 17 2020 01:00 PM
@Kevin Morgan Thanks so much for your answer. Saved me a lot of time wading through useless information!
Sep 01 2021 01:52 PM
With all due respect. Its not a question on best practice. I myself needed this for development stuff, a zone where I disable all security so that it is never in the way.
Apr 26 2019 01:12 AM - edited Apr 26 2019 01:14 AM
Solution
I have corrected your script (removed the unwanted call of Get-Credential and fixed the variable name creds)
$username = "admin@domain.com"
$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
Import-Module MSOnline
Connect-MSolService -Credential $psCred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell-liveid/ -Credential $psCred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber -DisableNameChecking