Solved

Remote powershell login

Posted on 2016-07-26
7
30 Views
Last Modified: 2016-07-26
Hello Team,

Is there any way, we can put the credentials in the script below that is used to connect exchange services.
after running below, it asked for credentials to enter.

i want credentials will not be asked, it required to mentioned in below Command

Please suggest


$cred = Get-Credential
$session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri "http://exchangecasarray.contoso.com/powershell"-Credential $cred -Authentication Kerberos -AllowRedirection
Import-PSSession $session
Set-AdServerSettings -ViewEntireForest $true
0
Comment
Question by:Andy Navi
  • 4
  • 3
7 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
ID: 41728932
Well, there's the obvious way with an obvious password:
$UserName = 'SomeDomain\SomeUser'
$Password = 'TopSecret'
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, (ConvertTo-SecureString $Password -asPlaintext -Force)

$session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri "http://exchangecasarray.contoso.com/powershell" -Credential $Credential -Authentication Kerberos -AllowRedirection
Import-PSSession $session
Set-AdServerSettings -ViewEntireForest $true

Open in new window


Or this, which allows you to save the credentials as an "Alternate Data Stream" (ADS) in the file object.
The password can only be retrieved on the machine where it was saved, and only from the user who saved it, so it's reasonably safe.
Call the script with the argument -SaveCredential to save the credentials.
Note that some Editors (like Notepad++) remove ADS on saving, others do not (like Notepad), so you might have to re-save the password after script changes, depending on the editor.
The ADS will be copied with the script file itself as long as the target is NTFS, and will be lost otherwise.
[CmdletBinding()]
Param(
	[switch]$SaveCredential
)
$Server = "exchangecasarray.contoso.com"

$ScriptItem = Get-Item -Path $MyInvocation.MyCommand.Path
$StreamName = 'MetaData'
If ($SaveCredential) {
	$gcArgs = @{'Message' = "Logon information for $($Server)"}
	$gcArgs['UserName'] = Try {([Management.Automation.PSSerializer]::Deserialize((Get-Content -Path $ScriptItem.FullName -Stream $StreamName -ErrorAction SilentlyContinue))).UserName} Catch {''}
	If ($Credential = Get-Credential @gcArgs) {
		Try {
			$LastWriteTimeUtc = $ScriptItem.LastWriteTimeUtc
			Set-Content -Path $ScriptItem.FullName -Value ([Management.Automation.PSSerializer]::Serialize($Credential)) -Stream $StreamName -ErrorAction Stop
			$ScriptItem.LastWriteTimeUtc = $LastWriteTimeUtc
		} Catch {
			Throw "Could not save credentials: $($_.Exception.Message)"
		}
	} Else {
		"No credentials were entered, logon information was not saved!" | Write-Warning
	}
	Exit
} Else {
	If ($StreamData = (Get-Content -Path $ScriptItem.FullName -Stream $StreamName -ErrorAction SilentlyContinue)) {
		Try {
			$Credential = [Management.Automation.PSSerializer]::Deserialize($StreamData)
		} Catch {
			Throw "You are not authorized to use this script."
		}
	} Else {
		Throw "File is corrupted, password information is not available."
	}
}

$session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri "http://$($Server)/powershell" -Credential $Credential -Authentication Kerberos -AllowRedirection
Import-PSSession $session
Set-AdServerSettings -ViewEntireForest $true

Open in new window

1
 
LVL 4

Author Comment

by:Andy Navi
ID: 41728938
the First command you mentioned worked !!

Can this be added with below command
Get-ExchangeServer | Get-ServerComponentState | ft

and smtp setting,

after joined all three, i want the results will be send on email.

is it possible ?
0
 
LVL 82

Expert Comment

by:oBdA
ID: 41728943
Once you have the $Credential, you can use it with every cmdlet supporting a -Credential argument.
You can send an email with Powershell with the cmdlet Send-MailMessage.
0
Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

 
LVL 4

Author Comment

by:Andy Navi
ID: 41728944
i need to join all three, because i need to set that as .ps1 and need to add in schedule task.. daily i get email

so how can we join all three in single script ?
0
 
LVL 82

Expert Comment

by:oBdA
ID: 41728969
I don't have an Exchange at hand to test, sorry.
And please be aware that your initial question was about how to store credentials in a script, for which I offered two possibilities.
What you're asking now is unrelated to this.
How to Succeed at Experts Exchange as an Asker
http://support.experts-exchange.com/customer/en/portal/articles/756544-how-to-succeed-at-experts-exchange-as-an-asker
* Be specific about your needs. This helps Experts steer you in the right direction. If you have follow-up or related questions, post a new question for each of them. This helps Experts focus on the questions that have not been answered and lets you award points to the Experts for each question they assist with.
1
 
LVL 4

Author Comment

by:Andy Navi
ID: 41728974
sure Thanks so much
0
 
LVL 4

Author Closing Comment

by:Andy Navi
ID: 41728975
Thank you so much
0

Featured Post

Why do Marketing keep bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
In this video we show how to create a User Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Mailb…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now