passing batch variables to embedded EMS Powershell
Hi All, quick query, I have been working on a bulk import script for new user accounts, I am fairly familiar with batch scripting however not so much with powershell. here is what I have so far: the layout of the users.csv file is first,last,dept,title for /f "Tokens=1,2,3,4 Delims=," %%a in (users.csv) do ( dsadd user "cn=%%a.%%b,ou=%%c,OU=Employees,OU=Users,DC=company,DC=local" -samid %%a.%%b -upn %%a.%%b@company.co.uk -fn %%a -ln %%b -display "%%a %%b" -profile \\fas1\Profiles\%%a.%%b -pwd "password" -canchpwd yes -pwdneverexpires no -desc %%c -office %%c -dept %%c -company "company name" -title "%%d" Add Groups... Add Profile/hmdir permissions... ) I then have the following lines outside of the original script echo Creating mailbox echo First,Last,Dept>mailbox.csv type users.csv>>mailbox.csv PowerShell.exe -command ". 'c:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\scripts\enable_mbx.ps1" at this point it runs import-csv c:\scripts\mailbox.csv | ForEach-Object { Enable-Mailbox -Identity 'company.local/Users/Employees/$_.Dept/$_.First.$_.Last' -Alias '$_.First.$_.Last' -Database 'Users'} the mailbox.CSV example is First,Last,Dept,title John,Smith,Sales,Sales Advisor My issue is that it does not seem to want to use the variables from the CSV file and outputs with the following error: The operation couldn't be performed because object 'company.local/Users/Employees/$_.Dept/$_. First.$_.Last' couldn't be found on 'DC.company.local'. + CategoryInfo : NotSpecified: (0:Int32) [Enable-Mailbox], ManagementObjectNotFoundException + FullyQualifiedErrorId : 994600C8,Microsoft.Exchange.Management.RecipientTasks.EnableMailbox I have a feeling that I just dont have the correct syntax but after a few hours researching I thought I should ask... any help would be greatly appriciated. Marc
December 12th, 2010 7:37pm

You've got a couple of things going wrong there. First, you single quoted the strings, which suppresses variable expansion. Use double quotes if you want the variables to expand. C:\testfiles> gci *.txt |% {'$_.basename.$_.extension'} $_.basename.$_.extension $_.basename.$_.extension $_.basename.$_.extension Change to double quotes, and: PS C:\testfiles> gci *.txt |% {"$_.basename.$_.extension"} C:\testfiles\logs.txt.basename.C:\testfiles\logs.txt.extension C:\testfiles\result.txt.basename.C:\testfiles\result.txt.extension C:\testfiles\test1.txt.basename.C:\testfiles\test1.txt.extension Better, but now we find the second problem - using variable properties in a string. The parser interpets $_.property as the string value of the current object in the pipeline, with ".property" tacked onto it. To tell the parser that's not what you want, you can use a sub-expression: PS C:\testfiles> gci *.txt |% {"$($_.basename).$($_.extension)"} logs..txt result..txt test1..txt (The second dot is part of the extension string) Hope that makes sense.[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Free Windows Admin Tool Kit Click here and download it now
December 12th, 2010 8:41pm

Cheers that worked when i eventually got the syntax correct, thought it was something to do with that. Did the following: import-csv c:\scripts\mailbox.csv | ForEach-Object { Enable-Mailbox -Identity "company.local/Users/Employees/$($_.Dept)/$($_.First).$($_.Last)" -Alias "$($_.First).$($_.Last)" -Database 'Users'}Marc Cooper
December 14th, 2010 7:33am

Hi, Thanks for sharing. AllenAllen Song
Free Windows Admin Tool Kit Click here and download it now
January 7th, 2011 1:14am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics