jahhan
asked on
Parsing text from a Powershell script
I have created a powershell script that displays a column (ServerDN field) that has the following text:
/o=ABC Group/ou=First Administrative Group/cn=Configuration/cn= Servers/cn =LAXMBGrou p01/cn=Mic rosoft Private MDB
/o=ABC Group/ou=First Administrative Group/cn=Configuration/cn= Servers/cn =LAXMBGrou p02/cn=Mic rosoft Private MDB
/o=ABC Group/ou=First Administrative Group/cn=Configuration/cn= Servers/cn =LAXMBGrou p03/cn=Mic rosoft Private MDB
I would like to minimize this column by only displaying LAXMBGroup01, LAXMBGroup01, LAXMBGroup01.
I am thinking the key is to use a parsing command; however, I am not good with reg expressions. I would really appreciate if someone could help me resolve this issue.
/o=ABC Group/ou=First Administrative Group/cn=Configuration/cn=
/o=ABC Group/ou=First Administrative Group/cn=Configuration/cn=
/o=ABC Group/ou=First Administrative Group/cn=Configuration/cn=
I would like to minimize this column by only displaying LAXMBGroup01, LAXMBGroup01, LAXMBGroup01.
I am thinking the key is to use a parsing command; however, I am not good with reg expressions. I would really appreciate if someone could help me resolve this issue.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
*edit* soostibi's replace is better, I forgot about group matching :) Anyway, like I said, lots of different ways :)
Chris
Chris
Any way you guys can explain the parameters on that replace statement? In particular:
".+?/cn=(\w+)"
I have never used those before nad would like to understand them.
".+?/cn=(\w+)"
I have never used those before nad would like to understand them.
. = Matches any character
+ = Repeats the last character as many times as possible
? = Makes the previous expression optional (so .+ is optional here)
/cn= = Literal text (no special meaning in this context)
( ) = Defines a group
\w = Matches a Word character, a - z, 0 - 9 and _
For better, more comprehensive definitions I recommend you head off to http://www.regular-expressions.info/reference.html.
Chris
+ = Repeats the last character as many times as possible
? = Makes the previous expression optional (so .+ is optional here)
/cn= = Literal text (no special meaning in this context)
( ) = Defines a group
\w = Matches a Word character, a - z, 0 - 9 and _
For better, more comprehensive definitions I recommend you head off to http://www.regular-expressions.info/reference.html.
Chris
ASKER
There are other servers that do not start with LAX. Will these scripts work for those as well? Do I have to apply these scripts to a where-object cmdlet?
Yes, it finds any servename without special characters (for example dash).
You just have to put in a ForEach-Object the expression:
yourexpressionthatpriduces thestrings | foreach-object {
$_ -replace ".+?/cn=(\w+)/cn=Microsoft \sPrivate\ sMDB",'$1'
}
You just have to put in a ForEach-Object the expression:
yourexpressionthatpriduces
$_ -replace ".+?/cn=(\w+)/cn=Microsoft
}
ASKER
OK,
So here is my script.
$Date = get-date -uformat "%Y%m%d"
$outFile = "c:\scripts\get-BESAccount s_$date.cs v"
$SQLCommand = "SELECT
[ServerConfig].[ServiceNam e]
,[UserConfig].[DisplayName ]
,[userconfig].[MailboxSMTP Addr] as [SMTPAddress]
,[SyncDeviceMgmtSummary].[ ModelName]
,[SyncDeviceMgmtSummary].[ PhoneNumbe r]
,[SyncDeviceMgmtSummary].[ PlatformVe r]
,[SyncDeviceMgmtSummary].[ IMEI]
,[SyncDeviceMgmtSummary].[ ICCID]
,[SyncDeviceMgmtSummary].[ HomeNetwor k]
,[SyncDeviceMgmtSummary].[ AppsVer]
,[UserConfig].[PIN]
,[ITPolicy2].[PolicyName]
,[UserConfig].[ServerDN] as [ExchangeServer]
,[UserConfig].[AgentId]
,[UserConfig].[RoutingInfo ]
,[UserStats].[MsgsPending]
,[UserStats].[LastFwdTime]
,[UserStats].[LastSentTime ]
,[UserConfig].[CreationTim e]
FROM [BESPOOL].[dbo].[UserConfi g]
INNER JOIN [BESPOOL].[dbo].[UserStats ]
ON [UserConfig].[Id]=[UserSta ts].[UserC onfigId]
INNER JOIN [BESPOOL].[dbo].[ITPolicy2 ]
ON [UserConfig].[ITPolicy2Id] =[ITPolicy 2].[Id]
INNER JOIN [BESPOOL].[dbo].[ServerCon fig]
ON [UserConfig].[ServerConfig Id]=[Serve rConfig].[ Id]
INNER JOIN [BESPOOL].[dbo].[SyncDevic eMgmtSumma ry]
ON [UserConfig].[Id]=[SyncDev iceMgmtSum mary].[Use rConfigId] "
$Connection = New-Object System.Data.SQLClient.SQLC onnection
$Connection.ConnectionStri ng ="server=BESSQL01;database =BESPOOL;t rusted_con nection=tr ue;"
$Command = New-Object System.Data.SQLClient.SQLC ommand
$Command.CommandType = [System.Data.CommandType]" Text"
$Command.Connection = $Connection
$Command.CommandText = $SQLCommand
$dt_BESUsers = New-Object "System.Data.DataTable"
$Connection.Open()
$Reader = $Command.ExecuteReader()
$dt_BESUsers.Load($Reader)
$Connection.Close()
$dt_BESUsers `
| foreach-object {
$_ -replace ".+?/cn=(\w+)/cn=Microsoft \sPrivate\ sMDB",'$1'
} `
|select displayname, servicename, exchangeserver, modelname, phonenumber,platformver,im ei,iccid,a ppsver,pin `
|sort 'ModelName' #| Export-CSV $OUTFILE -NoTypeInformation
When the script finish running it does not display any text.
So here is my script.
$Date = get-date -uformat "%Y%m%d"
$outFile = "c:\scripts\get-BESAccount
$SQLCommand = "SELECT
[ServerConfig].[ServiceNam
,[UserConfig].[DisplayName
,[userconfig].[MailboxSMTP
,[SyncDeviceMgmtSummary].[
,[SyncDeviceMgmtSummary].[
,[SyncDeviceMgmtSummary].[
,[SyncDeviceMgmtSummary].[
,[SyncDeviceMgmtSummary].[
,[SyncDeviceMgmtSummary].[
,[SyncDeviceMgmtSummary].[
,[UserConfig].[PIN]
,[ITPolicy2].[PolicyName]
,[UserConfig].[ServerDN] as [ExchangeServer]
,[UserConfig].[AgentId]
,[UserConfig].[RoutingInfo
,[UserStats].[MsgsPending]
,[UserStats].[LastFwdTime]
,[UserStats].[LastSentTime
,[UserConfig].[CreationTim
FROM [BESPOOL].[dbo].[UserConfi
INNER JOIN [BESPOOL].[dbo].[UserStats
ON [UserConfig].[Id]=[UserSta
INNER JOIN [BESPOOL].[dbo].[ITPolicy2
ON [UserConfig].[ITPolicy2Id]
INNER JOIN [BESPOOL].[dbo].[ServerCon
ON [UserConfig].[ServerConfig
INNER JOIN [BESPOOL].[dbo].[SyncDevic
ON [UserConfig].[Id]=[SyncDev
$Connection = New-Object System.Data.SQLClient.SQLC
$Connection.ConnectionStri
$Command = New-Object System.Data.SQLClient.SQLC
$Command.CommandType = [System.Data.CommandType]"
$Command.Connection = $Connection
$Command.CommandText = $SQLCommand
$dt_BESUsers = New-Object "System.Data.DataTable"
$Connection.Open()
$Reader = $Command.ExecuteReader()
$dt_BESUsers.Load($Reader)
$Connection.Close()
$dt_BESUsers `
| foreach-object {
$_ -replace ".+?/cn=(\w+)/cn=Microsoft
} `
|select displayname, servicename, exchangeserver, modelname, phonenumber,platformver,im
|sort 'ModelName' #| Export-CSV $OUTFILE -NoTypeInformation
When the script finish running it does not display any text.
The first of mine needs extra treatment for special characters. The second two don't care about the server name, but may need adjustment if the full string contains other special characters. None of them are bothered about the content of the server name beyond that, they will pull any server, not just those beginning LAX.
Chris
Chris
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the quick response Chris. Yes, it's the exchangeserver field I am trying to manipulate.
Cool, it should work. I used Soostibi's replace, yell if something doesn't work and one or both of us will endeavour to fix it :)
Chris
Chris
Thanks for the explanation and link Chris.
ASKER
Chris,
Thanks for the script modifcation that was the fix.
Soostibi,
Thanks for helping out giving an alternative cmdlet.
Thanks for the script modifcation that was the fix.
Soostibi,
Thanks for helping out giving an alternative cmdlet.
This regular expression match:
'\w*(?=/cn=(\w*\s?)*$)'
e.g.
Open in new window
Or this replace:'^(/(ou?|cn)=[\w\s]*){4}/c
e.g.
Open in new window
Or this split:'/cn='
e.g.
Open in new window
Or... well the list goes on, pick the one you find least upsetting?Chris