Creating customer service Calendar in D365 via PowerShell command - powershell

I want to create "customer service Calendar" in D365 via PowerShell command. Can anyone help me? Thanks in advance
I have tried something like this, can able to create calendar not calendarurle
$usercalendarid="4385DF09-D156-4D4E-BABE-E6548DF50B49"
$StandardBUid ="BCAF91CC-5D73-EA11-A811-000D3AB855E7"
$mainCalendarGuid="4385DF09-D156-4D4E-BABE-E6548DF50B49"
$innerCalendarGuid="9566ab3e-208f-40cb-9b01-35110efa1161"
$businessunit = Get-CrmRecord -conn $conn -EntityLogicalName businessunit -Id $StandardBUid -Fields name
$standardBUReference = New-CrmEntityReference -EntityLogicalName businessunit -Id $StandardBUid
$calendarTypeValue = New-Object -TypeName Microsoft.Xrm.Sdk.OptionSetValue -ArgumentList 1
$calendarid = New-Object -TypeName System.Guid $mainCalendarGuid
New-CrmRecord -conn $conn -EntityLogicalName calendar -Fields #{"name"="cale2";"type"=$calendarTypeValue;"businessunitid"=$standardBUReference;calendarid=$calendarid }
*-- Create innercalendar*
$innerscalendarid = New-Object -TypeName System.Guid $innerCalendarGuid
$calendarTypeValue = New-Object -TypeName Microsoft.Xrm.Sdk.OptionSetValue -ArgumentList -1
New-CrmRecord -conn $conn -EntityLogicalName calendar -Fields #{"name"="inner";"businessunitid"=$standardBUReference; calendarid=$innerscalendarid }
-- Create calendarrule
$createdInnerCalendarReference = New-CrmEntityReference -EntityLogicalName calendar -Id $innerCalendarGuid
$createdMainCalendarReference = New-CrmEntityReference -EntityLogicalName calendar -Id $mainCalendarGuid
$timezoneValue = New-Object -TypeName Microsoft.Xrm.Sdk.OptionSetValue -ArgumentList 190
$entCollectioncalendarRules = New-Object Microsoft.Xrm.Sdk.EntityCollection
[Microsoft.Xrm.Sdk.Entity]$calendarRuleEntity = New-Object Microsoft.Xrm.Sdk.Entity
$calendarRuleEntity.LogicalName = 'calendarrule'
$calendarRuleEntity.Id= New-Object -TypeName System.Guid '9d3423b2-c69e-46db-9fde-b7f68784286c'
$calendarRuleEntity.Attributes['duration']=1234
$calendarRuleEntity.Attributes['calendarid']=$createdMainCalendarReference
$calendarRuleEntity.Attributes['innercalendarid']=$createdInnerCalendarReference
$calendarRuleEntity.Attributes['effort']=1.0;
$calendarRuleEntity.Attributes['extentcode']=1;
$calendarRuleEntity.Attributes['pattern'] = "FREQ=DAILY;COUNT=1"
$calendarRuleEntity.Attributes['rank']=0
$calendarRuleEntity.Attributes['timezonecode']=110
$calendarRuleEntity.Attributes['starttime'] = Get-Date
$calendarRuleEntity.Attributes['innercalendarid'] =$createdInnerCalendarReference
$calendarRuleEntity.Attributes['calendarid'] = $createdMainCalendarReference
$entCollectioncalendarRules.Entities.Add($calendarRuleEntity)
$entCollectioncalendarRules.Entities.Add($calendarRuleEntity1)
-- Update Calendarrule to calendar
$calendartobeUpdated = Get-CrmRecord -conn $conn -EntityLogicalName calendar -Id $innerCalendarGuid -Fields *
$calendartobeUpdated.name="Updated from powershell 2"
$calendartobeUpdated.calendarrules.Entities.Add($calendarRuleEntity)
Set-CrmRecord -conn $conn -CrmRecord $calendartobeUpdated

Related

How can I uninstall Hotfix remotely with Powershell

This what I have so far it brings up a Syntax screen for wusa. I have confirmed that the Trim is working. If I leave out the remote computer name is works on the local computer. I will be adding this to a much larger script just trying to get this working before trying to add it.
<#
.NAME
Template
#>
$comp = "Remote Pc Name Goes Here"
$str = $Hotfix_TextBox.Text. Trim("K","B")
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,400)
$Form.text = "Form"
$Form.TopMost = $false
$gethotfix = New-Object system.Windows.Forms.Button
$gethotfix.text = "Get Hotfixes"
$gethotfix.width = 120
$gethotfix.height = 30
$gethotfix.location = New-Object System.Drawing.Point(100,81)
$gethotfix.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$removehotfix = New-Object system.Windows.Forms.Button
$removehotfix.text = "Remove Hotfix"
$removehotfix.width = 120
$removehotfix.height = 30
$removehotfix.location = New-Object System.Drawing.Point(100,120)
$removehotfix.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Hotfix_TextBox = New-Object system.Windows.Forms.TextBox
$Hotfix_TextBox.Text = ""
$Hotfix_TextBox.multiline = $false
$Hotfix_TextBox.width = 174
$Hotfix_TextBox.height = 20
$Hotfix_TextBox.location = New-Object System.Drawing.Point(12,235)
$Hotfix_TextBox.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
<#
$Trimmed_TextBox = New-Object system.Windows.Forms.TextBox
#$Trimmed_TextBox.Text = "$str"
$Trimmed_TextBox.multiline = $false
$Trimmed_TextBox.width = 174
$Trimmed_TextBox.height = 20
$Trimmed_TextBox.location = New-Object System.Drawing.Point(12,265)
$Trimmed_TextBox.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
#>
$Form.controls.AddRange(#($gethotfix,$removehotfix,$Hotfix_TextBox))
$gethotfix.Add_Click({ GetHotfix })
$removehotfix.Add_Click({ RemoveHotfix })
#region Logic
function GetHotfix {$Hotfix_TextBox.Text = Get-Hotfix -Computername $comp |
Select-Object -ExpandProperty 'HotFixID'|
Out-GridView -Title 'Installed Hotfixes' -PassThru }
#$Hotfix_TextBox.Text. Trim("K","B")
#$Hotfix_TextBox.Text = "$str"
function RemoveHotfix{
#$Trimmed_TextBox.Text = "$str"
$comp = "dus-xtdfed9r386"
#Uninstall-HotFix -ComputerName $comp
wusa -computername /$comp | /uninstall | /kb:$str
}
#endregion
[void]$Form.ShowDialog()
This turned out to be a fiasco. Even when I could get it to work there are many KBs that cannot be removed, as well as we had policies that would not allow us to remove many others. Just not worth the effort. Perhaps someone with less strengent policies in place can do something with this. Have Fun.

WinForm not loading the first time

I am writing a program using PowerShell WinForms.
For example, the "Login" part of the program.
#CREDENTIAL CREATE - LOGIN AND PASSWORD INPUT
############################################
#APP FOUNDATION
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#Login_Form
$Login_Form = New-Object -TypeName System.Windows.Forms.Form
$Login_Form.ClientSize = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]340,[System.Int32]110))
$Login_Form.Text = [System.String]'Login_Form'
$Login_Form.add_Load($Login_Form_Load)
$Login_Form.ResumeLayout($false)
$Login_Form.PerformLayout()
$Login_Form.SuspendLayout()
$Login_Form.FormBorderStyle = "FixedDialog"
#Login_Label_User
$Login_Label_User = (New-Object -TypeName System.Windows.Forms.Label)
$Login_Label_User.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]12,[System.Int32]9))
$Login_Label_User.Name = [System.String]'Login_Label_User'
$Login_Label_User.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]100,[System.Int32]18))
$Login_Label_User.TabIndex = [System.Int32]3
$Login_Label_User.Text = [System.String]'Login:'
$Login_Label_User.UseCompatibleTextRendering = $true
#Login_TextBox_User
$Login_TextBox_User = (New-Object -TypeName System.Windows.Forms.TextBox)
$Login_TextBox_User.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]12,[System.Int32]30))
$Login_TextBox_User.Name = [System.String]'Login_TextBox_User'
$Login_TextBox_User.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]224,[System.Int32]21))
$Login_TextBox_User.TabIndex = [System.Int32]0
$Login_TextBox_User.Text = ((Get-ADDomain).name + "\")
$Login_TextBox_User.add_TextChanged($Login_TextBox_User_TextChanged)
#Login_TextBox_Password
$Login_TextBox_Password = (New-Object -TypeName System.Windows.Forms.TextBox)
$Login_TextBox_Password.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]12,[System.Int32]74))
$Login_TextBox_Password.Name = [System.String]'Login_TextBox_Password'
$Login_TextBox_Password.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]187,[System.Int32]21))
$Login_TextBox_Password.TabIndex = [System.Int32]1
$Login_TextBox_Password.Text = [System.String]''
$Login_TextBox_Password.PasswordChar = '*'
#Login_Button_Enter
$Login_Button_Enter = (New-Object -TypeName System.Windows.Forms.Button)
$Login_Button_Enter.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]251,[System.Int32]74))
$Login_Button_Enter.Name = [System.String]'Login_Button_Enter'
$Login_Button_Enter.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]75,[System.Int32]21))
$Login_Button_Enter.TabIndex = [System.Int32]2
$Login_Button_Enter.Text = [System.String]'ENTER'
$Login_Button_Enter.UseCompatibleTextRendering = $true
$Login_Button_Enter.UseVisualStyleBackColor = $true
$Login_Button_Enter.add_Click($Login_Button_Enter_Click)
#Login_Button_ShowHide
$Login_Button_ShowHide = (New-Object -TypeName System.Windows.Forms.Button)
$Login_Button_ShowHide.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]205,[System.Int32]74))
$Login_Button_ShowHide.Name = [System.String]'Login_Button_ShowHide'
$Login_Button_ShowHide.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]32,[System.Int32]21))
$Login_Button_ShowHide.TabIndex = [System.Int32]2
$Login_Button_ShowHide.Text = [System.String]'SHOW'
$Login_Button_ShowHide.UseCompatibleTextRendering = $true
$Login_Button_ShowHide.UseVisualStyleBackColor = $true
$Login_Button_ShowHide.add_Click($Login_Button_ShowHide_Click)
#Login_Label_Password
$Login_Label_Password = (New-Object -TypeName System.Windows.Forms.Label)
$Login_Label_Password.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]12,[System.Int32]54))
$Login_Label_Password.Name = [System.String]'Login_Label_Password'
$Login_Label_Password.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]100,[System.Int32]17))
$Login_Label_Password.TabIndex = [System.Int32]4
$Login_Label_Password.Text = [System.String]'Password:'
$Login_Label_Password.UseCompatibleTextRendering = $true
$Login_Label_Password.add_Click($Label2_Click)
#Form.Controls
$Login_Form.controls.AddRange(#($Login_Label_Password,$Login_Label_User,$Login_Button_Enter,$Login_Button_ShowHide,$Login_TextBox_Password,$Login_TextBox_User))
#Login_Button_Enter Click logic
$Login_Button_Enter_Click = {
$Script:username = $Login_TextBox_User.Text
$Script:password = $Login_TextBox_Password.Text
$Script:secstr = New-Object -TypeName System.Security.SecureString
$Script:password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$Script:cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$Login_Form.Close()
}
#Login_Button_ShowHide Click logic
$Login_Button_ShowHide_Click = {
if($Script:Login_TextBox_Password.PasswordChar -eq '*') {
$Script:Login_TextBox_Password.PasswordChar = 0
$Login_Button_ShowHide.Text = [System.String]'SHOW'
}
elseif($Login_TextBox_Password.PasswordChar -eq 0) {
$Login_Button_ShowHide.Text = [System.String]'HIDE'
$Script:Login_TextBox_Password.PasswordChar = "*"
}
}
#START APPLICATION
[void]$Login_Form.ShowDialog()
When I run the script for the first time, the window and everything else is shown correctly, but the buttons and logic do not work.
For everything to work fine, I need to run it a second time.
How do I make everything work the first time I start it?
I've tried everything that is possible already. I do not understand what is wrong.
PowerShell is a largely interpreted language - statements are executed in line order.
You therefore need to define the event action scriptblocks before you can assign them to the respective event handlers, otherwise you're just assigning $null to the event handlers, which is why none of your buttons work.
# define the scriptblock that's supposed to handle the event action...
$Login_Button_ShowHide_Click = {
if($Script:Login_TextBox_Password.PasswordChar -eq '*') {
$Script:Login_TextBox_Password.PasswordChar = 0
$Login_Button_ShowHide.Text = [System.String]'SHOW'
}
elseif($Login_TextBox_Password.PasswordChar -eq 0) {
$Login_Button_ShowHide.Text = [System.String]'HIDE'
$Script:Login_TextBox_Password.PasswordChar = "*"
}
}
#Login_Button_ShowHide
$Login_Button_ShowHide = (New-Object -TypeName System.Windows.Forms.Button)
$Login_Button_ShowHide.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]205,[System.Int32]74))
$Login_Button_ShowHide.Name = [System.String]'Login_Button_ShowHide'
$Login_Button_ShowHide.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]32,[System.Int32]21))
$Login_Button_ShowHide.TabIndex = [System.Int32]2
$Login_Button_ShowHide.Text = [System.String]'SHOW'
$Login_Button_ShowHide.UseCompatibleTextRendering = $true
$Login_Button_ShowHide.UseVisualStyleBackColor = $true
# ... _before_ assigning it to handlethe event
$Login_Button_ShowHide.add_Click($Login_Button_ShowHide_Click)
Repeat for all the scriptblocks assigned as event handlers - either move them all to the top of the script, or, move all the .add_<EventName>() calls to the bottom, just before launching the GUI:
# Rest of script goes here ...
# register all event handlers
$Login_TextBox_User.add_TextChanged($Login_TextBox_User_TextChanged)
$Login_Button_Enter.add_Click($Login_Button_Enter_Click)
$Login_Button_ShowHide.add_Click($Login_Button_ShowHide_Click)
# start app
[void]$Login_Form.ShowDialog()

open a mailbox to which I have full access

I can open my mailbox in exchange online with EWS and Powershell
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$service.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $mail, $password
$service.URL = New-Object Uri("https://outlook.office365.com/EWS/Exchange.asmx")
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($numOfEmailsToRead, $index)
$folderview = New-Object Microsoft.Exchange.WebServices.Data.FolderView(10)
$folderview.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.Webservices.Data.BasePropertySet]::FirstClassProperties)
$folderview.PropertySet.Add([Microsoft.Exchange.Webservices.Data.FolderSchema]::DisplayName)
$folderview.Traversal = [Microsoft.Exchange.Webservices.Data.FolderTraversal]::Deep
$folderfindResults = $service.FindFolders([Microsoft.Exchange.Webservices.Data.WellKnownFolderName]::Inbox, $folderview)
$SearchFilterContainsSubString = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, $Paramerter)
$SearchFilterNot = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+Not($SearchFilterContainsSubString)
foreach ($InboxFolder in $folderfindResults) {
$findResultsItems = $InboxFolder.findItems($SearchFilterNot,$view)}
How do I change the code to open a mailbox to which I have full access?
In your example you need to use the Mailbox overload for Folder id so change
$folderfindResults = $service.FindFolders([Microsoft.Exchange.Webservices.Data.WellKnownFolderName]::Inbox, $folderview)
$SearchFilterContainsSubString = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, $Paramerter)
$SearchFilterNot = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+Not($SearchFilterContainsSubString)
foreach ($InboxFolder in $folderfindResults) {
$findResultsItems = $InboxFolder.findItems($SearchFilterNot,$view)}
To
$MailboxToAccess = "blah#blah.com"
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxToAccess)
$folderfindResults = $service.FindFolders($folderid, $folderview)
$SearchFilterContainsSubString = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, $Paramerter)
$SearchFilterNot = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+Not($SearchFilterContainsSubString)
foreach ($InboxFolder in $folderfindResults) {
$findResultsItems = $InboxFolder.findItems($SearchFilterNot,$view)}
Here is a simple example of connecting to a different mailbox
$MailboxName = New-Object Microsoft.Exchange.WebServices.Data.Mailbox("support#domain.com") #specifies which mailbox EWS should connect to
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
For more detailed info please read this blog: https://gsexdev.blogspot.com/2016/10/ews-basics-accessing-and-using-shared.html
Thank you for your support.
I was able to do it with the line
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$Mailbox);
Resolve.
I was getting a 401 error. The error was because I had overwritten the credentials. Now everything works.
In Exchange the executing user needs the role
"ApplicationImpersonation"

Create a user in MongoDb using powershell

I'm using MongoDb v3.0.3 and want to create a user in a database with admin privileges using powershell. I hook into the C# driver but I don't get very far:
$pathToMongoDbCSharpDriver = "F:\Work\...\mongocsharpdriver.1.9.2\lib\net35"
Add-Type -Path "$pathToMongoDbCSharpDriver\MongoDB.Bson.dll"
Add-Type -Path "$pathToMongoDbCSharpDriver\MongoDB.Driver.dll"
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost:30000"
$server = $client.GetServer()
$databaseName = "Dev"
$collectionName = "Settings"
$database = $server.GetDatabase($databaseName)
$collection = $database.GetCollection($collectionName)
$credentials = New-Object -TypeName MongoDB.Driver.MongoCredential("Admin", "password", $true);
$user = New-Object -TypeName MongoDB.Driver.MongoUser($credentials, $false)
$credentials fails because argument 1 it is not a MongoIdentity and I can't find any information about how to create one of these. Any help would be gratefully received
I use all the parameters in the argument list. The following works fine for me:
$Client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://dbuser:dbpass#localhost:27017/test"

PowerShell Active Directory AddAccessRule

I am attempting to add a Computer Object to the security of another Computer object in AD and give it Full Control.
$ou = [ADSI]"LDAP://CN=Tester1,OU=test,OU=Test1,DC=contoso,DC=com"
$sec = $ou.psbase.ObjectSecurity
$act = [System.Security.AccessControl.AccessControlType]::Allow
$adrights = [System.DirectoryServices.ActiveDirectoryRights]::GenericAll
$who = New-Object -TypeName System.Security.Principal.???? -ArgumentList "domain\ComputerObject"
$newrule1 = New-Object -TypeName System.DirectoryServices.ActiveDirectoryAccessRule -ArgumentList $who, $adrights, $act
$sec.AddAccessRule($newrule1)
$ou.psbase.CommitChanges()
With the line (New-Object -TypeName System.Security.Principal.??) what is the correct class for Computer object? (I have tried .NTaccount)
The answer was
$who = New-Object -TypeName System.Security.Principal.NTaccount -ArgumentList "domain\ComputerObject$"
the $ specifies a computer Object.