Remote session terminated when running Powershell script at 'Start-Process' - powershell

I'm attempting to run a Powershell script that calls the Start-Process function in order to begin installing some monitoring software. I'm attempting to run it via a remote shell session I entered using Enter-PSSession. I've verified that the argument list and file path are correct in the line below.
Start-Process -FilePath $Installer -ArgumentList $ArgList -Wait -PassThru
When the script reaches this line however, it closes my remote session with the server I'm in and throws one of the following errors:
Processing data for a remote command failed with the following error message: The WSMan provider host process did not return a proper response. A provider in the host
process may have behaved improperly. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : ResourceUnavailable: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : System.Management.Automation.Remoting.PSRemotingDataStructureException
or
Starting a command on the remote server failed with the following error message : An operation is being attempted on a session that is being closed.This can happen if the
session that is being used is also being closed by another thread. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : ResourceUnavailable: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : System.Management.Automation.Remoting.PSRemotingDataStructureException
I've tried doubling the max memory allocation per shell to 2Gb but that hasn't helped. Has anybody experienced an issue like this before?

Related

Running runas/psexec in powershell remote session is not working

I have a remote machine where PS-Remoting enabled and I am able to run commands and scripts from my client/workstation. I want to run certain tasks as different user, in other words not the same user as I connect the remote-session.
Why I am doing this ?
I am using puppet-bolt for remote execution, where I can re-use some already existing puppet modules. And this bolt tools uses WinRM.
Issue I am facing is, while executing the below command directly on the remote machine works fine. I am using jetbrains runas utility.
# Executing this command directly on the remote machine's powershell prompt works.
C:\agent\tools\custom_runas.exe '-u:ad_domain\username' '-p:password' C:\Windows\System32\HOSTNAME.EXE
If I execute the same command over PS-RemoteSession (or) Invoke-Command gives me same error again. User which I use to connect WinRM service is an administrator user.
PS C:\WINDOWS\system32> Invoke-Command -ComputerName $CName -Credential $Crdn -ScriptBlock { C:\agent\tools\custom_runas.exe '-u:ad_domain\username' '-p:password' C:\Windows\System32\HOSTNAME.EXE }
JetBrains RunAs x86 1.0.0.061
Copyright (C) 2017 JetBrains. All rights reserved.
Runs a process under the specified windows user account.
Argument(s): -u:ad_domain\username -p:***** C:\Windows\System32\HOSTNAME.EXE
Settings:
user_name: username
domain: ad_domain
working_directory: C:\Users\UserName\Documents
exit_code_base: -100000
integrity_level: auto
inheritance_mode: auto
show_mode: hide
self_testing: 0
executable: C:\Windows\System32\HOSTNAME.EXE
NotSpecified: (:String) [], RemoteException
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : remotemachine
NotSpecified: (:) [], RemoteException
Error: Access is denied.
CreateProcessWithLogonW returns the Win32 error 0x00000005.
command_line_args:
Some observations, running "runas" command on the PSRemoteSession session doesn't wait for entering password and I also tried using PsExec.exe apart of JetBrains runas which also has the same behavior.

Hyper-v Export-VM : Provider load failure error in 2012r2

I'm using powershell 4 to export all the VMs in hyper-V and move it to a backup server(nas unix). Export was successful for few VMs and after a while it fails with the following errors:
Export-VM : Provider load failure
At C:\scripts\test.ps1:37 char:5
Export-VM -Name $vmname -Path $ExportPath
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Export-VM], ManagementException
FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.ExportVMCommand
This is the error i get in event viewer:
The description for Event ID 18350 from source Microsoft-Windows-Hyper-V-VMMS cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
If the event originated on another computer, the display information had to be saved with the event.
The following information was included with the event:
71706037-090D-4B74-BB0B-AEE96D47D898
%%2147942432
0x80070020
The locale specific resource for the desired message is not present
The same script works fine in windows 2016 server running powershell 5. I'm not sure if this is OS specific or it has to do with any updates. Please share your view on this and help me resolve this issue.
Update
I also get this Error:
Get-VM : Quota violation
At line:1 char:1
+ Get-VM
+ ~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-VM], ManagementException
+ FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.GetVMCommand
This issue occurred because Hyper-V service became unresponsive. Restarting Windows Management Instrumentation service or hyper-v service fixed this 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 PSSessionStateBroken error

I use the following powershell command to RDP to a server and start a service. It was working fine until last week before we cut over the server to a different data center.
Invoke-Command -ComputerName itbldw13 {Start-Service -Name bamboo-remote-agent}
The error that i get after the cut over is
Connecting to remote server itbldw13 failed with the following error message : The client cannot connect to the destination specified in the request.
+ CategoryInfo : OpenError: (itbldw13:String) [], PSRemotingTrans portException
+ FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken
Could someone help me fix this error please

Powershell Remoting Failing After Server Restore

Our server had been set up to have Powershell Remoting enabled, so that we can remotely execute scripts from our developer machines. I should add that I am not massively fluent in Powershell and was not involved in setting this up.
However recently our server suffered a fault and subsequently had to be restored. The restoration process was supposed to restore Windows back to its last backed up state including all programs and data. However since the restore, Powershell Remoting no longer works on the server.
I receive the following error now when running Enter-PSSession ServerName.
Enter-PSSession : Connecting to remote server failed with the following error message : <f:WSManFault xmlns:f="http://s
chemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="2147749890" Machine="ServerName"><f:Message></f:Message></f:WSManFaul
t> For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:16
+ enter-pssession <<<< ServerName
+ CategoryInfo : InvalidArgument: (ServerName:String) [Enter-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : CreateRemoteRunspaceFailed
Or I get the following error when running New-PSSession ServerName
[ServerName] Connecting to remote server failed with the following error message : <f:WSManFault xmlns:f="http://schemas.
microsoft.com/wbem/wsman/1/wsmanfault" Code="2147749890" Machine="ServerName"><f:Message></f:Message></f:WSManFault> For
more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [], PSRemotingTransportExc
eption
+ FullyQualifiedErrorId : PSSessionOpenFailed
(I have replaced the actual server name above with ServerName for privacy purposes)
Any help is much appreciated
Based on this Microsoft Connect item, try adding-SessionOption (New-PSSessionOption -NoMachineProfile) to your Enter-PSSession command.
I have no idea why this would be needed, but it's worth a shot.