• Good day Mr. Figueroa, and a good day to all,

    I was quite surprised to find an answer from you. You have much better things to do than to help me.

    Yes, it does help me a lot, thank you.  ...and yes--making things more complicated than they need to be is one of my less-desirable traits. Don't ask to see my grill.

    I thank you for your assistance and the time you obviously put into this.

     

    I have added "-verbose" to the end of a few lines, making it work in my environment which is here:

    PS C:jctemp> write $psversiontable
    
    Name Value
    ---- -----
    PSVersion 5.1.19041.1
    PSEdition Desktop
    PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
    BuildVersion 10.0.19041.1
    CLRVersion 4.0.30319.42000
    WSManStackVersion 3.0
    PSRemotingProtocolVersion 2.3
    SerializationVersion 1.1.0.1

    Changed code:

    Added curley bracket at end of script, because PS accepted it. I am not sure where it actually belongs, or if it belongs at all, and I'm just messing up. PS wants the }  before the first ELSE statement. I put it at the end of the file.

    #OldProfNewProf_test2.ps1
    
    $OldProfDir = serveroldprofiles$
    $NewProfDir = servernewprofiles$
    
    # Write-Verbose -Message 'First check the new directory against the old directory' 
    Write-Verbose -Message 'First check the new directory against the old directory' -verbose
    
    Get-ChildItem -path $OldProfDir -directory | foreach-object {
    $OriginalDir = $_
    $DirName = $OriginalDir.BaseName.split('.')[0]
    $TestDir = Join-Path -path $NewProfDir -ChildPath $DirName
    if (Test-Path -path $TestDir ){
    Write-Verbose -Message ('User ({0}) has profiles in both directories, ignoring' -f $DirName) -verbose # I believe the -f switch tells it to format.
    }
    else {
    Write-verbose -Message ('User ({0}) has no matching profile in {1}; deleting old profile' -f $DirName, $NewProfDir) -verbose
    takeown.exe /a /f /r /skipsl "$OriginalDir"
    try {
    Remove-Item -path $TestDir -recurse -force -erroraction stop
    }
    catch {
    Write-Warning -Message ('Unable to completely remove {0}' -f $OriginalDir)
    }
    }
    Write-Verbose -Message 'Now check the old directory against the new directory' -verbose
    Get-ChildItem -path $NewProfDir -directory | foreach-object {
    $NewDir = $_
    $DirName = $_.basename 
    $OriginalDir = '{0}{1}.v2' -f $OldProfDir, $DirName
    if (Test-Path -path $OriginalDir) {
    Write-Verbose -Message ('User ({0}) exists in both places; ignoring' -f $DirName) -verbose
    }
    else {
    Write-Verbose "Well, you don't see that every day...."
    # decide what you want to do here.. your logic overlaps a different logic rule; so it doesn't make sense
    # You already have logic that cleans out the "original" directory
    }
    }}

    I have some output after making the changes:

    PS C:jctemp> .OldProfNewProf_test2.ps1
    AUSFÜHRLICH: First check the new directory against the old directory
    AUSFÜHRLICH: User (carp1) has no matching profile in server02profile_test$; deleting old profile
    Fehler: Ungültige Syntax. Wert für "/f" erwartet.
    Geben Sie "TAKEOWN /?" ein, um die Syntax anzuzeigen.
    WARNUNG: Unable to completely remove carp1.V2
    AUSFÜHRLICH: Now check the old directory against the new directory
    AUSFÜHRLICH: User (carp2) has no matching profile in server02profile_test$; deleting old profile
    Fehler: Ungültige Syntax. Wert für "/f" erwartet.
    Geben Sie "TAKEOWN /?" ein, um die Syntax anzuzeigen.
    WARNUNG: Unable to completely remove carp2.V2
    AUSFÜHRLICH: Now check the old directory against the new directory
    AUSFÜHRLICH: User (carp3) has no matching profile in server02profile_test$; deleting old profile
    Fehler: Ungültige Syntax. Wert für "/f" erwartet.
    Geben Sie "TAKEOWN /?" ein, um die Syntax anzuzeigen.
    WARNUNG: Unable to completely remove carp3.V2
    AUSFÜHRLICH: Now check the old directory against the new directory

    So, it looks like I'll be working on that TAKEOWN statement.

    I'm not sure what's going on with the -f switch. I don't know why PS is throwing errors for a formatting switch (I may be wrong on that...I'm not a professional...).

     

    Again, thank you for your assistance.

     

    I'm going to go home and work on this further.

     

    A great day to all...

    Best regards,

    -.-johnc/outedtrout

    0
  • Greetings all,

    I have been working this issue for well over a month now. I didn’t want to post, because I didn’t want to appear too dumb on this forum. You are all so smart and experienced. I thought I could figure this out on my own. I was wrong.
    Additionally, I would like to thank you all. Your experience published here has helped me very much in furthering my PS understanding.

    My question follows from the following facts:

    <initial script received from on-high>

    # Root-Ordner in dem die Userordner liegen
    $userroot = 'wffs01profile_old'
    # alle User aus dem AD lesen
    $users = get-aduser -Filter * | select -Expand SamAccountName
    gci $userroot | ?{$_.PSIsContainer -and ($_.Name -replace '.(V2|V5|V6)$','') -notin $users}

    I have two directories:

    • A) old AD Users
    • B) current AD Users
    • Each of the subdirectories in A has an extension of .V2.
    • None of the subdirectories in B has an extension of .V2.
    • I have assumed from the beginning that I need to remove the .V2 extension before I can do a compare (not sure if this is the best solution).

    This is the main issue.

    The files / profile directories as seen in Windows Explorer:

    Files as seen in Explorer

    I read that directory contents into $oldADUsers:

    PS C:WINDOWSsystem32> $oldADUsers =gci $userroot | % basename
    PS C:WINDOWSsystem32> $oldADUsers

    AfsgrA.V2
    afsgrb.V2
    afsgrC.V2
    AfsGrD.V2
    afsgre.V2
    afsgrf.V2
    afsgrg.V2
    afsgrh.V2
    afstelgte.V2

     

    I have a list of current AD users, read into $currentrADUsers:

    PS C:WINDOWSsystem32> $currentADUsers = get-aduser -filter * | sort-object | select samaccountname

    afsgra
    afsgrb
    afsgrc
    afsgrd
    afsgre
    afsgrf
    afsgrg
    afstelgte


    <h2>I am stuck here.</h2>
    VARIABLES:

    • $userroot:     fileserverprofile_testprofile_old$ -- Directory which holds old profile directories.
    • $oldADUsers  --  PS Variable which holds list of names of old AD users’ profile directories.
    • $currentADUsers  --  PS Variable which holds list of names of current AD users’ profile directories.

    I am stuck here.

    Here is my logic:

    1.       Read the names of the contents of the profile_test directory into the variable $oldADUsers.

    2.       Read the names of the current AD Users into the variable $currentADUsers.

    3.       If there exists a profile directory name in $oldADusers which matches a profile directory name in $currentADUsers, then leave it be with no changes.

    4.       If there exists NO profile directory name in $oldADUsers which matches a profile directory name in $currentADUsers, then delete the profile directory recursively from $oldADUsers.

    5.       If there exists a profile directory name in $oldADUsers for which there is NO match in $currentADUsers, then take ownership of that user’s old profile, and then delete recursively the user’s old profile directory from profile_test.

    PS C:WINDOWSsystem32> $userroot = 'fileserverprofile_test$'
    PS C:WINDOWSsystem32> $currentADUsers = get-aduser -filter * | select samaccountname
    PS C:WINDOWSsystem32> $oldADUsers = gci $userroot | % basename

    At this point, I have two lists of profile directory names:

    • $oldADUsers
    • $currentADUsers

    <p class="crayon-selected">where the individual entries in the $oldADUsers list have an extension of .V2, whereas the entries in the $currentADUsers list have no extensions.</p>
    *****

    What I believe I need to do:

    1. Remove the extensions from the directory names in $oldADUsers,
    2. compare the directory names to those in $currentADUsers,
    3. take ownership of the selected directory and
    4. perform the recursive delete.

    I believe I will need a for-loop similar to:

    Foreach-Object {$_.Name -replace ".(V2)",""}

    …but that won’t work, because I can’t replace with a null value.

     

    I am stuck here.

     

    Following is the code which I have used to get this far…..

    PS C:WINDOWSsystem32> $userroot = 'fileserverprofile_old$'
    PS C:WINDOWSsystem32> $currentADUsers = get-aduser -filter * | select samaccountname
    PS C:WINDOWSsystem32> $oldADUsers =gci $userroot | % basename

     

    I have tried separating the values in the $oldADUsers list:

    $oldadv = $oldADUsers.split(".")

    …but then I have array indices to worry about, which I think only makes things more complicated than they need to be.

    I have tried the following, but it will break on other lists:

    PS C:> $newADName = $oldADName.split(".")
    PS C:> $ADNamesingle = $newADName[0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40]
    PS C:> write $ADNamesingle

    ...

    AfsgrA
    afsgrb
    afsgrC
    AfsGrD
    afsgre
    afsgrf
    afsgrg
    afsgrh
    afstelgte

    ...

    OTHER CODE WHICH I'VE USED in this script WITHOUT SUCCESS:

    $cool = gci $userroot | ?{$_.PSIsContainer -and ($_.Name -remove '.(V2|V5|V6)$','')} ===> problem: -remove not recognised.
    $cool = gci $userroot | ?{$_.PSIsContainer -and ($_.Name -replace ".(V2|V5|V6)","")} ===> does not remove extension.
    $Files = $oldadv
    ForEach ($File in $Files)
    {
    $File.FullName | Copy-Item -Destination ("Drive:Path" + ($File.Name -replace '(.V2)','') )
    }
    Compare-Object -ReferenceObject (Get-Content -Path 'C:jctempmyusers.txt') -DifferenceObject (Get-Content -Path 'C:jctempoldadname.txt')
    
    get-childitem 'wffs02profile_testjc' -include *.V2 | foreach ($_) {remove-item $_.name}
    
    foreach-object in c:jctempmyusers.txt [io.path]::GetFileNameWithoutExtension("c:jctempmyusers.txt")
    
    foreach ($file in gci -literalpath "C:Temp") { rni $file.FullName $file.Name.Replace("A","B") }
    
    foreach ($file in gci -literalpath "wffs02profile_testjc$") { rni $file.FullName $file.Name.Replace("V2","Shit") -credential $credentials }

    I need help.

    Thank you in advance to any of you out there who take a look at this.

    I wish you all a nice start into your week.

     

    Best regards,

    -.-johnc

    0
  • carp liked the comment of carp (Rank: Level 2)
    on Introducing the 4sysops IT pro network. (So far, carp (Rank: Level 2)
    has 2 likes for this comment) 1 week ago

    0
  • The site looks great. I find it easy to navigate, and the drop-downs make finding what I'm looking for less difficult than analyzing a list of search engine results.

    I also thank you for the approval.

    Everyone a nice week ahead.

    Thank you.

    0
  • carp posted an update in the group Group logo of PowerShellPowerShell 3 weeks, 5 days ago

    Hello Group.

    I have just joined. Carp is my name. I am in Germany, and I although I use Powershell daily, I am still only beginning to learn it.

    I wish you all a good week.

    -.-carp

    2+
    avataravatar
  • carp became a registered member 3 weeks, 5 days ago

    1+
    avatar