Ruby delete_if block - arrays

This is using Ruby 1.9.
I have a delete_if block:
#logHash[date].delete_if{ | logItem | logItem.name == name }
Where logItem.name is the name of the logItem and name is the name of the logItem I'm looking for. This works fine, except it deletes each logItem with the specified name. Is there a way to find the first item with an equal name and only delete that? So if there are two logItems that have the same name, I only want to delete one of them. Any ideas?

I'd use Array#index to find the index of the first item which had a matching name, and then Array#delete_at to delete it.
index_to_delete = #logHash[date].index {|log_item| log_item.name == name}
#logHash[date].delete_at(index_to_delete)

Related

What is a way to create a unique name for a file given it's name and a namespace value in the same directory?

I have to create a file name for a file that will exist with other files with the same or similar name in the same folder.
I have two values that will be used to define the name. The namespace and the class name.
So if there are a team of users and user A creates a Person class they have to give it a unique namespace name so if user B creates a Person class does not conflict with user A's class name.
Traditionally, namespaces are defined something like,
com.amazon.category.people
http://www.amazon.com/2022/categories/7.0.0
I'm ok with joining the namespace with the class name to create a unique phrase like so:
com.amazon.category.people::Person
BUT Here's the problem. When I'm creating a file, the filesystem doesn't like all those dots or colons Actually, I don't like them because multiple reasons. I thought about replacing all dots and colons with underscore.
com_amazon_category_people_Person.json
That's the best I've come up with. It won't work well with the other type of namespace:
http___www_amazon_com_2022_categories_7.0.0_Person
Are there any answers this problem?
Names containing dots aren't a problem for any OS, as far as I know. What you can do is either:
Put the files in a folder hierarchy, where every folder is the name of a namespace segment, like this:
+-me
| +-topchetoeu
| | +-myprogram
| | +-Program.json
| +-somebodyelse
| +-calculator
| +-Add.json
| +-Subtract.json
| +-Divide.json
| +-Multiply.json
+-com
+-microsoft
+-windows
+-Spyware.json
Another solution would be to separate the namespace segments with dashes, and the class name with a # symbol, resulting in something like this:
me-topchetoeu-myprogram#Program.json
me-somebodyelse-calculator#Add.json
me-somebodyelse-calculator#Subtract.json
me-somebodyelse-calculator#Divide.json
me-somebodyelse-calculator#Multiply.json
com-microsoft-windows#Spyware.json
etc...

AD User Array CSV search and analyze error

i want to search to an exported Active Directory list but i got one problem.
AD Files looks like this and its saved as CSV-File (I added a Header to the file)
CN=ARV-PRO7,OU=etc,OU=etc,DC=etc,DC=etc,DC=etc
CN=BLAPO86z,OU=etc,OU=etc,DC=etc,DC=etc,DC=etc
This is a example (there are 37 more entrys like ARV in the "where" is shoreted it for better view) of the Code im trying to run:
$Datei = Import-Csv "C:\FILESPATH\AD-list.csv"
$Datei | where {$_.CN -notmatch "ARV" -and $_.CN -notmatch "BBN"} | Out-GridView -Title "Site Info"
This shows me all the items with no ARV in it, thats what i want.
BUT it ignores also the items which looks like this:
CN=PR122ARVO7,OU=etc,OU=etc,DC=etc,DC=etc,DC=etc
Because ARV is in the name the item is not shown.
I need a way to check if the first 3 letter ARV than its okay and he can ignore it but i also need him to show me names with ARV in it to check the name rules. ( Department-PCNAME ) and not (BLABLA-PC-DEPARTMENT-NAME)
I Hope its clear what iam asking and what my problem is ^^
If I understand correctly, you would like to ignore items starting with ARV. This should work (simplified, only where section is important here):
#{CN='ARV-PRO7'},
#{CN='BLAPO86z'},
#{CN='PR122ARVO7'} | where { !$_.CN.StartsWith('ARV') }

filling attribute with concatenated string

I am looking for a way to concatenate a string and put it in one active directory user account object, to be precise, in altsecurityidentities.
The value to be input will be as following:
" which is constant, and (firstName)(whitespace)(lastname) (custom value which can be taken from another attribute, which is in form x.yyyyyyyy.z (what matters to me is yyyyyyy part (.substring(2,8)) works like charm here.
I'd like to do it for several accounts that are listed in variable of type TypeName: Microsoft.ActiveDirectory.Management.ADUser.
So that it can be set for all accounts under $accounts variable.
So far I have the code to create the attribute value for one account listed in there:
$accounts | %{'constant value'+$.givenname+' '+$.surname+' '+'('+$(($_.attributename).substring(2,8))+')'}
This, i'd like to put in altsecurityidentities attribute value, log to event viewer success and errors
You're almost there really just need to apply the value to the desired field:
$accounts | ForEach-Object { Set-ADUser -Identity $_ -Add #{altsecurityidentities = "constant value $($_.givenname) $($_.surname) ($($_.attributename.substring(2,8)))"} }
I have tidied up your code by embedding the variables in a string rather than using concatenation, which is much cleaner.

How do I save CSV data to variables based on a ListBox selection?

I have a drop down list in my script that gets populated by the "name" column in a CSV file. I'd like to save the information in the other columns that go with that name into variables to be used later.
For example (bullets are there so they show underneath each other, showed as one line otherwise):
Name,Address,PhoneNumber,Email
Bob,1234 Bob Rd,123-4567,bob#email.com
Bill,5678 Bill Ave,246-8024,bill#email.com
The drop down list would show Bob and Bill. Based on what I pick I'd like the rest of their info to be saved into 3 different variables, $Address, $PhoneNumber, and $Email.
I've got as far as importing the CSV into the array and sorting them alphabetically.
$Customers = #(Import-CSV "$dir\Apps\Customers.csv")
$Array = $Customers.name | Sort-Object
Here's part of the drop down box code:
ForEach ($Choice in $Array) {
[void] $companybox.Items.Add($Choice)
}
After that I can't figure out how to grab the correct information based on what's been chosen when I click a button.
Here's the button code:
$handler_gobutton_Click = { $company = $companybox.SelectedItem
....
}
I have a few If statements in there right now for an earlier solution that doesn't use CSV's, but I'd like to move to a CSV file if possible.
Thanks!
EDIT: I'm thinking what I need to do is somehow find the array count number based on the name column. After that I should be able to save $array[0].address into a variable, for example. Finding how to compare the variable with the name in it to the name in the array and from that getting the count number is coming up empty so far... Ideas?
Got it to work!
$handler_gobutton_Click = {
$company = $companybox.SelectedItem
$index = [array]::IndexOf($customers.name,$company)
If ($customers[$index].uninstall -eq "True") { $uninstallbox.checked = $True }
... and so on
}
Source: https://www.reddit.com/r/PowerShell/comments/t1928/finding_array_index_number/

Active Directory, LDAP UserPrincipal

Got this information when I wrote out the DistinguishedName property on UserPrincipal class.
CN=Test Testie, OU=123,OU=Company,OU=Accounts,DC=myServer,DC=local
And im woundering if there is a property to get the nr 123 from OU. Is there any other property to get that or is this the best way to filter out my information?
\No. There is no "Parent" property.
Every object bellow the root has a parent being either an organizational unit (OU=) on a container (CN=). So just parse what is between the first two commas and remove OU= and CN=. That way you have the Name property.
Beware that it is possible to have objects with commas in their names. It then look like this:
Great OU, The
CN=Test Testie,OU=Great OU\,The,OU=Company,OU=Accounts,DC=myServer,DC=local
Testie, Test
CN=Testie\, Test
,OU=123,The,OU=Company,OU=Accounts,DC=myServer,DC=local
So you need to check for a backslah before parsing! If found, you then need to parse, between the first and the third comma.
Here is a link about "special" characters in Distinguished Names.
Ok, it's working but it doesn't feels like the best practice to do like this:
var ctx = new PrincipalContext(ContextType.Domain, "myDomain.local");
var user = UserPrincipal.FindByIdentity(ctx, "myUser");
var auth = user.GetAuthorizationGroups().Any(x => x.Name.Contains("myGroup"));
Because the thing is that I need to use contains because the group could look like this:
myGroup.xxxx.111
Any better way to soulve this?

Resources