Unable to run PowerShell commands on the Windows server where the MID Server is installed.

sivasumantk
Tera Contributor

I am unable to run PowerShell commands on the Windows server where the MID Server is installed. My use case is to upload a file into servicenow from midserver folder files. I am able to discover the Windows server through the MID Server (Quick discovery) and run a few basic commands. However, when I try to run the following PowerShell command, I receive the error message shown below. There are no firewall issues, and I can access the files and paths from the pattern debug, but I am unable to understand why my code is not working in the probe. Any suggestions would be appreciated

 

This is the powershell script:

# Eg. User name="admin", Password="admin" for this code sample.
$user = "admin"
$pass = "Welcome@123"

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')
# $headers.Add('Content-Type','text/plain') ??didn't seem to need this

# Specify endpoint uri
$uri = "https://xxxxx.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=a9e76edf87c8f..."

# Specifiy file to attach
$fileToAttach = "C:/RPA Screenshots/QueueProcess.png"

# Specify HTTP method (POST, PATCH, PUT)
$method = "POST"

# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -InFile $fileToAttach

# Print response
$response.RawContent

 

This command i had written in the WMI Probe under Probe Parameters value 

sivasumantk_0-1764069262762.png

 

 

1 REPLY 1

Prahlad Kumar
Tera Guru

Hello @sivasumantk 

 

When you attempt to run this PowerShell script from a ServiceNow Probe:

$response = Invoke-WebRequest .

it fails because:

- MID Server Probes/Commands DO not allow outbound internet calls using PowerShell by default

 

Any script that uses:

Invoke-WebRequest

Invoke-RestMethod

.NET HTTPClient

Custom authenticated REST calls is blocked by the MID Server security policy, unless explicitly enabled.

 

- MID Server Whitelisting is Required

Your script is not whitelisted, without whitelisting, MID will silently block the command.

This exactly matches MID Server PowerShell security restrictions.

 

Solutions:

  1. Enable PowerShell Script Execution on MID Server

* On the MID Server machine, run PowerShell as Administrator:

* Set-ExecutionPolicy Unrestricted -Scope LocalMachine

Then restart the MID service:

stop midserver

start midserver

 

Thank You
Prahlad Kumar
LinkedIn:- https://www.linkedin.com/in/prahlad-kumar-92a877117/