Powershell - Multiple paths in to one variable (AD)

Welcome Forums General PowerShell Q&A Powershell - Multiple paths in to one variable (AD)

This topic contains 14 replies, has 3 voices, and was last updated by

 
Participant
2 months ago.

  • Author
    Posts
  • #166690

    Participant
    Topics: 2
    Replies: 8
    Points: 21
    Rank: Member

    Hello –

    I am working on a script to reach out to computers on AD which are  enabled and online.

    I used the script:
    $computers = Get-ADComputer ...
    $OnlineComputers = foreach($computer in $computers) {
        if (Test-Connection -ComputerName $computer.Name -Count 2 -Quiet) {
            Write-Host "$($computer.Name) is online."
            $computer
            }
            else
            {
            Write-Host "$($computer.Name) is offline. Skipping."
            }
    
    
             if (Test-Path "\\$($computer.Name\c$\Users\*" -PathType Container) {
            Write-Host "Path is there"
            }
        }
    
    

    All of that works fine to put the online computers in to the variable. Now I want to cycle through those online computers and put the paths ( $computer.Name\c$\Users\* ) in to another variable like  this:

    $directories = "$Computer1\c$\Users\*","$Computer2\c$\Users\*","$Computer3\c$\Users\*" – and so on.

    $directories = foreach ($OnlineComputer in $OnlineComputers) {     Get-ChildItem -Path "\\$($OnlineComputer.Name\c$\Users\*"    }

    This gives me the information of: Directory, Mode, LastWrittenTime, Length, Name.

    If I use Get-ChildItem -Path "\\$($OnlineComputer.Name\c$\Users\*" | Select Directory – It gives me an empty Directory list.

    Am I on the right track?

    Thanks for all you guys do here on this site!

  • #166720

    Participant
    Topics: 1
    Replies: 59
    Points: 328
    Helping Hand
    Rank: Contributor

    I'd think you'd want to use select Name and not select directory.

     

  • #166723

    Participant
    Topics: 2
    Replies: 8
    Points: 21
    Rank: Member

    I would think this also – but using names gives me only a list of the users on each machine. I specifically am looking for the path of each machine – for example the administration machines lets say have a a name structure of ADM- before the name of their machine so we know where they are at. I am looking for the path of ADM-DESK1\C$\Users\*, ADM-DESK2\C$\Users\*,ADM-DESK3\C$\Users\* to be put in to the $directories variable so if I were to output the variable it would read – $directories = "\\ADM-DESK1\C$\Users\*",\\ADM-DESK2\C$\Users\*", etc. I don't really care about the names of directories IN the Users folder, I just want to make sure that when I use the variable $directories that it will have ALL of the paths I iterated through in the foreach loop and in the configuration needed.

  • #166729

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    If i've got you right you could try following approach:

    $SearchBase = 'OU=Berlin,OU=Germany,OU=EMEA,OU=Regions,OU=Enterprise,DC=contoso,DC=com'
    Get-ADComputer -SearchBase $SearchBase -Filter * |
    ForEach-Object {
        $Online = Test-Connection -ComputerName $_.Name -Count 1 -Quiet
        $UserProfileList = if ($Online) { (Get-ChildItem -Path \\$($_.Name)\c$\Users -Directory) -join ',' }
        Else { 'n/a' }
        [PSCustomObject]@{
            Name            = $_.Name
            Online          = $Online
            UserProfileList = $UserProfileList
        }
    }

    ... of course you have to provide the proper SearchBase for your environment. 😉

  • #166759

    Participant
    Topics: 2
    Replies: 8
    Points: 21
    Rank: Member

    Thanks for that Olaf –
    When I use the example you gave, it gives me the information I am looking for in a tabled format. i.e.

    Name      Directories
    --------    --------------
    Comp1    name1,name2,name3,etc.
    Comp2    name1,name2,name3,etc.

    Below is what I changed in your example:

    $SearchBase = 'CN=****,DC=****,DC=***'
    Get-ADComputer -SearchBase $SearchBase -Filter {enabled -eq $true -and Name -like "ADM-*"} |
    ForEach-Object {
    $Online = Test-Connection -ComputerName $_.Name -Count 2 -Quiet
    $Directories = if ($online) { (Get-ChildItem -Path \\$($_.Name)\c$\Users) -join ',' }
    Else { 'Computer Not Online.' }
    [PSCustomObject]@{
    Name = $_.Name
    #Online = $Online
    Directories = $Directories
    }
    }

    What I am looking for is to take the information from above (custom object created) and have 1 variable that has the paths joined i.e.

    $DirectoriesJoined = "\\$($_.Name)\c$\Users\*","\\$($_.Name)\c$\Users\*","$($_,Name)\c$\Users\*"

    I want to grab the whole Users folder no matter what is inside it.

    Thanks again for your help!

  • #166771

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    I want to grab the whole Users folder no matter what is inside it.

    May I ask what exactly your end goal is? I tend to create simple, space saving data formats what makes it easier to use it for whatever further use case you want. This way you're not limited to one specific purpose.
    If you want to use this information later on you can assemble the desired target format there/then.

  • #166777

    Participant
    Topics: 2
    Replies: 8
    Points: 21
    Rank: Member

    May I ask what exactly your end goal is?

    I am trying to create a backup script – one where I select the department, iterate through the online computers, pull the online computers' path (Cmp1\c$\Users\*, Cmp2\c$\Users\*), and then put it in to a variable so that it's formatted a certain way ($DirectoriesJoined = \\Cmp1\c$\Users\*","\\Cmp2\c$\Users\*","\\Cmp3\c$\Users\*")

    I have a backup script already that I am building off of – which works if I manually input the paths. End goal is to be able to automate getting the paths and put them to the variable so I am not having to type 20 paths per department when I do backups.

  • #166780

    Participant
    Topics: 1
    Replies: 59
    Points: 328
    Helping Hand
    Rank: Contributor

    I'll take one last stab at this...

    create an empty array outside of your loop:

    $directories=@()

    Do your logic (assume it saves to $onlineComputers)

    foreach ($online in $onlinecomputers){
    $directories += "\\$online\C$\users\*"
    }

    That should populate the directories variable, even if its hard coded....
    The BAD thing about this (depending on the number of servers) is that the array is deleted and re-created each time. For a lot of servers, you might want to create an ArrayList and populate that.

  • #166786

    Participant
    Topics: 2
    Replies: 8
    Points: 21
    Rank: Member

    an0nemus09 –

    This! This gave me the output I needed. I tried using an empty array before but I completely forgot about the += operation.

    So this gives me the data in the format:
    \\cmp1\c$\Users\*
    \\cmp2\c$\Users\*
    \\cmp3\c$\Users\*

    I know if I output that $directories array with double quotes it turns it to a string – \\cmp1\c$\Users\* \\cmp2\c$\Users\*  \\cmp3\c$\Users\*

    If I wanted to output to be "\\cmp1\c$\Users\*","\\cmp2\c$\Users\*","\\cmp3\c$\Users\*" would I be able to use $ofs to format it in that way? Or is there another suggestion of a way to do this?

  • #166795

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    Hmmm ... that sounds unnecessary convoluted. If the computer is online I'd think you don't need to enumerate every single profile path.

    BTW: You can join the elements of an array with the -join operator. 😉 😀

  • #166798

    Participant
    Topics: 2
    Replies: 8
    Points: 21
    Rank: Member

    Hmmm ... that sounds unnecessary convoluted. If the computer is online I'd think you don't need to enumerate every single profile path.

    Olaf –

    I am not quite sure I understand that statement. If the computer is online, that's the only way I can reach it. I am backing up the complete Users folder due to the fact that our users log in to many different machines around the site. We don't use folder redirection and a lot of other stuff that we inherited when my team took over the shop. We're trying to get things back in some sort of order, but if we aren't backing up machines – we have no way of keeping users' data safe in the event that our machines take hits from lightning and other things.
    I am simply trying to keep user data backed up and automated until we can make larger changes to our structure.

    And thank you – I tried the -join operator and it worked perfectly. 🙂

  • #166801

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    I am not quite sure I understand that statement.

    If the computer is online you can simply backup all profile folders by providing the users folder. like ... \\computer\c$\users\* should be the same like \\computer\c$\users\user1,\\computer\c$\users\user2,\\computer\c$\users\user3,\\computer\c$\users\user4,\\computer\c$\users\user5,\\computer\c$\users\user6,\\computer\c$\users\defaultuser,... and so on.

  • #166804

    Participant
    Topics: 2
    Replies: 8
    Points: 21
    Rank: Member

    like ... \\computer\c$\users\*

    This is exactly what I am doing with the wild card character in to make sure I am getting ALL user folders in my foreach loop. Is this what you mean?

  • #166807

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    OK, never mind ... at least you solved the issue you had, right? Great. 😉

  • #166810

    Participant
    Topics: 2
    Replies: 8
    Points: 21
    Rank: Member

    OK, never mind ... at least you solved the issue you had, right? Great. 😉

    I did and I thank you for all the help you've given today!

You must be logged in to reply to this topic.