Solved

powershell convert to string

Posted on 2014-10-03
9
304 Views
Last Modified: 2014-10-09
hi guys,

$user = get-qaduser $_

$DepartmentName = $user.Department

$samaccountname = $user | select samaccountname

$samaccountname is a pscustomobject and i this to be in a string so i can:

$path="$shareroot" + "\" + "$DeptPath" + "\" + "$samaccountname"
test-path $path

please help!
0
Comment
Question by:Kay
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40359529
$samaccountname = $user | select -expa samaccountname
0
 

Author Comment

by:Kay
ID: 40359620
i have created the command

echo "$user" $path > c:\users\kellyg\testpathusers.txt

how do i get it to add all users to this text file?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40360008
Does adding append help?

echo "$user" $path > c:\users\kellyg\testpathusers.txt -append
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40360017
Also for the path you can just use

$path = "$shareroot\$DeptPath\$samaccountname"
0
 
LVL 29

Expert Comment

by:becraig
ID: 40360051
$user $path | outfile  c:\users\kellyg\testpathusers.txt  -append


or
echo "$user" $path >> c:\users\kellyg\testpathusers.txt

> creates a new file

>> appends
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40360060
outfile should also have a dash

$user $path | Out-File  c:\users\kellyg\testpathusers.txt  -append
0
 
LVL 29

Expert Comment

by:becraig
ID: 40360064
I can't help but think there is a lot more that can be done to clean up your process.

The script below is based on the assumption you want to test for users who have the path properly defined:
$results = @()
$shareroot = ""
$DeptPath = ""

gc userlist.txt | %  {
$user = get-qaduser $_
$DepartmentName = $user.Department
$samaccountname = $user | select -expa samaccountname
$path="$shareroot" + "\" + "$DeptPath" + "\" + "$samaccountname"
if (test-path $path) {$pathval = "TRUE"} else {$pathval = "FALSE"}
$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'USER' -Value $samaccountname
$item | Add-Member -type NoteProperty -Name 'PATH' -Value $pathval
$results += $item
}
$results | export-csv file.csv -nti

Open in new window

0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40361870
This script contains much stuff which is inconsistent or superfluous in its application, and I feel the need to stop that misconceptions as early as possible.

You never abbreviate -ExpandProperty as -Expa. Use -Expand or the full name.

You either use
$path= $shareroot + "\" + $DeptPath + "\" + $samaccountname

Open in new window

or
$path="$shareroot\$DeptPath\$samaccountname"

Open in new window


In a pipeline you should refrain from collecting results in vars, just to dump them out after the pipeline has been processed. I understand the usage for $DepartmentName and such, but $results is too much. Always keep in mind that PowerShell handles rich objects, which consume significant amount of memory, so you should get rid of storing bigger amounts of them.
The code of http:#a40360064 should hence be:
$shareroot = ""
$DeptPath = ""

gc userlist.txt | %  {
  $user = get-qaduser $_
  $DepartmentName = $user.Department
  $samaccountname = $user | select -expa samaccountname
  $path="$shareroot\$DeptPath\$samaccountname"
  if (test-path $path) {$exists = "TRUE"} else {$exists = "FALSE"}
  New-Object PSObject |
    Add-Member -PassThru -type NoteProperty -Name 'User' -Value $samaccountname |
    Add-Member -PassThru -type NoteProperty -Name 'Path' -Value $path |
    Add-Member -PassThru -type NoteProperty -Name 'Exists' -Value $exists
} | export-csv file.csv -nti

Open in new window

Please also note the indention, which is an important part for more complex scripts.
I've also added the path itself, as I guess it should be part of the output.

The script in its intention seems to be correct. The output file though is comma-separated with double quotes surrounding each entry. If the output should give something easily readable, my preference would be to use something along
} | % { write-output "$user`t$path`t$exists" } | out-file c:\users\kellyg\testpathusers.txt

Open in new window

to generate a tab-separated file without the double quotes.

On another note, I prefer the usage of
New-Object PSObject -Property @{
  User = $samaccountname
  Path = $path
  Exists = $exists
}

Open in new window

over using Add-Member, because it is much shorter and obvious.
0
 

Author Closing Comment

by:Kay
ID: 40370606
Fantastic!! Thank you soo much!
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question