Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Set homedrive and create directory, need to output to CSV

$
0
0

Hello Experts

 

I'm relatively new to Powershell scene, and also new to this site!

I'm currently trying to automate as many things as possible. One thing I've been looking at recently is automating the creation of Homedrives. If the directory is already created, the script will set the correct permissions on the folder.

So far I've come up with the following:

 

$Users = Get-ADuser -SearchBase 'OU of the users' -Filter *

$Path = "\\fileserverpath"

 

foreach ($user in $Users)

{

 

    Set-ADUser $user -HomeDrive P: -HomeDirectory $Path$($user.SamAccountName)

 

    if (!(Test-Path -Path $path$($user.SamAccountName))) {

        $HomeFolderACL=Get-Acl $Path

   $HomeFolderACL.SetAccessRuleProtection($false,$true)

 

        New-Item -ItemType directory -Path $Path$($user.SamAccountName)|Out-Null

        $ACL=New-Object System.Security.AccessControl.FileSystemAccessRule($($user.SamAccountName), "FullControl","ContainerInherit,ObjectInherit","InheritOnly","Allow")

   $HomeFolderACL.AddAccessRule($ACL)

 

   Set-Acl -Path $Path$($user.SamAccountName) -AclObject $HomeFolderACL

    }

    Else {

        $HomeFolderACL=Get-Acl $Path

   $HomeFolderACL.SetAccessRuleProtection($false,$true)

 

        $aclold = Get-Acl $Path$($user.SamAccountName)

        $aclold.Access | %{$aclold.RemoveAccessRule($_)} | Out-Null

 

        $ACL=New-Object System.Security.AccessControl.FileSystemAccessRule($($user.SamAccountName), "FullControl","ContainerInherit,ObjectInherit","InheritOnly","Allow")

   $HomeFolderACL.AddAccessRule($ACL)

 

   Set-Acl -Path $Path$($user.SamAccountName) -AclObject $HomeFolderACL

    } 

 

}

This script is working fine, although it is probably possible to shorten the script. What I would like is an output writing which folders were created and which is edited, I simply cannot grasp how to get all the data from my Foreach into a CSV file. I've tried multiple things, but I've given up now, and this seems like the place where I can get my answers.
Best Regards
Jacob

Viewing all articles
Browse latest Browse all 6937

Trending Articles