Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, July 2, 2014 7:57 AM
Hi,
I am new at this sorry and looking for some help. I have put together a script using New-OSCEXOAppointment to create exchange appointments via powershell and using it to integrate with one of our products. I need to execute the script via a bat file.
The command is:
New-OSCEXOAppointment -Identity $Identity -Subject $Subject -Body $Body -EndDate $EndDate -Location $Location -StartDate $StartDate -RequiredAttendees $RequiredAttendees -BusyStatus $BusyStatus -Category $Category
So the BAT file runs (for example):
powershell.exe -ExecutionPolicy Bypass -File c:\calendarscripts\CreateCalendarItem.ps1 -Identity "ben" -subject "test subject" -Body "Test body text:" -EndDate "2014-07-03T17:00:00" -Location "test location" -StartDate "2014-07-03T09:00:00" -RequiredAttendees "ben" -BusyStatus "oof" -Category "Green Category"
This runs successfully. Unfortunately for the -Body parameter, i need to pass through multi line text like below:
Customer Name
Status
Test Body Text
Is it possible to pass this sort of text through as a parameter?
I hope that makes sense?
Many thanks
Ben
All replies (29)
Wednesday, July 2, 2014 2:44 PM ✅Answered
You'll need to convert the object that Get-Content returns into a string if you want to use this method:
$body = Get-Content c:\CalendarScripts\bodytext.txt | Out-String
It's a bit on confusing side, but you'll get the hang of it. =]
Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)
Wednesday, July 2, 2014 2:59 PM ✅Answered
I can suggest calling parameters from XML is also another option
Can you try this once please - Appending Mike's Approach
$paramater = @{
Identity = 'service.desk';
Subject = 'test1234';
Body = Get-Content C:\Temp\File.txt | Out-String
Location = 'location';
EndDate = '2014-07-01T17:00:00';
StartDate = '2014-07-01T09:00:00';
RequiredAttendees = '[email protected]'
BusyStatus = 'oof';
Category = 'Green Category';
}
New-OSCEXOAppointment @paramater
I am testing with XML now
Regards Chen V [MCTS SharePoint 2010]
Wednesday, July 2, 2014 8:08 AM
Have a try with .... -Body "Test body text:`n" .....
Wednesday, July 2, 2014 10:45 AM
unfortunately anything between the double quotes " is returned as txt. So I get the `n returned as text and is ignored
Thanks for the response.
Cheers
Ben
Wednesday, July 2, 2014 11:19 AM
Have a look at: PowerShell.exe Command-Line Help
Specifically the -EncodedCommand parameter. It might help you out a bit.
Wednesday, July 2, 2014 11:47 AM
Assuming you looking for BODY content in appointments.
May try this. I am not 100 % sure because I don't have an environment to test and this is not PowerShell command looks like some custom made one.
$paramater = @{
Identity = $Identity;
Subject = $Subject;
Body = "Customer Name
Status
Test Body Text";
Location = $Location;
StartDate = $StartDate;
RequiredAttendees = $RequiredAttendees;
BusyStatus = $BusyStatus;
Category = $Category;
}
New-OSCEXOAppointment @paramater
This will make Body as multiple lines.
Regards Chen V [MCTS SharePoint 2010]
Wednesday, July 2, 2014 11:59 AM
I think the BODY parameter is just whole bunch of Text. It needs to be in Meeting Request body. If you say Customer Name, Status etc.., varies then my code will not be much helpful .
I tested using Send-Mail Message. and Created a new batch file got desired output.
Let me hear from you.
Regards Chen V [MCTS SharePoint 2010]
Wednesday, July 2, 2014 12:46 PM
Thanks Chen. The text in the body is dynamic but your code may just solve my problem. I can actually produce the powershell script on the fly from our product. ie. as it runs it will produce the script first and then execute. So I can actually pass the values into the script first as per your suggestion above. Its an extra step but it solves my problem. Ill test and let you know. Thank you!
Ben
Wednesday, July 2, 2014 12:51 PM
Glad to know that it helped.
Regards Chen V [MCTS SharePoint 2010]
Wednesday, July 2, 2014 1:06 PM
Hi Chen,
I tried the following:
$paramater = @{
Identity = 'service.desk';
Subject = 'test1234';
Body = 'example customer
example line 2 text
example line 3 text';
Location = 'location';
EndDate = '2014-07-01T17:00:00';
StartDate = '2014-07-01T09:00:00';
RequiredAttendees = '[email protected]'
BusyStatus = 'oof';
Category = 'Green Category';
}
New-OSCEXOAppointment @paramater
It runs but unfortunately it ignores the line breaks. That is running it from powershell. I think you are onto it though and ill see if i can insert characters like `n
Cheers
Ben
Wednesday, July 2, 2014 1:13 PM
Hi Ben,
You can try using a here-string for the body:
$body = @"
Customer Name
Status
Other body text
So on and so forth
"@
Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)
Wednesday, July 2, 2014 1:26 PM
running powershell from command line and dealing with the quotations is a pain, but here is an example of how to do what you are wanting:
powershell -command """test`ntest"""
Please note that it is surrounded on both sides by 3 sets of double quotes.
Wednesday, July 2, 2014 1:34 PM
Worth trying here strings
$paramater = @{
Identity = 'service.desk';
Subject = 'test1234';
$Body = @'
Line 1
Line 2
Line 3
'@
Location = 'location';
EndDate = '2014-07-01T17:00:00';
StartDate = '2014-07-01T09:00:00';
RequiredAttendees = '[email protected]'
BusyStatus = 'oof';
Category = 'Green Category';
}
New-OSCEXOAppointment @paramater
Regards Chen V [MCTS SharePoint 2010]
Wednesday, July 2, 2014 1:35 PM
Another option:
$paramater = @{
Identity = 'service.desk';
Subject = 'test1234';
Body = @('example customer',
'example line 2 text',
'example line 3 text') -join "`n"
Location = 'location';
EndDate = '2014-07-01T17:00:00';
StartDate = '2014-07-01T09:00:00';
RequiredAttendees = '[email protected]'
BusyStatus = 'oof';
Category = 'Green Category';
}
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Wednesday, July 2, 2014 1:48 PM
Thanks. The problem seems I am calling the script found here http://gallery.technet.microsoft.com/office/Create-Appointments-for-779b55ad
This contains the following code. I am guessing my problem is my script ignores what I do with $body as its set below:
#Create a new appiontment
$newAppointment = New-Object Microsoft.Exchange.WebServices.Data.Appointment($exService)
$newAppointment.Subject = $Subject
$newAppointment.Location = $Location
$newAppointment.Body = $Body
$newAppointment.Start = $StartDate
$newAppointment.End = $EndDate
$newAppointment.RequiredAttendees.Add($RequiredAttendees)
$newAppointment.LegacyFreeBusyStatus = $BusyStatus
$newAppointment.Categories.Add($Category)
Cheers
Ben
Wednesday, July 2, 2014 1:51 PM
Thanks. The problem seems I am calling the script found here http://gallery.technet.microsoft.com/office/Create-Appointments-for-779b55ad
This contains the following code. I am guessing my problem is my script ignores what I do with $body as its set below:
Based on what I can see there, you can pass in your here-string $body with the -Body parameter and it should work.
$body = @"
Body text
More text
Even more text
"@
New-OSCEXOAppointment -Identity johnd -Subject "Company Event" -Location "Meeting Room" -Body $body
Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)
Wednesday, July 2, 2014 2:10 PM | 1 vote
I don't think he can use the here string since he is calling this from a .bat file so it needs to able to be passed through as a parameter from inside the .bat. I tested and calling the script from a powershell session and using `n breaks the line, but doing it from command line does not it just passes it as literal.
Wednesday, July 2, 2014 2:17 PM | 1 vote
I don't think he can use the here string since he is calling this from a .bat file so it needs to able to be passed through as a parameter from inside the .bat. I tested and calling the script from a powershell session and using `n breaks the line, but doing it from command line does not it just passes it as literal.
Ah, I missed that part.
In that case, I might just put the body text into a standalone text file and then read it in via Get-Content instead. That way the batch file never needs to change to update the message, just the text file.
Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)
Wednesday, July 2, 2014 2:17 PM
Thanks. Based on what Chen mentioned above, I have dropped using the BAT file as I can generate the script on the fly. Same end result, different approach.
I have tried using the@" but not luck. Its hard to explain as I am a newbie but my script calls:
Import-Module C:\CalendarScripts\NewOSCEXOAppointment\NewOSCEXOAppointment1.psm1
The NewOSCEXOAppointment1.psm1 module has the paramaters as well, so $body is $newAppointment.Body = $Body
So I think I need to change this script and not mine but that is beyond me. Not sure how I can allow the $newAppointment.Body = $Body to have multiple lines.
How did you pass through the `n Noah?
Cheers
Ben
Wednesday, July 2, 2014 2:21 PM
In that case, I might just put the body text into a standalone text file and then read it in via Get-Content instead. That way the batch file never needs to change to update the message, just the text file.
Good idea...I was thinking like a regex match inside the script to split the $body parameter, but that would probably be better.
Wednesday, July 2, 2014 2:28 PM
Thanks Mike. Can you give me an example of how I would do this? It sounds like a solid plan :-)
Thanks
Ben
Wednesday, July 2, 2014 2:31 PM
if you are calling the script from powershell rather than command prompt, have you tried Stuart's suggestion above which was:
**-Body "Test body text:`n" **
unless the script is doing weird things to the formatting it should cause newlines at "`n"
although I would have thought the here string method would work also when performed through ise.
Wednesday, July 2, 2014 2:39 PM
Thanks Noah. I just get the `n as text. I tried mikes suggestion using a file, and assuming I am doing it right, I get the following error:
Exception setting "Body": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type
"Microsoft.Exchange.WebServices.Data.MessageBody"."
So in the C:\CalendarScripts\NewOSCEXOAppointment\NewOSCEXOAppointment1.psm1 script, I added:
$body = Get-Content c:\CalendarScripts\bodytext.txt
#Create a new appiontment
$newAppointment = New-Object Microsoft.Exchange.WebServices.Data.Appointment($exService)
$newAppointment.Subject = $Subject
$newAppointment.Location = $Location
$newAppointment.Body = $Body
$newAppointment.Start = $StartDate
$newAppointment.End = $EndDate
$newAppointment.RequiredAttendees.Add($RequiredAttendees)
$newAppointment.LegacyFreeBusyStatus = $BusyStatus
$newAppointment.Categories.Add($Category)
I think its correct but the data types are different.
so confused :-)
Thanks
Ben
Wednesday, July 2, 2014 2:49 PM
Thanks. So running this from powershell, it strips all the line breaks. I am convinced its not my script but the module I am calling. Some how I need to modify the module but lost with that.
My full script looks like this:
$paramater = @{
Identity = 'service.desk';
Subject = 'test1234';
Body = @('example customer',
'example line 2 text',
'example line 3 text') -join "`n"
Location = 'location';
EndDate = '2014-07-01T17:00:00';
StartDate = '2014-07-01T09:00:00';
RequiredAttendees = '[email protected]'
BusyStatus = 'oof';
Category = 'Green Category';
}
#Login using Office 365 username and encrypted password from SecureString file
$username = "[email protected]"
$password = cat C:\CalendarScripts\securestring.txt | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password
Import-Module MSOnline
#Import Calendar Function
Import-Module C:\CalendarScripts\NewOSCEXOAppointment\NewOSCEXOAppointment1.psm1 | Unblock-File
Connect-OSCEXOWebService -Credential $mycred -Verbose
New-OSCEXOAppointment @paramater
Wednesday, July 2, 2014 3:03 PM
So close thanks Mike. It reads the file and brings back the text but still strips the lines. It must be the Exchange Web Services that the module calls that is stripping it. Thats a well beyond my peanut brain. Thanks for your help.
Wednesday, July 2, 2014 3:08 PM
Thanks Chen. Tried mikes suggestion with no luck. It reads the file but strips it. It has to be the EWS newAppointment.Body function that is stripping it. Who knows.
Monday, July 14, 2014 3:28 AM
Hi Chen,
Did you have any luck with the XML?
Thanks
Ben
Monday, July 14, 2014 4:26 AM
I think I have it. Using the script above from Chen, I changed file.txt to file.html and put in <br> as needed. That worked.
Thanks for all your help. I have learnt a lot.
Cheers
Ben
Monday, July 14, 2014 8:05 AM
Glad it worked for you !!! Cheers !!!
Regards Chen V [MCTS SharePoint 2010]