Solved

Powershell Replace

Posted on 2013-02-01
7
181 Views
Last Modified: 2013-04-02
I'm trying to pull back some information on logfiles from a server.  The Parent is being pulled back with brackets, [ & ], that I want to remove.  What is the best way of doing this.  I thought this might work but it doesn't.  

ls \SQL\LOCALHOST\DEFAULT\Databases\ | ForEach-Object {$_.LogFiles} | SELECT @{name="Parent";expression={$_.Parent.Replace("[","").Replace("]","")}}

Open in new window


This just returns a bunch of blank values.
0
Comment
Question by:tim_cs
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38844730
I think the problem was with closing brakets for Foreach loop. Try this now.

ls \SQL\LOCALHOST\DEFAULT\Databases\ | ForEach-Object {$_.LogFiles | SELECT @{name="Parent";expression={$_.Parent.Replace("[","").Replace("]","")}}
0
 
LVL 15

Author Comment

by:tim_cs
ID: 38844750
Nope, same result, just a bunch of blank lines.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 38844758
FileSystem objects do not have a property of LogFiles, and PS defaults to not display any message, but continue with empty values. Further you should show the filename itself.
get-childitem \SQL\LOCALHOST\DEFAULT\Databases\*.log |
   select @{name="Parent"; expression={$_.DirectoryName.Replace("[","").Replace("]","")}}, Name

Open in new window

0
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

 
LVL 15

Author Comment

by:tim_cs
ID: 38844813
FileSystem objects very well might not contain a LogFiles property but database servers do which is what I'm using.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 38844940
Ooops. But I don't get what you want to get here, the parent of a SQL Transaction Log is the DB?!
0
 
LVL 15

Author Comment

by:tim_cs
ID: 38845185
Yes, if I run this.
ls \SQL\LOCALHOST\DEFAULT\Databases\ | ForEach-Object {$_.LogFiles} | SELECT PARENT

Open in new window

Then I get back
[DBName1]
[DBName2]
[Testdb]

I need to be able to pull that back without the [ ].  I'm pulling back other values as well but to simpify the question I removed those columns.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 38878223
Parent is a Microsoft.SqlServer.Management.Smo.Database Parent object, and cannot be  treated (directly) like a string. But if you use
dir SQLServer:\SQL\LOCALHOST\DEFAULT\Databases\ | 
 SELECT @{name="Parent";expression={$_.LogFiles.Parent -replace "\[|\]" }}

Open in new window

you'll get the stripped DB name. Note that I used the (regex) replace operator instead of the Replace method.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

9 Experts available now in Live!

Get 1:1 Help Now