Using MAPI to create a PST file with WinRM - powershell

I have the following PowerShell code which creates a PST file
enter code Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.AddStore("C:\tmp\file.pst")
Running this code locally works excellent, but I have to run this code remotely. The script is sitting on the Windows machine and I'm starting it from Linux using WinRM (python) in that setup, it hangs on AddStore(). I assume this is related to user/security context but I can't find the resolution or info if it is possible to run it with some configuration changes.
EDIT: Ok, it is not hanging it just taking a long time before it will fail with the following error:
New-Object : Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed
due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005
(CO_E_SERVER_EXEC_FAILURE)).
At C:\\tmp\\pst.ps1:3 char:12
+ $Outlook = New-Object -comobject Outlook.Application
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
You cannot call a method on a null-valued expression.
At C:\\tmp\\pst.ps1:4 char:1
+ $namespace = $Outlook.GetNameSpace("MAPI")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\\tmp\\pst.ps1:6 char:1
+ $namespace.AddStore("C:\\tmp\\file.pst")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
'

CO_E_SERVER_EXEC_FAILURE means Outlook.exe and your code are running in different security contexts, and hence the COM system refuses to marshal calls between the two processes.
Firstly, Outlook cannot be used from anything that runs outside of an interactive user context (such as service or a scheduled task). Secondly, does it work if Outlook is not yet running on the target machine?
That being said, Extended MAPI (C++ or Delphi only) code runs in-proc might be able to do the job. If C++ or Delphi are not an option, you can try Redemption (any language - I am its author) - RDOSession.LogonPstStore is probably what you need - it creates and deletes a temporary profile that loads the specified PST file.

Related

Selenium WebDriver Chrome could not be started in Powershell

I am trying to start the web automation with using Chrome driver and Selenium. But I am getting an error that, web driver was not loaded. Below is my code entered. If someone could help me to correct this and start to automate would be appreciated.
Import-Module 'C:\AHKM\ChromeAutomation\WebDriver.dll'
$WebDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
$WebDriver.Navigate().GoToUrl('www.google.com')
Error is below:
You cannot call a method on a null-valued expression.
At line:3 char:9
+ $WebDriver.Navigate().GoToUrl('www.google.com')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
We resolved this issue by supplying the folder where chromedriver.exe is located, e.g.
$WebDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver -ArgumentList "C:\TEMP"

I have a problem with "Create an Azure AD App" Specifically with the powershell Script "Create-AADIdentityApp.ps1"

I have installed Azure stack development kit and i am trying to install the App service.
Then when i have to run the PS1 script Create-AADIdentityApp.ps1 i fill in the following parameters
DirectoryTenantName
AdminArmEndpoint
TenantArmEndpoint
CertificateFilePath
Then i get the following error
New-Object : Exception calling ".ctor" with "3" argument(s): "The system cannot find the file specified.
"
At C:\app-service-certificate-install\Create-AADIdentityApp.ps1:81 char:24
+ ... rtificate = New-Object System.Security.Cryptography.X509Certificates. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
does anyone have any experience?
You provided a invalid CertificateFilePath.
It should be Full path to the identity application certificate file generated earlier.
Please check and correct it.

Local User Credential Check in windows 2012 server

I am trying to validate the local user credential using the below code:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine',$ComputerName)
$status=$DS.ValidateCredentials($UserName, $Password)
I am getting the below error:
Exception calling "ValidateCredentials" with "2" argument(s): "The specified network name is no longer available.
"
At line:1 char:1
+ $ds.ValidateCredentials('User','Password')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : PrincipalOperationException
Please help me to fix this out.
Thanks in advance
Note: I am not getting this error in all the machine. Out of 87 machines i am getting this error in 12 machines alone.
Starting 'server' service fixed the issue

Powershell scripts will not work with Jenkins plugin on Windows

I'm writing some pretty simple scripts in Powershell 32-bit and they are working fine.
For instance, I want to open internet explorer to a webpage and start typing in keys. The following code works fine when I run it in powershell ISE.
Add-Type –AssemblyName System.Windows.Forms
$url = "http://WebAddress"
$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate($url)
sleep 30
[System.Windows.Forms.SendKeys]::SendWait("{2}{tab}{H}{E}{L}{L}{O}")
I have downloaded a Windows PowerShell plugin and have executed the same code through it and received the following errors:
Building on master in workspace C:\Program Files\Jenkins\workspace\Jenkins Test
[Jenkins Test] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\WINDOWS\TEMP\hudson257018662776252417.ps1'"
New-Object : Creating an instance of the COM component with CLSID {0002DF01-000
0-0000-C000-000000000046} from the IClassFactory failed due to the following er
ror: 80004005.
At C:\WINDOWS\TEMP\hudson257018662776252417.ps1:4 char:17
+ $ie = New-Object <<<< -com "InternetExplorer.Application"
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMExcept
ion
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Comman
ds.NewObjectCommand
You cannot call a method on a null-valued expression.
At C:\WINDOWS\TEMP\hudson257018662776252417.ps1:5 char:13
+ $ie.Navigate <<<< ($url)
+ CategoryInfo : InvalidOperation: (Navigate:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At C:\WINDOWS\TEMP\hudson257018662776252417.ps1:9 char:42
+ [System.Windows.Forms.SendKeys]::SendWait <<<< ("{2}{tab}{H}{E}{L}{L}{O}")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Is there a reason why my code will not work through Jenkins like it does in Powershell? I tried some simple echo scripts which worked fine, but any more complex and it throws an error.
You may need to set execution policy once: https://technet.microsoft.com/library/hh849812.aspx
Set-ExecutionPolicy Unrestricted
Run PowerShell as admin and run that command. There may be security concerns using Unrestricted. But if it works, read the link above and find the policy that works best in your context. Options are (copy/pasted from the link):
Restricted. Does not load configuration files or run scripts. Restricted is the default execution policy.
AllSigned. Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on
the local computer.
RemoteSigned. Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
Unrestricted. Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet,
you are prompted for permission before it runs.
Bypass. Nothing is blocked and there are no warnings or prompts.
Undefined. Removes the currently assigned execution policy from the current scope. This parameter will not remove an execution policy
that is set in a Group Policy scope.

Powershell, enable windows update error

I'm trying to configure windows update with the Microsoft.Update.AutoUpdate COM object with Powershell v4, on a Windows 2008 r2. I'm however running into a little trouble. When trying to set the NotificationLevel, I'm getting the following error (Note running with elevated permissions).
$WUSettings = (New-Object -com "Microsoft.Update.AutoUpdate").Settings
$WUSettings.NotificationLevel=4
$WUSettings.save()
Output:
Exception setting "NotificationLevel": "Exception from HRESULT:
0x80240037" At line:1 char:1
+ $WUSettings.NotificationLevel=4
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterSetValueTI
Any help greatly appreciated!