Jump to content

Need Help With PowerShell - Scripting....

Hey Guys, its me again

 

I need help with some PowerShell scripting...

my current PS Script looks like that:

echo ""
echo "If this Message is seen, the Event Button.Click worked" 

$Cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://[*Remoteconnection to my Server*]/PowerShell/ -Authentication Kerberos -Credential $Cred
Import-PSSession $Session

get-messagetrackinglog -Start "2-3-2020" -End "2-6-2020" -MessageSubject "Bills" -sender: "sender1@anonymous.com" | Select messagesubject,recipients,eventid | Out-File 'C:\Temp\MessageLogs\LogSender1.txt'
get-messagetrackinglog -Start "2-3-2020" -End "2-6-2020" -MessageSubject "Bills" -sender: "sender2@anonymous.com" | Select messagesubject,recipients,eventid | Out-File 'C:\Temp\MessageLogs\LogSender2.txt'
get-messagetrackinglog -Start "2-3-2020" -End "2-6-2020" -MessageSubject "Bills" -sender: "sender3@anonymous.com" | Select messagesubject,recipients,eventid | Out-File 'C:\Temp\MessageLogs\LogSender3.txt'
            
echo "This script was executed on Windows Server 2012 R2 - MS Exchange 2013."
echo ""
Pause

 

It creates a Log file that looks like this:

 

2020-02-06_11h45_00.thumb.png.1633577ee3b183188387aaa597e2ac72.png

 

It shows the entered attributes "MessageSubject", "Recipients" and "EventID" and the connect data-value for each attribute.

And my question now is, wether or not it is possible, to let PowerShell divide the "MessageSubject-Value" into 2 Substrings (like 1. "Company: Bill" and 2. "0133456789") so I can easily, after the Import into MS Excel, devide the data-value of MessageSubject into 2 Cells.

 

I need that, because in my VBA script, the Cells, where only the Billnumber is located in, is used for upcomming calculations and controlstructures like  IF-ELSE or switch-case structures.

 

Does anyone has an idea on how to solve my Problem?

R9 3900X | Aorus X570 Elite | 32GB 3600MHz RAM | RTX 2060 Super | Aquafusion 360mm | 2TB NVMe x4 Gen. 4.0 

Link to comment
Share on other sites

Link to post
Share on other sites

You could use a regex.

 

You already know that the "MessageSubject" field is made of one or more strings followed by a 10 digit number; you can match the string like this

"Company: Bill 0123456789" -match "(.+)\s(\d{10})"

and then access the values using the automatic $Matches variable:

PS C:\Users\Sauron> $Matches.1
Company: Bill
PS C:\Users\Sauron> $Matches.2
0123456789

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

36 minutes ago, Sauron said:

You could use a regex.

 

You already know that the "MessageSubject" field is made of one or more strings followed by a 10 digit number; you can match the string like this


"Company: Bill 0123456789" -match "(.+)\s(\d{10})"

 

Hm thats the Problem ... in this case the Billnumber is a digit number ... but it can also be a 8 or 15 digit number...

can i use the -match attribute like this:

 

                      -match "(.+\s(\d{$variable})" ?

 

so that the length of the Billnumber is a variable?

 

Greetings and thanks for the fast anwser

R9 3900X | Aorus X570 Elite | 32GB 3600MHz RAM | RTX 2060 Super | Aquafusion 360mm | 2TB NVMe x4 Gen. 4.0 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Iqu said:

Hm thats the Problem ... in this case the Billnumber is a digit number ... but it can also be a 8 or 15 digit number...

can i use the -match attribute like this:

 

                      -match "(.+\s(\d{$variable})" ?

 

so that the length of the Billnumber is a variable?

 

Greetings and thanks for the fast anwser

this should work:

-match "(.+)\s(\d+)"

it will just match any number so long as it's the last thing in the string.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

and for what exactly does the (.+) and the \s(\d+) stand? 

sry im new with powershell and aint knowing too much so far

R9 3900X | Aorus X570 Elite | 32GB 3600MHz RAM | RTX 2060 Super | Aquafusion 360mm | 2TB NVMe x4 Gen. 4.0 

Link to comment
Share on other sites

Link to post
Share on other sites

Are you aware you have these logs and filter already implemented on the exchange server with a nice interface that has an export to CSV which excel open very nicely.

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, Franck said:

Are you aware you have these logs and filter already implemented on the exchange server with a nice interface that has an export to CSV which excel open very nicely.

yeah i know .... i can just change the command to 

 

get-messagetrackinglog -Start "2-3-2020" -End "2-6-2020" -MessageSubject "Bills" -sender: "sender1@anonymous.com" | Select messagesubject,recipients,eventid | Out-File 'C:\Temp\MessageLogs\LogSender1.csv'

 

and I´m good to go...

 

But it doesn´t really matter, if I have my Logs as .txt files or as .csv files... cause i need to import them either way into another excel sheet, which is continueing with some calulations of several other Billnumbers.

 

But I also was thinking about doin the "splitting part" in VBA cause that seems a lot easier for me than with powershell, since I´m not too comftable in the handling with PowerShell.

Because in VBA you already have the "split" function which u can use to divide a long string into several shorter ones.

 

But still thx for the advice :D

R9 3900X | Aorus X570 Elite | 32GB 3600MHz RAM | RTX 2060 Super | Aquafusion 360mm | 2TB NVMe x4 Gen. 4.0 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×