at the beginning I would like to write that I am just learning to write scripts. I have a test domain "universum.local" in VBox set on Ubuntu 22.04 ADDC Samba. I would like to query a domain controller for a list of domain users (10) with bash script and save data about them to a json file. At the moment I was able to get the necessary information and save it to a txt file.
Here is my scripts code:
#!/bin/bash
clear
ldapuserslistfilename="ldapuserslist.txt"
ldapuserslistfile="$tmp/$ldapuserslistfilename"
ldapusersinfofilename="ldapusersinfo.txt"
ldapusersinfofile="$tmp/$ldapusersinfofilename"
# main code
touch $ldapuserslistfile
touch $ldapusersinfofile
samba-tool user list > $ldapuserslistfile
while read -r line ; do
for user in $line ; do
samba-tool user show $user >> $ldapusersinfofile
done
done < $ldapuserslistfile
# copying txt files for tests
cp $ldapuserslistfile /mnt
cp $ldapusersinfofile /mnt
# deleting files
if [ -f $ldapuserslistfile ] ; then rm -f $ldapuserslisfile ; fi
if [ -f $ldapusersinfofile ] ; then rm -f $ldapusersinfofile ; fi
There is output, all users are saved in the txt file in the form below:
dn: CN=Bruce Banner,OU=Users,OU=MARVEL,OU=UNIVERSUM,DC=universum,DC=local
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Bruce Banner
sn: Banner
givenName: Bruce
instanceType: 4
whenCreated: 20220926075536.0Z
whenChanged: 20220926075536.0Z
displayName: Bruce Banner
uSNCreated: 4128
name: Bruce Banner
objectGUID: d1fb86d4-17bc-43f2-af83-ca06fa733e9e
badPwdCount: 0
codePage: 0
countryCode: 0
badPasswordTime: 0
lastLogoff: 0
lastLogon: 0
primaryGroupID: 513
objectSid: S-1-5-21-2846706046-4262971904-2743650290-1109
accountExpires: 9223372036854775807
logonCount: 0
sAMAccountName: hulk
sAMAccountType: 805306368
userPrincipalName: [email protected]
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=universum,DC=local
pwdLastSet: 0
userAccountControl: 512
uSNChanged: 4132
memberOf: CN=Avengers,OU=Groups,OU=MARVEL,OU=UNIVERSUM,DC=universum,DC=local
distinguishedName: CN=Bruce Banner,OU=Users,OU=MARVEL,OU=UNIVERSUM,DC=universum,DC=local
I would like to have this data in json format like
{
"users": [
{
"cn" : "Bruce Banner",
"sn" : "Banner",
"givenName" : "Bruce",
"whenCreated" : "20220926075536.0Z",
"<objectname>" : "<value>",
"<objectname>" : "<value>",
},
{
<next user info>
},
{
<next user info>
}
]
}
objectname is the next user item like lastLogon, lastLogoff, etc. I would like to save all users in the json file so that I can read them with another powershell script on my computer
UPDATE:
I added the lines below
# conversion fron txt to json
jsonfilename="jsontestfile.json"
json="./$jsonfilename"
touch $json
ed -s $ldapusersinfofile << 'EOF' > $json
v/^cn:\|^sn:\|^givenName:\|^displayName:\|^name:\|^whenCreated:/d
,s/^\(.*[^:]*\): \(.*\)/"\1": "\2"/
g/cn\|sn\|givenName\|displayName\|name\|whenCreated/s/$/,/
,s/^/ /
g/lastLogon/t. \
s/.*/},/g
1,$-1g/}/t. \
s/.*/{/g
0a
{
.
$s/,//
,p
Q
EOF
between #main code section and # copying txt files for tests section and I have output to json file like
{
"cn": "James Rhodes",
"sn": "Rhodes",
"givenName": "James",
"whenCreated": "20220926075852.0Z",
"displayName": "James Rhodes",
"name": "James Rhodes",
"lastLogon": "0"
},
{
"cn": "T'Chala",
"givenName": "T'Chala",
"whenCreated": "20220926081521.0Z",
"displayName": "T'Chala",
"name": "T'Chala",
"lastLogon": "0"
},
{
"cn": "Stephen Strange",
"sn": "Strange",
"givenName": "Stephen",
"whenCreated": "20220926080942.0Z",
"displayName": "Stephen Strange",
"name": "Stephen Strange",
"lastLogon": "0"
}
to be able to read the jsonfile in my powershells script, there missing
{
"users": [
at the beginig data and
]
}
at the end of data to have file like
{
"users": [
{
"cn": "James Rhodes",
"sn": "Rhodes",
"givenName": "James",
"whenCreated": "20220926075852.0Z",
"displayName": "James Rhodes",
"name": "James Rhodes",
"lastLogon": "0"
},
{
"cn": "T'Chala",
"givenName": "T'Chala",
"whenCreated": "20220926081521.0Z",
"displayName": "T'Chala",
"name": "T'Chala",
"lastLogon": "0"
},
{
"cn": "Stephen Strange",
"sn": "Strange",
"givenName": "Stephen",
"whenCreated": "20220926080942.0Z",
"displayName": "Stephen Strange",
"name": "Stephen Strange",
"lastLogon": "0"
}
]
}
to read by PS script
Clear
$json = Get-Content <pathToFile>\jsontestfile.json -Raw | ConvertFrom-Json
foreach ($user in $json.users){
echo $user.cn
echo $user.sn
echo $user.givenName
echo "----------"
}
how to add missing characters?
I changed my code to this:
# add prefix
printf '{\n"users" :\n[\n' > $json
# add converted users data
ed -s $data << 'EOF' >> $json
v/^cn:\|^sn:\|^givenName:\|^whenCreated:\|^displayName:\|^name:\|^badPwdCount:\|^badPasswordTime:\|^lastLogoff:\|^primaryGroupID:\|^accountExpires:\|^sAMAccountName:\|^userPrincipalName:\|^pwdLastSet:\|^userAccountControl:\|^lastLogonTimestamp:\|^whenChanged:\|^lastLogon:\|^logonCount:\|^distinguishedName:/d
,s/^\(.*[^:]*\): \(.*\)/"\1" : "\2"/
g/cn\|sn\|givenName\|whenCreated\|displayName\|name\|badPwdCount\|badPasswordTime\|lastLogoff\|primaryGroupID\|accountExpires\|sAMAccountName\|userPrincipalName\|pwdLastSet\|userAccountControl\|lastLogonTimestamp\|whenChanged\|lastLogon\|logonCount/s/$/,/
,s/^/ /
g/distinguishedName/t. \
s/.*/},/g
1,$-1g/}/t. \
s/.*/{/g
0a
{
.
$s/,//
,p
Q
EOF
# add suffix
printf ']\n}' >> $json
It now works and Powershell can read my file.