Help with error for MID server script to disable logon hours in Active Directory

gjz
Mega Sage

I am trying to create a custom action to use in a flow to automate disabling logon hours in Active Directory, but I keep getting an error.  If I run the PowerShell script outside of the MID Server, it works fine, but not within ServiceNow.  I've tried various ways to create the logonhours array in PowerShell, but they all result in the same error when I test the Action in Flow.  Can anyone help me with this, please?

 

Here is my MID Server Script:

if (test-path env:\SNC_userName) {
    $username = $env:SNC_userName;
}
 
If(-Not $username){
    $result = "User Name field is empty"
    $status = "Error"  
Else {
$username = $username -replace "%27", "'";
 
try {
    $logonHours = @{"logonHours" = ([byte[]](0..20 | ForEach-Object { 0 }))}
    $result = Set-ADUser  -Identity $username -Replace $logonHours
}
catch {
    $result = $_.Exception.Message
    $status = "Error"
  }
}
 
My custom action: 

gjz_0-1746739118467.png

 

And the error message I get when I run it:

gjz_1-1746739210625.png

 

 

1 REPLY 1

jonsan09
Giga Sage
Giga Sage

Whenever I've had issues running powershell scripts via flows it usually ended up being permissions related. I'd try the following to try to pinpoint the issue:


  • Try running the powershell script from the mid server as the service account from your credential alias
  • Check the service account your MID server service on the Server is running under, depending on your config it'll try running as that account instead of your connection alias.
  • Try changing the remoting type, I'd had a the powershell script try to run from a domain controller listed in the connection alias instead of the MID server. The service account didn't have permissions to do this.