#iHeartPowerShell
One of my colleague asked me a PowerShell script to query AD User information! Yes, I did your voice!
Import-Module ActiveDirectory Get-ADUser -Identity <samaccountname>
But, he asked “How will I do it in NODE JS with no AD Module?” Well! it’s too deep to discuss about node js! Anyways to make it simple I shared an app whichย serves as a REST endpoint to query AD User information from any platforms like ASP.NET , NODE JS, PowerShell etc.
var express = require('express'); var shell = require('node-powershell'); var app = express(); var ps = new shell({ executionPolicy: 'bypass', noProfile: true }); app.get("/:guid", function (req, res) { ps.addCommand("./user.ps1", [ { name: 'guid', value: req.params.guid } ]) ps.invoke().then(output => { console.log(req.params.guid) res.end(output); }) }) var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) })
The PS script for User.PS1 is using ADSI ๐ with a parameter for samaccountname which is named as GUID (A Friendly Name ๐ )!
param ( [Parameter(Mandatory = $true)] $guid ) try { $adsi = New-Object adsisearcher $propstoLoad = @("givenname" , "sn" , "samaccountname" , "mail"); $adsi.PropertiesToLoad.AddRange($propstoLoad) $adsi.Filter = "(&(ObjectCategory=User)(samaccountname=$guid))" $result = $adsi.FindOne().Properties [pscustomobject]@{ GivenName = $result['givenname'] -as [string] SurName = $result['sn'] -as [string] GUID = $result['samaccountname'] -as [string] mail = $result['mail'] -as [string] } | ConvertTo-Json } catch { $_.Exception }
Spin your NODE and browse for the result!ย REST URL : http://127.0.0.1:8081/ChenV
{ "GivenName": "Chendrayan", "SurName": "Venkatesan", "GUID": "ChenV", "mail": "chendrayan.venkatesan@contoso.com" }
The RESTful continues! Happy PowerShell!