New-MailboxExportRequest error when running from CMD - powershell

I have a very simple PowerShell script that runs the new-mailboxexportrequest command. if I run the script as a scheduled task, I get the following error:
new-mailboxexportrequest : Failed to communicate with the mailbox database.
If I open the PowerShell command prompt, and run the script, I get the same error.
The script only works when I manually open the exchange management shell and run the script.
Keep in mind that the first line of the script actually has the exchange snapin command:
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
The question is, what loads from the exchange management shell that doesn't load on when the snapin runs?
Also, the scheduled task, the manual powershell command and the exchange management shell commands are running on the same box (exchange server). I'm also running them with the same user account.
Any help is appreciated.

the reason for your problem is explained in this link:
https://blogs.technet.microsoft.com/rmilne/2015/01/28/directly-loading-exchange-2010-or-2013-snapin-is-not-supported/
this should fix your problem :
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<ServerFQDN>/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
you can check this also :
powershell -psconsolefile "C:\Program Files\Microsoft\Exchange Server\Bin\exshell.psc1" your-exchange-command

Related

Using invoke-command scriptblock with exchange management shell

I've written some scripts to automate some user add and user modifications functions. But there is one last piece to the puzzle I can't figure out.
I need to run some commands in exchange management shell on the exchange server from a local powershell session, like an invoke-command scriptblock.
Is this possible?
Will adding Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
to the beginning of my script block accomplish this?
As for this...
Will adding Add-PSSnapin
Microsoft.Exchange.Management.PowerShell.SnapIn
You can't do this natively, without installing the EMC directly on your host.
Secondly, there is no real reason to. You can use PSRemoting to proxy the Exchange cmdlets to your host. The cmdlets are only available during the session
This process is the same whether you are using Exchange on-prem or Exchange online, though Exchange Online points to the O365 URI.
This has been documented in several places via the MS provided docs as noted here:
Connect to Exchange servers using remote PowerShell
Connect to a remote Exchange server
1.On your local computer, open Windows PowerShell, and run the following command:
$UserCredential = Get-Credential
In the Windows PowerShell Credential Request dialog box that opens, enter your user principal name (UPN) (for example, chris#contoso.com) and password, and then click OK.
2.Replace with the fully qualified domain name of your Exchange server (for example, mailbox01.contoso.com) and run the following command:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<ServerFQDN>/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Note: The ConnectionUri value is http, not https.
3.Run the following command:
Import-PSSession $Session -DisableNameChecking
https://learn.microsoft.com/en-us/powershell/exchange/exchange-server/connect-to-exchange-servers-using-remote-powershell?view=exchange-ps
Remote PowerShell in Exchange 2013
1.On your local computer, open Windows PowerShell and execute the following command:
$UserCredential = Get-Credential
2.After entering the credentials prompted when you executed the above command, execute the following:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<FQDN of Exchange 2013 Client Access server>/PowerShell/ -Authentication Kerberos -Credential $UserCredential
3.Finally, import the session established above with the following:
Import-PSSession $Session
https://blogs.technet.microsoft.com/nathanscott/2015/06/14/remote-powershell-in-exchange-2013/
Yes you can, some commands have to be run locally, below is an example. You can do the same with Exchange.
Invoke-Command -ComputerName $srv -ScriptBlock{Add-PSSnapin Microsoft.Forefront.Filtering.Management.Powershell;
Get-EngineUpdateInformation}

Keeping power shell console open after running script

I created a script to manage my exchange server remotely the only way I can run it with powershell console staying open after execution is by running the script by browsing to the directory once in the shell console when I just double click the script it runs then closes which defeats the purpose of it. I have tried the no exit parameter but when I use it keeps running executing the script I simply need the console to execute the script once then stay open below is my script and what I have tried is there a way to accomplish this with out editing the registry of my work desktop?
Script:
$UserCredential = Get-Credential
$session = new-pssession -configurationname microsoft.exchange -connectionuri http://my exchange server/powershell/ -authentication kerberos -credential $usercredential
import-pssession $session
And the script that keeps executing the script over and over:
PowerShell -NoExit -File "C:\SomeFolder\SomePowerShellScript.ps1"
I have tried
PowerShell -NoExit -Command "Write-Host 'This window will stay open.'"
This allows the console to stay open but end the connection to my exchange server when I hit enter

Run Exchange command from Windows Powershell

I've connected to EMS from Windows Powershell using the below:
&"C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1 "
I can't run Get-ExCommands or Disable-Mailbox -Identity Test without getting an error. "The term Get-ExCommands is not recognized as the name of a cmdlet, function, script..."
Is it possible to run Exchange commands in Win Powershell?
This works fine for me:
. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
Connect-ExchangeServer -auto
If you did not install the Exchange Management Tools on the server you are using you can connect to exchange this way:
$s = New-PSSession -ConnectionUri http://putyourservernamehere/Powershell -ConfigurationName Microsoft.Exchange
import-pssession $s
I suspect you need to import the Exchange PowerShell module. Take a look at this:
Exchange Powershell - How to invoke Exchange 2010 module from inside script?

remote powershell in azure VM

Since many days I am trying to sysprep an azure virtual machine.
Following is the command that I am using to connect to the VM from local and executing the Upload-GoldImage.ps1 script
Invoke-Command -ConnectionUri $uri -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -Credential $credential -ScriptBlock {C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -file "c:\program files\WindowsPowershell\Upload-GoldImage.ps1" -validateCurrentOS}
remote machine gets connected successfully. All the commands in the ps1 file are executed successfully but only the sysprep command isn't executing.
The ps1 mentioned above contains one command to sysprep the machine - Start-Process -FilePath C:\Windows\System32\Sysprep\sysprep.exe -ArgumentList '/generalize /oobe /shutdown /quiet'
Write-Host("After sysprep")
When this gets executed on my local powershell prompt - "After sysprep" is getting printed. I am getting no error in the sysprep command. Still the machine is not getting sysprepped.
Please if anyone could help me out
Thanks.
Finally I was able to sysprep the machine from my local powershell.
Previously my script used Start-Process command.
But then, http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx#Diagnostics_Process_Start
- this link helped me out, where I tried various several ways mentioned in the link.

Restart IIS on remote machine using Powershell

I have a TFSserver and a QAserver. I am using autodeployment using TFS and have a powershell script that do the requirement.
But I have a issue in restarting the QA server IIS from the same power shell script.
i am doing the following set of commands for restarting the IIS.
/*struser is in the administrator group of the QAserver
$cred = New-Object System.Management.Automation.PSCredential ("$QAserver$struser", $password )
$session = new-pssession $oceane_server -Auth Negotiate -Credential $cred
/* Some deployment script */
invoke-command -session $session -ScriptBlock {iisreset /stop}
Following error appears :
Access denied, you must be an administrator of the remote computer to use this
command. Either have your account added to the administrator local group of
the remote computer or to the domain administrator global group.
I could not find the solution for this. Any help would be appreciable.
The quick work around is to open all the ports of your server and run iisreset [MACHINENAME] /stop.
For powershell remoting, I use credssp because it allows the double-hop. Also, did you set your execution policy to bypass?
Set-ExecutionPolicy Bypass