Solved

Remote powershell login

Posted on 2016-07-26
7
43 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
  • 4
  • 3
7 Comments
 
LVL 84

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 84

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
Independent Software Vendors: 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!

 
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 84

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we show how to create a Shared 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 >> Sha…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

726 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