none
PS1 logonScript RRS feed

  • Question

  • How do I make a logonscript that copy to a folder on the profile a docx whose name is the userPrincipalName

    Test-local.ps1
    ----------------
    import-module activedirectory
    $users = Get-ADUser -filter * -searchbase "OU" -Properties *
    foreach ($user in $users) {
    $account_name = "$($User.userPrincipalName)"
    Copy-Item \\server\file\$account_name.docx c:\Users\$env:username\AppData\Roaming\APP
    }

    Thanks!

    Wednesday, May 18, 2016 9:11 AM

Answers

  • Hi Thor,

    you got a point there, sorry, didn't read the question properly. Very well, then this should work for you:

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = "(samaccountname=$($env:USERNAME))"
    $objSearcher.SearchScope = "Subtree"
    $objSearcher.PropertiesToLoad.Clear()
    $objSearcher.PropertiesToLoad.Add("userPrincipalName") | Out-Null
    $res = $objSearcher.FindAll()
    $upn = $res.Properties["userprincipalname"][0]
    Copy-Item "\\server\file\$($upn).docx" "c:\Users\$($env:username)\AppData\Roaming\APP"

    This should give you the information you need and all users should have access to the information (and you don't need the AD Module in order to run it).

    Cheers,
    Fred


    There's no place like 127.0.0.1


    • Edited by FWN Wednesday, May 18, 2016 1:58 PM Fixed the error I made in the filter and suppressed the stray output from calling Add()
    • Marked as answer by ThorElPoderoso Wednesday, May 18, 2016 6:19 PM
    Wednesday, May 18, 2016 9:40 AM

All replies

  • Hi Thor,

    it is a simple line like this:

    Copy-Item "\\server\file\$($env:Username).docx" "c:\Users\$($env:username)\AppData\Roaming\APP"

    Note: This will not overwrite the file, so when the file is already there it errors, but continues without copying. To overwrite, add the parameter "-Force".

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, May 18, 2016 9:15 AM
  • Hi

    But the problem is that the docx takes the userPrincipalName

    Wednesday, May 18, 2016 9:23 AM
  • Hi Thor,

    you got a point there, sorry, didn't read the question properly. Very well, then this should work for you:

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = "(samaccountname=$($env:USERNAME))"
    $objSearcher.SearchScope = "Subtree"
    $objSearcher.PropertiesToLoad.Clear()
    $objSearcher.PropertiesToLoad.Add("userPrincipalName") | Out-Null
    $res = $objSearcher.FindAll()
    $upn = $res.Properties["userprincipalname"][0]
    Copy-Item "\\server\file\$($upn).docx" "c:\Users\$($env:username)\AppData\Roaming\APP"

    This should give you the information you need and all users should have access to the information (and you don't need the AD Module in order to run it).

    Cheers,
    Fred


    There's no place like 127.0.0.1


    • Edited by FWN Wednesday, May 18, 2016 1:58 PM Fixed the error I made in the filter and suppressed the stray output from calling Add()
    • Marked as answer by ThorElPoderoso Wednesday, May 18, 2016 6:19 PM
    Wednesday, May 18, 2016 9:40 AM
  • How to get the current users UPN at logon:

    $sid=(New-Object System.Security.Principal.NTAccount($env:Username)).Translate([System.Security.Principal.SecurityIdentifier]).value
    ([adsi]"LDAP://<SID=$sid>").UserPrincipalName
    

    Method #2

    ([adsisearcher]"samaccountname=$env:username").FindOne().Properties['userprincipalname']

    Both methods are equally fast and equally reliable.  Name Translate() is faster because it does not return the whole user object.


    \_(ツ)_/


    • Edited by jrv Wednesday, May 18, 2016 3:10 PM
    Wednesday, May 18, 2016 3:09 PM
  • How to quickly get the full user object from AD for the current user:

    $user=[adsi]([adsisearcher]"samaccountname=$env:username").FindOne().GetDirectoryEntry()


    \_(ツ)_/

    Wednesday, May 18, 2016 3:17 PM