joni
joni

Reputation: 11

Get-Content, looping through directories and writing to CSV

I have a set of subdirectories each containing one file with a .pub extension containing an SSH key. This key I then need to import to AD (this part is working from a CSV).

For now I have this working script where I import the SSH key from the CSV file to the AD object, and this works fine.

Import-Module activedirectory

Import-Csv "C:\Script\SSH Keys\users.csv" | ForEach-Object {
     
     Get-ADUser -Identity $_.SamAccountName
     Set-ADUser $_.SamAccountName -add @{'altsecurityidentities'=$_.altSecurityIdentities}

}

The CSV is simple and looks like this:

SamAccountName altSecurityIdentities
User1 ssh-123123123123XYZ
User2 ssh-234234234234XYZ

SamAccountName altSecurityIdentities User1 ssh-123123123123XYZ User2 ssh-234234234234XYZ

Now my thought was to automate this even more and loop through these subfolders and reading the content with Get-Content then writing these keys to a CSV file.

I have this so far where I can Get-Content from one file and write it to a CSV

Get-Content -Path "D:\Script\SSH Keys\1\*.pub"| Out-File -FilePath "D:\Script\SSH Keys\users.txt" -Append

I have 2 Questions - any pointer will help

  1. How can I loop through subdirectories (making a foreach) to get the content of all files in said subdirectories?

  2. How can I write to a specific part in the csv? Lets say the subfolder where the file is in has the same name as the SamAccountName in the table above. Any way I can match those?

C:\SSH\User1\key.pub

C:\SSH\User2\key.pub

Cheereo

Upvotes: 0

Views: 286

Answers (1)

Daniel
Daniel

Reputation: 5114

Where is your csv file with the existing ssh id coming from? Is it something that has data that you need to use or is that just the file that you've been piecing the data together in manually? Reason I ask is I'm not sure if it is necessary.

Here is the code to check for pub files recursively in c:\ssh

Get-ChildItem 'C:\SSH' -Filter *.pub -Recurse |
    ForEach-Object {
        [PSCustomObject]@{
            SamAccountName        = ($_.FullName -split '\\')[2]
            Path                  = $_.FullName
            altSecurityIdentities = Get-Content -Raw $_.FullName
        }
    }

It will output something like the results below which you could pipe to Export-Csv or you could just pipe directly to your existing Foreach-Object loop.

SamAccountName Path                      altSecurityIdentities
-------------- ----                      ---------------------
charley        C:\ssh\charley\id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDdiNcLyUwpqp75YbCjL44Bfc+vle3OMVEgoH9eBrfLo… 
daniel         C:\ssh\daniel\id_rsa.pub  ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDdiNcLyUwpqp75YbCjL44Bfc+vle3OMVEgoH9eBrfLo… 
tony           C:\ssh\tony\id_rsa.pub    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDdiNcLyUwpqp75YbCjL44Bfc+vle3OMVEgoH9eBrfLo…

The SamAccountName property isn't an actual SamAccountName unless the folder actually contains a SamAccountName which it sounded like it might from your post. I'm assuming "User1" and "User2" are valid SamAccountNames.

How can I write to a specific part in the csv? Lets say the subfolder where the file is in has the same name as the SamAccountName in the table above. Any way I can match those?

C:\SSH\User1\key.pub

C:\SSH\User2\key.pub

If this is a correct assumption then you can see that the output taken directly from the C:\SSH folder has a SamAccountName and the altSecurityId which I believe is all you need so this is why I wonder if the csv you originally mentioned is even necessary at this point or how it would fit into the solution. Please feel free to clarify/comment and I can modify my answer accordingly.

Edit I guess it's not an assumption, you state that the path actually has a SamAccountName name :) The code above should get the user/samaccountname properly as long as the path is how you say c:\ssh\someuser or could be c:\somefolder\someuser, just as long as the folder labeled as someuser is the 2nd level.

Upvotes: 0

Related Questions