Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell convert to string

Posted on 2014-10-03
9
Medium Priority
?
450 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 71

Accepted Solution

by:
Qlemo earned 2000 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

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Loops Section Overview
Screencast - Getting to Know the Pipeline

876 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