Solved

powershell convert to string

Posted on 2014-10-03
9
264 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 28

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
 
LVL 16

Expert Comment

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

$path = "$shareroot\$DeptPath\$samaccountname"
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 28

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 28

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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now