Solved

powershell convert to string

Posted on 2014-10-03
9
283 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
 
LVL 16

Expert Comment

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

$path = "$shareroot\$DeptPath\$samaccountname"
0
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.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

19 Experts available now in Live!

Get 1:1 Help Now