Need execute PowerShell commands in a remote server (ex: 10.0.0.0) via mid server without using any custom plugin or Orchestration

Ben138
Mega Expert

Hi ,

 

Need execute PowerShell commands in a remote server (ex: 10.0.0.0) via mid server without using any custom plugin or Orchestration 

 

is there any method to archive this 

 

ex PowerShell commands are get-service and need to capture its response as well

6 REPLIES 6

VivekSattanatha
Mega Sage
Mega Sage

Hi,

 

Bit tricky but achievable you can refer to the below link for windows command line execution. You can do the similar way with PowerShell as well

 

http://www.john-james-andersen.com/blog/service-now/command-line-execution-with-servicenow-mid-servers.html

 

Regards,

Vivek

Rahul Priyadars
Giga Sage
Giga Sage

You can use PS remote Shelling concept for same from Mid Server.

https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7.2

Regards

RP

Raj_Esh
Kilo Sage
Kilo Sage

Hi Ben,

 

The first question which comes to my mind before executing any commands is: 

 

Is the MID server and the remote server is in the same vLAN? if yes, then you can execute the following command:

Enter-PSSession -ComputerName 10.0.0.0

Once it's connected you can execute the PowerShell commands. 

 

Refer PowerShell Commands on Remote Computers

 

Hope it helps.

 

Thanks,

Raj

 

--Raj

Ben138
Mega Expert

Hi Community,

 

It helped me half way below code i am using to get sample response for hostname or Get-Service of the target server (i.e 10.0.0.0 == myserver.dc)

 

var ecc = new GlideRecord("ecc_queue");
ecc.initialize();//to create record
ecc.agent = "mid.server.mymidserver";
ecc.topic = "Command";
ecc.name = "Powershell.exe Hostname ; Get-Service";
ecc.source = "myserver.dc:mymidserver";
ecc.queue = "output";
ecc.state = "ready";
ecc.insert();
//gs.log(ecc.sys_id);
gs.sleep(1000);
var ecc1 = new GlideRecord("ecc_queue");
ecc1.addQuery("topic=Command^nameLIKEPowershell^queue=input");
ecc1.orderByDesc('sys_created_on');
ecc1.query();
ecc1.setlimit(1);
if(ecc1.next()){
var opp =ecc1.payload
}
gs.log(opp)

 

I'm able to get the payload output in opp here but now i need to pass few more  sql powershell commands to get response of the sql DB like update , insert or read data

 

Sample powershell command is

 

Read-SqlTableData -ServerInstance "myserver" -DatabaseName "Test" -SchemaName "dbo" -TableName "MyTable" -TopN 3

 

can you help to to pass this above command on the ecc script by using the target server myserver via midserver  as mymidserver 

 

Note: Here myserver is DB server i have required permissions on the server and its table to perform the above powershell commands and powershell module related to SQL were also installed and midserver and DB server i.e myserver are also in same vlan 

 

Thanks,

Ben