Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Trouble Piping Commands

$
0
0

I am working on a PS Script to add users to AD, set their home directory, create the home directory and set the permissions, add them to their appropriate group in AD, add to OnPremise Exchange, and MSOnline services, from a CSV file. My issue seems to be that it just doesn't execute everything past the initial add to AD. It will create the users, but won't add them to the groups, set the home directory, create the directory, etc... Oddly enough, I don't get any error messages except for the MSOnline services which tells me it doesn't take pipelined input. The only other error message I get is when it tries to set the permissions on the folders. Since the folders didn't get created, there's nothing to set permissions on. The script is below. Any help would be appreciated. 

$Users = import-csv newusers.csv
ForEach ( $record in (Import-Csv newusers.csv)) {
$username = $record.SamAccountName
$DisplayName = $record.DisplayName
$Name = $record.Name
$Surname = $record.Surname
$GivenName = $record.GivenName
$OUPath = $record.OUPath
$Description = $record.Description
$Office = $record.Office
$password = $record.password
$ADGroup = $record.ADGroup
$MailDB = $record.MailDB
}
$HomeDrive = 'H:'
$UserRoot = '\\myserver\myshare\'
$HomeDirectory = $Userroot+$UserName
$Domain = 'mydomain'
$AccountSkuId = "mydomain:STANDARDWOFFPACK_FACULTY"
$UsageLocation = "US"
$LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://exch2013.mydomain.com/powershell
$O365Cred = Get-Credential

Import-Module ActiveDirectory
Import-PSSession $session
Connect-MsolService –Credential $O365Cred

$Users | ForEach-Object {
New-ADUser -SamAccountName $username -DisplayName $displayname -Name $Name -Surname $Surname -GivenName $GivenName -Path $OUPath -CannotChangePassword $true -PasswordNeverExpires $true -Description $Description -Office $Office -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -Enabled $true |
Set-ADUser -Identity $username -HomeDrive $HomeDrive -HomeDirectory $HomeDirectory |
Add-ADGroupMember -Identity $ADGroup -Members $username |
New-Item -Path $UserRoot -Name $username -ItemType directory -Force | 
cscript xcacls.vbs $HomeDirectory /E /G `"$domain\$username`":F |
Enable-Mailbox -Identity $username -Alias $username -Database $MailDB |
Set-CASMailbox -Identity $username -OWAMailboxPolicy OWAPOL
New-MsolUser -UserprincipalName $username@mydomain.com -Firstname $GivenName -LastName $Surname -DisplayName $DisplayName -Department $Description -Office $Office -UsageLocation $UsageLocation -PasswordNeverExpires $True -StrongPasswordRequired $False -LicenseAssignment $AccountSkuId | 
Set-MsolUserPassword -UserPrincipalName $UserName@mydomain.com -NewPassword $Password -ForceChangePassword $False |
}

If I execute each set of commands individually, importing the CSV at each new cmdlet, then it works but it seems inefficient to keep importing the CSV for each command.

Again, any help would be appreciated.



Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>