LDAP query doesn't work when there is space in the OU - ldap

There are about 20 OU in AD. 6 of the OU has space in it. for example, "Department Heads", "Operation Managers". The following works all 15 OU where there are no space but doesn't work when there is a space in the OU description.
Any idea. I tried putting the string into "" but nothing helps.
$LdapServer = "FLEX01AD.COLGATE.FILA"
$SearchBase = "OU=Department Heads,DC=COLGATE,DC=FILA"
$LDAPResult = Get-ADUser -SearchBase $searchbase -SearchScope 'subtree' -Server $ldapserver -filter "employeeID="U99YBTTXR" -Properties * | Select -Property userAccountControl, whenChanged

Actually, spaces in the OU is not the issue. I found the solution here
https://community.spiceworks.com/topic/2054880-powershell-count-unique-users-in-group-and-nested-group
this code worked for me
$LDAPResultCount = ($LDAPResult.userAccountControl | select -Unique).count

Related

Manager DisplayName instead of LDAP name in ps script

Is there any way to combine these two scripts and output them in one csv file?
$Users = Get-ADUser -filter * -Properties Manager
$Users | Select Name,#{label="Manager";expression={(Get-ADUser $_.Manager -Properties DisplayName).DisplayName}}
Get-ADUser -Filter * -Properties * | Select-Object GivenName, Surname, mail, Description, co, City, Department, Company |export-csv -path c:\userexport.csv
In the latter script I wanted to add "Manager" as Select-Object but it returns with fulle ldap name, which I cant use later on to import on another system. I need to get manager displayname instead

When I convert to upper case azure-powershell command is not working as expected

In azure pipeline I have to give $env = prd in small letters as its being used by many othe tasks. But actual resoucrce group names look like rg-e2-PRD-703. SO I have given below commands but its not giving me output
## $env =prd is given in pipeline
$environment = "$(env)".ToUpper()
write-host $environment ## its printing PRD as expected
$getNIClist = Get-AzNetworkInterface | Where-Object {$_.ResourceGroupName -clike 'rg-*-$environment-*' | Select-Object
You can use the command as below:
$aa = 'rg-*-' + $environment + '-*'
$getNIClist = Get-AzNetworkInterface | Where-Object {$_.ResourceGroupName -clike $aa} | Select-Object
or you can also change the ' to ":
$getNIClist = Get-AzNetworkInterface | Where-Object {$_.ResourceGroupName -clike "rg-*-$environment-*"} | Select-Object
because escape will not be done in single quotes
Hope it helps~

Powershell and SQL Strings passed from AD

Okay here is an interesting one. I will try to simplify the issue down
In Powershell, lets say I use the following:
$users = Get-ADUser | Select samAccountName, givenName, sn
Active Directory example has 3 users:
First Name Last Name
__________ _________
Jen Jones
Jason O'Neil
Jake Johnson
I have a SQL Update command within powershell to update a table that looks like so:
ForEach($U in $Users){
$SQLAddInfo = "
UPDATE
Table1
SET
User_Last_Name = '$($User.sn)',
User_First_Name = '$($User.givenName)'
WHERE
User_ID = '$($User.samAccountName)'"
$SqlConnection.Open()
$SqlCmd.CommandText = $SQLaddInfo
$SqlCmd.Connection = $SqlConnection
$SqlCmd.ExecuteNonQuery()
$SqlConnection.Close()
}
Now the problem is that this works most of the time, but when I get to the last name of O'Neil, SQL thinks the apostrophe is a terminator for the SQL command. How do I use variables here and still have Powershell or SQL ignore the apostrophe?
Parameterized queries. Anything else, and O'Neil is far from your only problem.
.... coming back a day later, because I forgot to talk about Table-Valued parameters.
If the Get-ADUser call here produces 1000 results, you're gonna run 1000 SQL queries, and that's not fun. You can do better, by sending all of the data to the server in one step via a table-valued parameter, where you do an INSERT/SELECT that uses that parameter to only run one query.

Get-ADUser not functioning correctly

I've created a script that lists all users in a OU that are NOT a member of a certain group. This saves the results into a text file. I thought this was working fine until I took a username from the text file and searched it in Active Directory. Turned out the user was a member of the group I was trying to filter out. Here is the code -
Get-ADUser -SearchBase "OU=Users 2004,OU=Cyngor,DC=gwynedd,DC=rhwydwaith" -Filter {( memberof -ne "CN=CTX AppSense,OU=Rheoli,OU=Grwpiau,OU=TC Enviroment,DC=gwynedd,DC=rhwydwaith")} -Properties Name | select Name | Export-CSV "C:\Users.txt"
I can't figure out why this isn't working correctly. Any suggestions out there?
Thanks.
memberOf is a multi-valued attribute, i.e. a list of distinguished names. Use the -notcontains operator to check if does not contain a particular distinguished name:
$ou = 'OU=Users 2004,OU=Cyngor,DC=gwynedd,DC=rhwydwaith'
$dn = 'CN=CTX AppSense,OU=Rheoli,OU=Grwpiau,OU=TC Enviroment,DC=gwynedd,DC=rhwydwaith'
Get-ADUser -Filter * -SearchBase $ou -Properties Name, MemberOf |
? { $_.MemberOf -notcontains $dn } |
select Name |
Export-Csv 'C:\Users.txt' -NoType
Note that a user's primary group is not listed in the memberOf attribute. If the code should also handle primary groups you need to add a check for that:
$ou = 'OU=Users 2004,OU=Cyngor,DC=gwynedd,DC=rhwydwaith'
$dn = 'CN=CTX AppSense,OU=Rheoli,OU=Grwpiau,OU=TC Enviroment,DC=gwynedd,DC=rhwydwaith'
Get-ADUser -Filter * -SearchBase $ou -Properties Name, MemberOf |
? { $_.MemberOf -notcontains $dn -and $_.PrimaryGroup -ne $dn } |
select Name |
Export-Csv 'C:\Users.txt' -NoType

powershell and SQL

I am working with powershell and SQL using a query to extract drive information from a server
I am writing following query
set #sql = 'C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -c "Get-WmiObject -Class Win32_Volume -Filter ''DriveType = 3'' | select name,label,capacity,freespace | foreach{$_.name+''!''+$_.label+''|''+$_.capacity/1048576+''%''+$_.freespace/1048576+''*''}"'
xp_cmdshell #SQL
i get following output
output
*******************************************************************************************
C:\!RISCDCC36N03C$|139980.3984375%35242.921875*
D:\!RISCDCC36N03D$|139977.99609375%34774.08984375*
G:\!RISCDCSQL552G|92151.9375%46329.1875*
M:\!|0%0*
M:\RISCDCSQL557BMP\!RISCDCSQL557BMP|81911.9375%31869.3125*
M:\RISCDCSQL557DMP\!RISCDCSQL557DMP|40954.9375%37753.5*
M:\RISCDCSQL557CMP\!RISCDCSQL557CMP|20475.9375%7643.375*
T:\!RISCDCSQL563T$|81911.9375%15462*
R:\!RISCDCSQL561R$|35836.9375%19392.0625*
P:\RISCDCSQL560BMP\!RISCDCSQL560BMP|225278.9375%15844.625*
P:\!RISCDCSQL560P$|245759.9375%13014.75*
P:\RISCDCSQL560CMP\!RISCDCSQL560CMP|122876.9375%29950.9375*
P:\RISCDCSQL560AMP\!RISCDCSQL560AMP|102398.9375%100423.25*
L:\!RISCDCSQL556L$|20479.9375%5072.1875*
I:\!RISCDCSQL553I$|512003.9375%81162.5*
I:\RISCDCSQL553MP1\!RISCDCSQL553MP1|307200.9375%137322.9375*
X:\RISCDCSQL567CMP\!RISCDCSQL567CMP|97288.9375%45540.125*
X:\!|0%0*
X:\RISCDCSQL567AMP\!RISCDCSQL567AMP|35841.9375%28526.125*
U:\!RISCDCSQL564U$|66552.9375%7892*
NULL
**************************************************************************
I don't understand why for M drive and X drive it is giving 0%0.
I am using this information to calculate further growth of space. while calculating it is givng me divide by zero.
( docs http://msdn.microsoft.com/en-us/library/windows/desktop/aa394515%28v=vs.85%29.aspx )
It looks like drive M and X each have a full volume/partition (no free space) amongst the others.
Remove them from the output with an extra filter clause -
-Filter ''DriveType = 3 and freespace>0''
That works for me, in the Powershell prompt...
PS C:\Windows\System32> Get-WmiObject -Class Win32_Volume -Filter 'DriveType = 3
and freespace>0' | select name,label,capacity,freespace