Solved

Remote powershell login

Posted on 2016-07-26
7
53 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:Addy Nadia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 85

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 5

Author Comment

by:Addy Nadia
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 85

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 5

Author Comment

by:Addy Nadia
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 85

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 5

Author Comment

by:Addy Nadia
ID: 41728974
sure Thanks so much
0
 
LVL 5

Author Closing Comment

by:Addy Nadia
ID: 41728975
Thank you so much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Suggested Courses

631 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