Problem Printing Variable data to File
-
6 พฤษภาคม 2555 20:14
Hello I hope someone can take the time to answer another Question.
I am trying to do something simple, write the contents of a variable to a file.
What I have done is use a txt file containing UserID’s to query AD and extract the Displayname and email address for each user ID.
I then want to write to a file either a txt file or csv each DisplayName, Email, and UserID per line.
I have tried the following.
{ $objUser = $item.GetDirectoryEntry()
$a = $objUser.displayname + = $objUser.mail + $userID
$a | out-file –filepath “c:\script\outfile.txt”
}
This writes the displayname on one line, the mail on the second line and the user ID on the third line and repeats until the end of file is reached.
When I try using Export-csv
$a | export-csv c:\scripts\outfile.csv –noinfo
The only thing that gets printed is the string length of the variable; this is overwritten in the same field again and again until the EOF is reached.
I have looked at similar questions however all of them involve something more complex then just trying to write the contents of a simple variable.
What am I missing?
Thanks for any help.
ตอบทั้งหมด
-
6 พฤษภาคม 2555 20:18
$objUser = $item.GetDirectoryEntry()
'{0} - {1} - {2} -f $objUser.displayname,$objUser.mail, $userID | out-file –filepath “c:\script\outfile.txt”
¯\_(ツ)_/¯
- แก้ไขโดย jrvMicrosoft Community Contributor 6 พฤษภาคม 2555 20:20
-
6 พฤษภาคม 2555 20:40
JRV thanks for your quick response. Maybe I am not using your solution correclty. When I added your code the only thing that happens is
Powershell sits there waiting for another command.
Here is how I implemented your code
$usernames = Get-Content c:\script\username.txt
foreach($userID in $usernames){
$objADSI = [adsi]""
$domain = $objADSI.distinguishedname
$objDomain = [adsi]("LDAP://" + $domain)
$search = New-Object System.DirectoryServices.DirectorySearcher
$search.SearchRoot = $objDomain
$search.Filter = "(&(objectClass=user)(sAMAccountName=$userID))"
$search.SearchScope = "Subtree"
$results = $search.FindAll()
foreach($item in $results)
{ $objUser = $item.GetDirectoryEntry()
'{0} - {1} - {2} -f $objUser.displayname,$objUser.mail, $userID | out-file –filepath “c:\script\outfile.txt”
} -
6 พฤษภาคม 2555 20:56
You are missing a quote:
'{0} - {1} - {2}' -f $objUser.displayname,$objUser.mail, $userID | out-file –filepath “c:\script\outfile.txt”¯\_(ツ)_/¯
-
6 พฤษภาคม 2555 22:05
Still no joy, I coppied your text directly into mine.
$usernames = Get-Content c:\script\username.txt
foreach($userID in $usernames){
$objADSI = [adsi]""
$domain = $objADSI.distinguishedname
$objDomain = [adsi]("LDAP://" + $domain)
$search = New-Object System.DirectoryServices.DirectorySearcher
$search.SearchRoot = $objDomain
$search.Filter = "(&(objectClass=user)(sAMAccountName=$userID))"
$search.SearchScope = "Subtree"
$results = $search.FindAll()
foreach($item in $results)
{ $objUser = $item.GetDirectoryEntry()
'{0} - {1} - {2}' -f $objUser.displayname,$objUser.mail, $userID | out-file –filepath “c:\script\outfile.txt”
}No file is created and it looks like PS is waiting for another command
-
6 พฤษภาคม 2555 22:53
This is what happens when someone who doesn't know AD or Net attempts to convert VBScritp code to PowerSHell. It just gets worse. Everytime I see code that looks like vbscript with a bunch of dollar signs I know it is not going to be easy. I know this is just code you copied from somewher so you wouldn't know that that woug create subtle issues.
In the future look around the Internet for code examples of PowerShell that are not just regurgitated VBScript. You will find that PowerShell is way easier to use than VBScript and that it does not need all of that extra code.
Here is a pure PowerShell solution that is easy and also very fast and efficient.
$userfile='usernames.txt' $outfile='output.txt' Get-Content $userfile | forEach-Object{ $searcher=[adsisearcher]"(&(objectClass=user)(sAMAccountName=$_))" $searcher.PropertiesToLoad.AddRange(@('samaccountname','displayname','mail')) $user=$searcher.FindOne() '{0} {1} {2}' -f $($user.Properties['displayname'][0]),$($user.Properties['mail'][0]),$($user.Properties['samaccountname'][0]) } | #Out-File $outfile
As long as you do not change the code it will work on any system that is connected to AD.
Untill you run it successfully leave the Out-File bit commented out. Once it is returning what you need then uncomment the Out-File and you are set to go.
¯\_(ツ)_/¯
- ทำเครื่องหมายเป็นคำตอบโดย TheFatman 7 พฤษภาคม 2555 15:13
-
7 พฤษภาคม 2555 15:13
JRV
Thank you so much for your help. You are right I am learning powershell however I pasted together some of the examples from Powershell for Dummies. Your solution works and is a little more elegent then what I got from the book. I will mark this as Answered.
I do have one more question if you can take the time. Its really kind of anomaly more then anything.
I added comma's as seperaters in the output filed. It works for displayname and mail samaccountname is left out of the outputfile.
Here is how I did it.
{0} {1} {2}' -f $($user.Properties['displayname'][0]),",",$($user.Properties['mail'][0]), ",",$($user.Properties['samaccountname'][0])
The output is “FristName LastName , Email address
It should be “FristNamme LastName , Email Address , UserID
Like I said its more of an anomaly.
Again thanks for your help. -
7 พฤษภาคม 2555 15:30
You need to spend a lot of time in the PowerSHell help system. Once you learn how it works it is an invaluable resource.
This one, however, is a bit difficult to explain.
The -f operator is a call to teh Net string formatter. It is common to almost all languages in some form.
<format specifier> -f $var1m$var2m$var3,,,
The format specified uses {} with a number to match the vaiable list. The number chooses the var in the list by order.
Try this:
'{0} - {1}' -f 1,2 '{1} - {0}' -f 1,2 'The number "{0}" is first and this number "{1}" is last' -f 1,2 # magic with numbers 'Decimal:{0} is Hex:0x{0:x}' -f -2147217401 #magic with positions: '{0,20}{1,-20}' -f 'Hello','World' '{0,-20}{1,-20}' -f 'Hello','World' '{0,20}{1,-20}' -f 'Hello','World'
¯\_(ツ)_/¯