Run powershell script for remoting on MID server

Manasi Marathe
Tera Contributor

I am trying to run a powershell script on MID server for transferring a file from MID to an external server using flow and action. I am using script step to insert cmd on ecc queue for MID server and am not using the powershell step since I do not want to enter separate connection and credential details in Servicenow for MID server.

 

When I run the powershell script directly on MID server in an interactive session, file transfer works successfully. However, when I trigger the flow with script action, I observe the below error which states that access for PSremoting is denied.

 

Connecting to remote server "remote_server" failed
with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.

 

Kindly help me troubleshoot the issue further.

11 REPLIES 11

Hi @Manasi Marathe 

 

If possible, can you please share your PowerShell script. Let me see if we can tweak something to make it work. From ServiceNow end, nothing can be done as of now as you are not using PowerShell step.


Also, any particular reason for not using PowerShell step ? If you are concerned about storing Mid Creds, then you can use Mid Server Service Account to run your scripts.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Below is the powershell script I'm attempting to execute from MID server:

 

# set username and password
$password = ConvertTo-SecureString "MyPlainTextPassword" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ("username", $password)

 

# create new session with remote system
$so = New-PsSessionOption –SkipCACheck -SkipCNCheck
$session = New-PSSession -ComputerName destination_hostname -Credential $Cred -UseSSL -SessionOption $so

 

# copy file
Copy-Item "source location" -Destination "destination location" -ToSession $session

 

For running powershell step I need MID server credentials which cannot be shared with me. Hence the approach of using script action and triggering via ECC.

Hi @Manasi Marathe 

 

No need to get the Mid Server credentials. You can create a Windows Credential for your mid server and check "Use Mid Server Service Account" as shown in snip below :

 

AmitVerma_0-1705041033130.png

This will help the PowerShell step to authenticate to mid server via the Mid Server Service account so that you can run your PowerShell scripts on the mid server.

 

For the copy operation, let me simulate the conditions on my side. I check and let you know how we can avoid multi hop authentication issue.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Amit Verma
Kilo Patron
Kilo Patron

Also, just to be clear with the requirement, you are trying to copy a file from Mid Server to another remote server ?


Please mark this response as correct and helpful if it assisted you with your question.

Hi @Amit Verma 

Yes my requirement is to copy file from MID server to remote server.

 

I have created Windows credentials as suggested and used the same in powershell step. But logs have reported the same error again:

 

Connecting to remote server <hostname> failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic. HRESULT: [5] Stack Trace: