Create exchange account with powershell script

Whenever I try to run powershell script on machine 1 to create exchange account on machine 2 I get an error stating scripts on enabled even though i have unrestricted execution policy on both the machines. I have both machines on same network and domain. Any help guys. Thanks

$ExchangeFQDN = “DomainName”
$User = “Username”
$PWord = ConvertTo-SecureString -String “Password” -AsPlainText -Force
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “http://$ExchangeFQDN/PowerShell/” -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
$Password = ConvertTo-SecureString -String “ABC123##” -AsPlainText -Force
New-Mailbox -UserPrincipalName $UserPrincipalName -Name $Name -Password $Password

@Pablito can u help here?

Hi @rameezimtiaz,
Try to run Studio with workflow as an Administrator.

@Pablito same issue. I think it has to do with some certificate. You can see it in the error as well. But idk how to resolve this certificate issue. When i try to run the script in powershell on machine 1 it works perfectly fine with no error of certificate.

I’m not 100% sure but problem is probably here:

$Session = New-PSSession

If you are running code with new pssession from powershell Windows can exchange certificates from one computer to another without any problem. But running this from UiPath Studio the certificate can’t be recognized as it’s not signed for the Studio. I think the only solution for this case is to make workflow for opening powershell and typing the script there directly.

@Pablito It could be the reason. I was able to install the certificate but still issue persists. The solution opening powershell and writing script works.
If u find anything else let me know.
Thanks alot.

1 Like

Hello,

Have you tried to replace the New-PSSession with the new module published by microsoft ?

This module will avoid the New-PSSession and replace it with Connect-ExchangeOnline cmdlet.