Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remote powershell login

Posted on 2016-07-26
7
Medium Priority
?
60 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 85

Accepted Solution

by:
oBdA earned 2000 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
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 
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

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

A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
The main intent of this article is to make you aware of ‘Exchange fail to mount’ error, its effects, causes, and solution.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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