Hello Scripting Guys,
I'm completely new to scripting and reach out to the experts for assistance. I have been tasked to come up with a Powershell script that will query Active Directory for any new user that has been created (we use an application to create new users that sets a homedirectory as \\server\%username%$ with the actual path of \\server\h\users\%username%) within the last 5 minutes and create 2 child directories under the users root share. i.e. \\server\%username%$\folder1 and \\server\%username%$\folder2. The application we use to create user accounts also has the ability to attach a script to run when the user account is created. I'm trying to create a script and attach it to the application.
After scouring the net for a week I have come up with something that looks as though it may work (from my extremely limited experience) but, nonetheless, does not.
$fiveminutes = (Get-Date).AddMinutes(-5) $newuser = Get-ADUser -Filter * -Properties * | where { $_.whenCreated -ge $fiveminutes } | select homeDirectory $folders = gci $newuser | Where-Object{($_.PSIsContainer)} | foreach-object{$_.Name} ForEach($fldr in $folders){ New-Item -ItemType Directory -Path "\\$newuser\$fldr\Folder1"} ForEach($fldr in $folders){ New-Item -ItemType Directory -Path "\\$newuser\$fldr\Folder2"}
Basically, I'm trying to query A.D. for any new user that was created in the last 5 minutes, grab the homeDirectory location and create the 2 new folders within it. As you can probably tell, this isn't working out too well. When I break it down it does complete the search and pull new users and display their homeDirectory but Powershell errors out in line 7 when trying to create the folders.
New-Item : The path is not of a legal form. At C:\QueryADCreateFolders.ps1:7 char:37+ ForEach($fldr in $folders){ New-Item <<<< -ItemType Directory -Path "\\$newu ser\$fldr\Folder1"}+ CategoryInfo : InvalidArgument: (\\\Intel\Folder1:String) [New- Item], ArgumentException+ FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShel l.Commands.NewItemCommand
I've come across many forums where people would recommend Group Policy to create these folders. Unfortunately, we need these child folders created before the user logs in. (Due to another application that requires these folders be created, which checks for them upon login) Oh and the permissions need to be inhereted from the parent folder (\\server\%username%$).
I'm open to any suggestions. Thank you!