Solved

powershell convert to string

Posted on 2014-10-03
9
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 70

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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