NXLog main page
  • Products
    LOG COLLECTOR
    NXLog Enterprise Edition
    Full feature multi-platform log collection
    NXLog Community Edition
    Open-source free log collector
    ADD-ONS FOR NXLOG ENTERPRISE EDITION
    NXLog Add-Ons
    Integration with various software
    AGENT MANAGER FOR NXLOG ENTERPRISE EDITION
    NXLog Manager
    Manage and monitor NXLog instances
    NXLog Minder
    Hyper-scalable, API-first agent management
    DATABASE FOR NXLOG ENTERPRISE EDITION
    Raijin Database Engine
    The schemaless SQL database for storing events
    more from nxlog
    Professional Services
    Compare NXLog EE and CE
    NXLog Solution Packs
  • Downloads
    NXLog Enterprise Edition
    Full feature multi-platform log collection
    NXLog Manager
    Manage and monitor NXLog instances
    NXLog Community Edition
    Open-source free log collector
  • Solutions
    Integrations
    With SIEM, Devices, SaaS...
    Specfic OS support
    AIX, Linux, FreeBSD
    SCADA/ICS
    Energy, Oil & Gas, Transport...
    Windows Event log
    Collect locally or remotely, ..
    DNS Logging
    Enterprise-grade DNS log...
    Log Collection Modes
    Agent-based, Agentless or Cloud
    Agent Management
    Agents management and monitoring
    FIM
    File Integrity Monitoring
    macOS Logging
    ULS events, Apple System Logs ...
    By Industry
    Financial Services
    Government & Education
    Entertainment & Gambling
    Telecommunications
    Medical & Healthcare
    Military & Defense
    Law Firms & Legal Counsel
    Industrial & Manufacturing
  • Partners
    Find a Reseller
    Look for our resellers worldwide
    Technology Ecosystem
    See all our partners and integrations
    Partner Program
    Join our community of partners
  • Resources
    Documentation
    Products guides and integrations
    Blog
    Tutorials, updates and releases
    White papers
    Datasheets, infographics and more
    Videos
    Trainings and tutorial on specific topics
    Webinars
    Community events and webinars
    Case Studies
    Customer success stories
    Community Forum →
  • Support
  • Why Nxlog
    About Us
    Our journey, team and mission
    Customers
    Testimonials and case studies
    Careers
    We are hiring!
    Contact Us →
LOG COLLECTOR
NXLog Enterprise Edition
Full feature multi-platform log collection
NXLog Community Edition
Open-source free log collector
ADD-ONS FOR NXLOG ENTERPRISE EDITION
NXLog Add-Ons
Integration with various software
AGENT MANAGER FOR NXLOG ENTERPRISE EDITION
NXLog Manager
Manage and monitor NXLog instances
NXLog Minder
Hyper-scalable, API-first agent management
DATABASE FOR NXLOG ENTERPRISE EDITION
Raijin Database Engine
The schemaless SQL database for storing events
more from nxlog
Professional Services
Compare NXLog EE and CE
NXLog Solution Packs
NXLog Enterprise Edition
Full feature multi-platform log collection
NXLog Manager
Manage and monitor NXLog instances
NXLog Community Edition
Open-source free log collector
Integrations
With SIEM, Devices, SaaS...
Specfic OS support
AIX, Linux, FreeBSD
SCADA/ICS
Energy, Oil & Gas, Transport...
Windows Event log
Collect locally or remotely, ..
DNS Logging
Enterprise-grade DNS log...
Log Collection Modes
Agent-based, Agentless or Cloud
Agent Management
Agents management and monitoring
FIM
File Integrity Monitoring
macOS Logging
ULS events, Apple System Logs ...
By Industry
Financial Services
Government & Education
Entertainment & Gambling
Telecommunications
Medical & Healthcare
Military & Defense
Law Firms & Legal Counsel
Industrial & Manufacturing
Find a Reseller
Look for our resellers worldwide
Technology Ecosystem
See all our partners and integrations
Partner Program
Join our community of partners
Documentation
Products guides and integrations
Blog
Tutorials, updates and releases
White papers
Datasheets, infographics and more
Videos
Trainings and tutorial on specific topics
Webinars
Community events and webinars
Case Studies
Customer success stories
Community Forum →
About Us
Our journey, team and mission
Customers
Testimonials and case studies
Careers
We are hiring!
Contact Us →
Request trial
  • Loading...
Request Trial
April 21, 2023 strategy

Our customers asked - Execution of powershell scripts inside NXLog Exec modules

By Collins Maina

Share
ALL SIEM STRATEGY SECURITY ANNOUNCEMENT DEPLOYMENT COMPLIANCE COMPARISON RSS

PowerShell icon

PowerShell scripts can be used with NXLog for generating, processing, and forwarding logs, as well as for generating configuration content. In this article, we will take a look at how to execute PowerShell directly from NXLog.

You can run a PowerShell script in multiple NXLog instances without using any PowerShell script file, and is achievable through having the script code directly in NXLog’s exec modules. This is ideal because if you need to make any change to the script, it’s easier to modify just the NXLog module rather than change the script on every computer used.

For the sake of demonstration, this is the script we want to include in the NXLog config:

powershell-script.ps1
import-module activedirectory ;get-aduser -filter * -Properties * | Select-Object -Property @{Name='ACCOUNTEXPIRATIONDATE';Expression={$.ACCOUNTEXPIRATIONDATE.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")}}, ACCOUNTEXPIRES , @{Name='ACCOUNTLOCKOUTTIME';Expression={$.ACCOUNTLOCKOUTTIME.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")}} , BADLOGONCOUNT, CANNOTCHANGEPASSWORD, CANONICALNAME, CERTIFICATES, CITY,CN, COMPANY, COUNTRY,@{Name='Created';Expression={$.Created.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")}},DEPARTMENT, DESCRIPTION, DISPLAYNAME, DISTINGUISHEDNAME, DIVISION,EMAILADDRESS, EMPLOYEEID, EMPLOYEENUMBER, ENABLED, GIVENNAME, INITIALS,ISDELETED, @{Name='LASTBADPASSWORDATTEMPT';Expression={$.LASTBADPASSWORDATTEMPT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")}},@{Name='LASTLOGONDATE';Expression={$.LASTLOGONDATE.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")}}, LOCKEDOUT, MANAGER, MEMBEROF, @{Name='MODIFIED';Expression={$.MODIFIED.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")}}, NAME, OBJECTCATEGORY, OBJECTCLASS, OBJECTGUID, OBJECTSID, OFFICE, ORGANIZATION, OTHERNAME, PASSWORDEXPIRED, @{Name='PASSWORDLASTSET';Expression={$.PASSWORDLASTSET.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")}}, PASSWORDNEVEREXPIRES, PASSWORDNOTREQUIRED, PWDLASTSET, SAMACCOUNTNAME, SAMACCOUNTTYPE, SID, STATE, STREETADDRESS, SURNAME, TITLE, @{Name='WHENCHANGED';Expression={$.WHENCHANGED.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")}},@{Name='WHENCREATED';Expression={$_.WHENCREATED.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")}}  | export-csv -path 'C:\logs\results.csv'

This script collects data from Active Directory and parses it in a specific way. It will generate a file with that data, and NXLog must read that file once a day and then send it to a particular destination. The script is run just once a day.

Using im_exec

The im_exec module will run once and won’t be restarted if it exits. To run this module daily at a specific time, you must add an xm_exec module that restarts your im_exec module. Also, because the file created by the PowerShell script will have the same name, you need to delete the file before running the script to ensure NXLog reads the new file correctly.

The configuration should look like this:

nxlog.conf
<Extension exec_run_powershell_script>
    Module     xm_exec
    <Schedule>
        When    0 1 * * *
        Exec    exec("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" , 'Remove-Item -Path \""C:\Program Files\nxlog\RopesGray_adusers.csv\""');
        Exec    in_powershell->module_restart();
    </Schedule>
</Extension>

<Input in_powershell>
    Module     im_exec
    Restart    false
    Command    "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    Arg        import-module activedirectory ;get-aduser -filter * -Properties * | Select-Object -Property @{Name='ACCOUNTEXPIRATIONDATE';Expression={$.ACCOUNTEXPIRATIONDATE.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}, ACCOUNTEXPIRES , @{Name='ACCOUNTLOCKOUTTIME';Expression={$.ACCOUNTLOCKOUTTIME.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}} , BADLOGONCOUNT, CANNOTCHANGEPASSWORD, CANONICALNAME, CERTIFICATES, CITY,CN, COMPANY, COUNTRY,@{Name='Created';Expression={$.Created.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}},DEPARTMENT, DESCRIPTION, DISPLAYNAME, DISTINGUISHEDNAME, DIVISION,EMAILADDRESS, EMPLOYEEID, EMPLOYEENUMBER, ENABLED, GIVENNAME, INITIALS,ISDELETED, @{Name='LASTBADPASSWORDATTEMPT';Expression={$.LASTBADPASSWORDATTEMPT.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}},@{Name='LASTLOGONDATE';Expression={$.LASTLOGONDATE.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}, LOCKEDOUT, MANAGER, MEMBEROF, @{Name='MODIFIED';Expression={$.MODIFIED.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}, NAME, OBJECTCATEGORY, OBJECTCLASS, OBJECTGUID, OBJECTSID, OFFICE, ORGANIZATION, OTHERNAME, PASSWORDEXPIRED, @{Name='PASSWORDLASTSET';Expression={$.PASSWORDLASTSET.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}, PASSWORDNEVEREXPIRES, PASSWORDNOTREQUIRED, PWDLASTSET, SAMACCOUNTNAME, SAMACCOUNTTYPE, SID, STATE, STREETADDRESS, SURNAME, TITLE, @{Name='WHENCHANGED';Expression={$.WHENCHANGED.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}},@{Name='WHENCREATED';Expression={$_.WHENCREATED.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}  | export-csv -path 'C:\Program Files\nxlog\RopesGray_adusers.csv'
</Input>

The first module (exec_run_powershell_script) will delete the file and restarts the im_exec module to allow it to be executed again. Also, the timing for this module is defined as a cron job to allow you to specify the time you want it to run (in this case, every day at 1:00 AM).

The second module (in_powershell) has two sections:

  • Command To indicate you will run powershell.exe

  • Arg Contains the whole script as a single line. It doesn’t matter if the command has spaces, pipes, single quotes, or anything else. You just need to be careful to escape the double quotes correctly.

Using xm_exec

When using xm_exec, you don’t need any extra module because the execution time is defined inside the very same exec. Additionally, before the PowerShell script, there’s a line to delete the file first. In this scenario, you need to use the exec command instead of exec_async to ensure the file is deleted before the PowerShell script runs.

The configuration should look like this:

nxlog.conf
<Extension exec_powershell>
    Module    xm_exec
    <Schedule>
        When    0 1 * * *
        <Exec>
            exec("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" , 'Remove-Item -Path \""C:\Program Files\nxlog\customer.csv\""');
            exec("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" , 'import-module activedirectory ;get-aduser -filter * -Properties * | Select-Object -Property @{Name=\""ACCOUNTEXPIRATIONDATE\"";Expression={$.ACCOUNTEXPIRATIONDATE.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}, ACCOUNTEXPIRES , @{Name=\""ACCOUNTLOCKOUTTIME\"";Expression={$.ACCOUNTLOCKOUTTIME.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}} , BADLOGONCOUNT, CANNOTCHANGEPASSWORD, CANONICALNAME, CERTIFICATES, CITY,CN, COMPANY, COUNTRY,@{Name=\""Created\"";Expression={$.Created.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}},DEPARTMENT, DESCRIPTION, DISPLAYNAME, DISTINGUISHEDNAME, DIVISION,EMAILADDRESS, EMPLOYEEID, EMPLOYEENUMBER, ENABLED, GIVENNAME, INITIALS,ISDELETED, @{Name=\""LASTBADPASSWORDATTEMPT\"";Expression={$.LASTBADPASSWORDATTEMPT.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}},@{Name=\""LASTLOGONDATE\"";Expression={$.LASTLOGONDATE.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}, LOCKEDOUT, MANAGER, MEMBEROF, @{Name=\""MODIFIED\"";Expression={$.MODIFIED.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}, NAME, OBJECTCATEGORY, OBJECTCLASS, OBJECTGUID, OBJECTSID, OFFICE, ORGANIZATION, OTHERNAME, PASSWORDEXPIRED, @{Name=\""PASSWORDLASTSET\"";Expression={$.PASSWORDLASTSET.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}, PASSWORDNEVEREXPIRES, PASSWORDNOTREQUIRED, PWDLASTSET, SAMACCOUNTNAME, SAMACCOUNTTYPE, SID, STATE, STREETADDRESS, SURNAME, TITLE, @{Name=\""WHENCHANGED\"";Expression={$.WHENCHANGED.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}},@{Name=\""WHENCREATED\"";Expression={$_.WHENCREATED.ToUniversalTime().ToString(\""yyyy-MM-ddTHH:mm:ss\"")}}  | export-csv -path \""C:\Program Files\nxlog\RopesGray_adusers.csv\""');
        </Exec>
    </Schedule>
</Extension>
Note

The script is enclosed into single quotes and only the double quotes are escaped.

For more information on using PowerShell scripts and Logging PowerShell activity with NXLog, see the Windows PowerShell section in the NXLog User Guide.

GET STARTED TODAY:
CONTACT US Our experts are happy to help REQUEST A FREE TRIAL Give NXLog Enterprise Edition a try GET PRICING Request a quote

NXLog Ltd. develops multi-platform log collection tools that support many different log sources, formats, transports, and integrations. The tools help administrators collect, parse, and forward logs so they can more easily respond to security issues, investigate operational problems, and analyze event data. NXLog distributes the free and open source NXLog Community Edition and offers additional features and support with the NXLog Enterprise Edition.

This document is provided for informational purposes only and is subject to change without notice. Trademarks are the properties of their respective owners.

  • Our customers asked
Share

Facebook Twitter LinkedIn Reddit Mail
Related Posts

NXLog vs Splunk Universal Forwarder
7 minutes | January 16, 2023
NXLog provides native support for Google Chronicle
2 minutes | May 11, 2022
Aggregating macOS logs for SIEM systems
9 minutes | February 17, 2022

Stay connected:

Sign up

Keep up to date with our monthly digest of articles.

By clicking singing up, I agree to the use of my personal data in accordance with NXLog Privacy Policy.

Featured posts

Upgrading from NXLog Enterprise Edition 5 to NXLog Enterprise Edition 6
September 11, 2023
Announcing NXLog Enterprise Edition 6.0
September 11, 2023
The cybersecurity challenges of modern aviation systems
September 8, 2023
Raijin announces release of version 1.2
August 11, 2023
The Sarbanes-Oxley (SOX) Act and security observability
August 9, 2023
Log Management and PCI DSS 4.0 compliance
August 2, 2023
Detect threats using NXLog and Sigma
July 27, 2023
HIPAA compliance logging requirements
July 19, 2023
Announcing NXLog Enterprise Edition 5.9
June 20, 2023
Industrial cybersecurity - The facts
June 8, 2023
Raijin announces release of version 1.1
May 30, 2023
CISO starter pack - Security Policy
May 2, 2023
Announcing NXLog Enterprise Edition 5.8
April 24, 2023
CISO starter pack - Log collection fundamentals
April 3, 2023
Raijin announces release of version 1.0
March 9, 2023
Avoid vendor lock-in and declare SIEM independence
February 13, 2023
Announcing NXLog Enterprise Edition 5.7
January 20, 2023
NXLog - 2022 in review
December 22, 2022
Need to replace syslog-ng? Changing to NXLog is easier than you think
November 23, 2022
The EU's response to cyberwarfare
November 22, 2022
Looking beyond Cybersecurity Awareness Month
November 8, 2022
GDPR compliance and log data
September 23, 2022
NXLog in an industrial control security context
August 10, 2022
Raijin vs Elasticsearch
August 9, 2022
NXLog provides native support for Google Chronicle
May 11, 2022
Aggregating macOS logs for SIEM systems
February 17, 2022
How a centralized log collection tool can help your SIEM solutions
April 1, 2020

Categories

  • SIEM
  • STRATEGY
  • SECURITY
  • ANNOUNCEMENT
  • DEPLOYMENT
  • COMPLIANCE
  • COMPARISON
logo

Subscribe to our newsletter to get the latest updates, news, and products releases.

© Copyright 2023 NXLog Ltd.

PRIVACY POLICY TERMS OF USE

  • PRODUCTS
  • NXLOG ENTERPRISE EDITION
  • NXLOG COMMUNITY EDITION
  • NXLOG ADD-ONS
  • NXLOG MANAGER
  • NXLOG MINDER
  • RAIJIN DATABASE
  • MORE NXLOG
  • COMPARE SOLUTIONS
  • INDUSTRIES
  • INTERGRATIONS
  • FIND A RESELLER
  • PARTNER PROGRAM
  • RESOURCES
  • DOCUMENTATION
  • WHITE PAPERS
  • WEBINARS
  • CASE STUDIES
  • TUTORIALS
  • BLOG
  • COMMUNITY FORUM
  • ABOUT US
  • WHY NXLOG
  • CUSTOMERS
  • CAREERS
  • CONTACT US
  • DOWNLOADS
  • NXLOG ENTERPRISE EDITION
  • NXLOG COMMUNITY EDITION
  • NXLOG MINDER
  • NXLOG MANAGER
  • NXLOG ADD-ONS
  • RAIJIN DATABASE