Solved

Remote powershell login

Posted on 2016-07-26
7
34 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 83

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 83

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 83

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
A procedure for exporting installed hotfix details of remote computers using powershell
In this video we show how to create a Contact 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 >> Contact ta…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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