Manager DisplayName instead of LDAP name in ps script - properties

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

Related

Trouble converting file stored in SQL table to file stored on disk (missing content)

I have a database table with following columns (among others):
emailAttachment
emailAttachmentFileName
I am using the following Powershell to convert a .csv file to binary, to store in emailAttachment (defined as varbinary(max)):
$("0x$(([Byte[]](Get-Content -Encoding Byte -Path c:\temp\test.csv) | ForEach-Object ToString X2) -join '')")
As far as I can tell, that works fine.
To retrieve the data, I run this SQL query:
$query = #'
SELECT
*
FROM
[dbo].[mydatabase].[mytable]
WHERE
id = 1
'#
$file = Invoke-Sqlcmd -Query $query -ServerInstance server.domain.local -Credential (Get-Credential)
Now that I have the bytes from SQL, I want to write it to a file:
[IO.File]::WriteAllBytes($file.emailAttachmentFileName, $file.emailAttachment)
That creates the file with the correct name and some of the rows of the .csv, but not all of them (63 instead of 464).
I also tried both, but they have the same result:
[IO.File]::WriteAllLines($file.emailAttachmentFileName, [System.Text.Encoding]::ASCII.GetString($file.emailAttachment))
[IO.File]::WriteAllText($file.emailAttachmentFileName, [System.Text.Encoding]::ASCII.GetString($file.emailAttachment))
What am I doing wrong here?

Powershell - Can I scrape SQL file and compare results?

I am fairly new to Powershell and have run into a bit of a pickle.
I am trying to scrape a SQL script in txt file format. My goal is to check if every created (volatile) table is also dropped in the same file. And if not, output the name of the table which is not dropped in the SQL script.
On top of that I would like check if the "drop" of the table is AFTER "create volatile table" and not BEFORE, because that would be wrong syntax. Do you think it's somehow possible?
I tried to do it by extracting lines of codes where is the create table string, then using regex to get name(string) of the table and saving it to variable. I did the same with the "drop table...". Now I am trying to compared those two (string) variables by converting them to list, object or whatever. I feel like I am in dead end.
$vtMatch = Get-ChildItem "$packagepath\Scripts\" -Recurse | Select-String -Pattern "create multiset volatile table VT_","create multiset volatile table vt_"
$vtMatch = $vtMatch.line
$vt = $vtMatch | Select-String 'vt_(.*?)(?=,)' -AllMatches | Select-Object -Expand matches | ForEach-Object {$_.value}
$dropMatch = Get-ChildItem "$packagepath\Scripts\" -Recurse | Select-String -Pattern "drop table VT_","drop table vt_"
$dropMatch = $dropMatch.line
$drop = $dropMatch | Select-String 'vt_(.*?)(?=;)' -AllMatches | Select-Object -Expand matches | ForEach-Object {$_.value}
$missing = Compare-Object -ReferenceObject $vt -DifferenceObject $drop -Property item
$missing
The variable $vt contains these strings:
vt_R_transaction_Dm
vt_h_bank_account
vt_DD_Agreement_Detail_1
vt_DD_Agreement_Detail_2
vt_DM_Agreement_Detail_Prebase
vt_DM_Agreement_Detail_RWA
vt_DM_Agreement_Detail_1
vt_posted_transaction
vt_DM_Corp_Objective_Specific_MAT
vt_DM_Party_Detail
And variable $drop contains these strings:
vt_DD_Agreement_Detail_1
vt_DD_Agreement_Detail_2
vt_DM_Agreement_Detail_Prebase
vt_DM_Agreement_Detail_RWA
vt_DM_Agreement_Detail_1
vt_DM_Corp_Objective_Specific_MAT
vt_posted_transaction
vt_DM_Party_Detail

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

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

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