# Invoking Powershell script from Rule

**URL:** <https://developer.sailpoint.com/discuss/t/invoking-powershell-script-from-rule/11575>\
**Category:** IIQ Discussion and Questions\
**Tags:** rules, identityiq\
**Created:** [May 23, 2023, 7:22am UTC](https://developer.sailpoint.com/discuss/t/invoking-powershell-script-from-rule/11575 "2023-05-23T07:22:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prathyusha\_174](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Prathyusha\_174](https://developer.sailpoint.com/discuss/u/Prathyusha_174)\
**Post date:** [May 23, 2023, 7:22am UTC](https://developer.sailpoint.com/discuss/t/invoking-powershell-script-from-rule/11575/1 "2023-05-23T07:22:48Z")

</div>

Hi All,

We have tried to invoke the PowerShell script through rule, it is creating the .ps1 file , but script is not getting executed.

If we try the same script from application connector Rule(native rule), it is working.

Any help will be appreciated!!.

Code snippet We are using to create the file :

FileCreationRule.xml

$value = New-Item -Path ‘\ServerPath\E$\test\testfile1112.emp’ -ItemType “File”;  
$log.Debug(“exiting New-FileCreation”);  
return $value;

code snippet to invoke the PowerShell script from rule:

```java
String ruleName= "FileCreationRule";
Rule rules = context.getObject(Rule.class, ruleName);
Application ad = context.getObject(Application.class, "Active Directory");
List iqServiceConfig = ad.getAttributes().get("IQServiceConfiguration");
Map iqServiceConfigMap = iqServiceConfig.get(0);
String host = iqServiceConfigMap.get("IQServiceHost");
int port = Integer.parseInt(iqServiceConfigMap.get("IQServicePort"));
Map dataMap = new HashMap();
dataMap.put("postScript", rules);
dataMap.put("Application", ad.getAttributes());
RPCService service = new RPCService(host, port,false, false);
service.setConnectorServices(new sailpoint.connector.DefaultConnectorServices());
RpcRequest request = new RpcRequest("ScriptExecutor", "runAfterScript", dataMap);
RpcResponse response = service.execute(request);
if (response == null) {
return null;
}
if (response.getErrors() != null &amp;&amp; response.getErrors().size() > 0) {
throw new IllegalStateException(response.getErrors().toString());
}

return response.toXml();

```

Thanks,  
Prathyusha

---

<div class="post-metadata">

**Author:** ![brian\_weigel](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/brian_weigel/32/2194_2.png) [@brian\_weigel](https://developer.sailpoint.com/discuss/u/brian_weigel)\
**Post date:** [May 23, 2023, 3:50pm UTC](https://developer.sailpoint.com/discuss/t/invoking-powershell-script-from-rule/11575/2 "2023-05-23T15:50:18Z")

</div>

> [@Prathyusha\_174](#):
>
> service.setConnectorServices(new sailpoint.connector.DefaultConnectorServices());

In the example Compass post I shared in your original post - [Creating file in Windows shared location from Sail Point IIQ](https://developer.sailpoint.com/discuss/t/creating-file-in-windows-shared-location-from-sail-point-iiq/11319/) - I didn’t see `service.setConnectorServices(new sailpoint.connector.DefaultConnectorServices());` in the sample code.

As a basic troubleshooting step, I’d try creating a “dummy” powershell script that does something really simple, like writing some warning messages (via something like `Write-Warning` or `Write-Error` and create a local file/folder on the IQService host itself. That should help provide some indication of whether the issue is with the invocation of the script itself, or there’s something in the script that’s failing and not returning the failed status back to your rule’s invocation.

---

<div class="post-metadata">

**Author:** ![ismaelmoreno1](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/ismaelmoreno1/32/2756_2.png) [@ismaelmoreno1](https://developer.sailpoint.com/discuss/u/ismaelmoreno1)\
**Post date:** [June 13, 2023, 6:38pm UTC](https://developer.sailpoint.com/discuss/t/invoking-powershell-script-from-rule/11575/3 "2023-06-13T18:38:30Z")

</div>

hi @Prathyusha_174

The code to invoke Powershell looks fine.  
Could you try add following line in FileCreationRule.xml file at the beginig of the rule?

```auto
Add-type -path C:\<IQServicePath>\Utils.dll

```

As you mentioned, rule works properly though application connector Rule, because you are using IQService to provision, but when you try execute powershell code using external rule, in the code you are not importing IQService Library

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sailpoint/original/2X/f/f2136700ed5e3703e0b85e02f6be799dacca7735.png) [@system](https://developer.sailpoint.com/discuss/u/system)\
**Post date:** [August 12, 2023, 6:39pm UTC](https://developer.sailpoint.com/discuss/t/invoking-powershell-script-from-rule/11575/5 "2023-08-12T18:39:00Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
