0

i wrote a script for Exchange Management Shell. i've saved the script on the server. my question is: how can i run the script from a remote windows 10 Computer? *regarding to server authentiication - it's O.K for me to write my server Credentials on the script

i've try this script, but it's asking for user and password every time and cannot exeute the file.

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mailserver/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
Invoke-Command -ComputerName mailserver -FilePath \\mailserver\c$\Script\MPC3.ps1

EDIT: i want to be able to open the following PS on my Windows computer: MAPILab.Pop3Connector.Shell

Thanks for helping me !

Aviad Ofek
  • 13
  • 4
  • Invoke-Command also has a [-Session](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.2#example-17--access-a-network-share-in-a-remote-session) parameter. Shouldn't that suffice? – Theo Jan 10 '22 at 13:16
  • From the server, you can do `invoke-command computername scriptname.ps1`. – js2010 Jan 10 '22 at 16:50

1 Answers1

1

I do not recommend to store credentials inside a script. But here a way of doing it:

$Username = 'username'
$Password = 'passwd' | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList $Username, $Password
Patrick
  • 2,128
  • 16
  • 24
  • is correct. Look for premade Ps1 scripts that will help you not expose your username and password. I have that makes a file that if modified in anyway won't let the scripts that call it to work. – dcaz Jan 10 '22 at 14:06
  • Thanks for your answer. i've edited my question, hope you can help me – Aviad Ofek Jan 10 '22 at 15:31
  • Could you not simply run `Invoke-Command -ComputerName mailserver -FilePath \\mailserver\c$\Script\MPC3.ps1 -Credential $UserCredential`? – Patrick Jan 10 '22 at 16:24