How do I retrieve PR_RULE_ACTIONS via PowerShell and the EWS Managed API? - powershell

I have need of retrieving and inspecting the delegate forwarding rule (the built-in delegate commands in EWS being inadequate for my needs since they choke on groups being used as delegates).
I am able to successfully locate the rule created by "Schedule+ EMS Interface". However, I am unable to retrieve PR_RULE_ACTIONS. Turning on tracing.
I see that the PidTagRuleMsgProvider property is getting returned just fine, but PR_RULE_ACTIONS never does.
I suspect that I am using the wrong MAPI property type in the propertyset definition, but I've gone through everything listed at http://msdn.microsoft.com/en-us/library/exchangewebservices.mapipropertytypetype(v=exchg.140).aspx . Any clues?
Here is the relevant snippet of code:
# Setup Basic EWS Properties for Message Search - Used to locate Hidden Forwarding Rule
$searchFilterForwardRule = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass, "IPM.Rule", [Microsoft.Exchange.WebServices.Data.ContainmentMode]::Prefixed, [Microsoft.Exchange.WebServices.Data.ComparisonMode]::Exact)
$itemViewForwardRule = New-Object Microsoft.Exchange.WebServices.Data.ItemView(30, 0, [Microsoft.Exchange.Webservices.Data.OffsetBasePoint]::Beginning)
$itemViewForwardRule.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties, [Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass, [Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject)
$itemViewForwardRule.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Associated
# Properties for Hidden Delegate Forwarding Rule
$PID_TAG_RULE_MSG_PROVIDER = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x65EB,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$PID_TAG_RULE_ACTIONS = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6680,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
# Property Set for Delegate Forward Rule
$propertySetForwardRule = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties, $PID_TAG_RULE_MSG_PROVIDER)
$forwardRuleExists = $false
$findResults = $service.FindItems([Microsoft.Exchange.Webservices.Data.WellKnownFolderName]::Inbox, $searchFilterForwardRule, $itemViewForwardRule)
If ($findResults.TotalCount -lt 1) {
Write-Error "Failed to find rule" "Error"
} Else {
Foreach ($item in $findResults.Items) {
$item.Load($propertySetForwardRule)
If ($item.ExtendedProperties.Count -ge 1) {
If ($item.ExtendedProperties[0].Value -eq "Schedule+ EMS Interface") {
$forwardRuleExists = $true
write-host "Delegate forwarding rule found." -ForegroundColor Cyan
$propertySetForwardRule.Add($PID_TAG_RULE_ACTIONS)
$item.Load($propertySetForwardRule)
Write-Host "Attempting to retrieve x6680 PR_RULE_ACTIONS (PidTagRuleActions)" -ForegroundColor Cyan
$PR_RULE_ACTIONS = $null
if($Item.TryGetProperty($Pid_Tag_Rule_Actions,[ref]$PR_RULE_ACTIONS)){
return $PR_RULE_ACTIONS
} # endif
else {write-host "TryGetProperty for PR_RULE_ACTIONS failed!" -ForegroundColor Red
} # endelse
} # End If - Correct Message
} # End If - Has Extended Properties
} # End ForEach
} # End If - Message Count

Glen Scales was able to set me on the right path. It turns out that PR_RULE_ACTIONS is not exposed via EWS, but the same data exposed via an attribute called PR_EXTENDED_RULE_ACTIONS. Now I'm happily slinging code to parse the binary blob.
http://msdn.microsoft.com/en-us/library/ee218391(v=EXCHG.80).aspx

The property tag for PR_RULE_ACTIONS is 0x668000FE. You can see it (and the property data) in OutlookSpy (I am its author) - go to the Inbox folder, click IMAPIFolder button, go to the PR_RULES_TABLE tab, select the rule, double click on the PR_RULE_ACTIONS property.
Note that PT_ACTIONS MAPI type (0x000FE) is only accessing in Extended MAPI, I don't think EWS will be able to return it.

Related

Powershell: Passed array returns $true [duplicate]

This question already has answers here:
How do I pass multiple parameters into a function in PowerShell?
(15 answers)
Closed last year.
I'm pretty sure I have some synthax error in here, but i can't find it.
I wrote this little script here to find some Files in a Folder - I want all the Filepaths that don't have some of the terms in the $excludeList in there but my Contains-Function always returns $true. And when I enable the Write-Output-Line, it also, always sais $true
$path = 'C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications'
$excludeList = #('Test', 'Samples', 'BaseApp\Source', 'Application\Source', 'system application\source')
function Contains([string]$Fullpath, $ExcludeList) {
$result = $false
#Write-Output $ExcludeList
$ExcludeList | % { $result = ($FullPath.Contains($_) -or $result) }
return $result
}
(Get-ChildItem $path -Filter '*.app' -Recurse).FullName | % {
if (!(Contains($_, $excludeList))) { $_ }
}
Here is the List of Files I'm trying to filter:
C:\WINDOWS\system32> (Get-ChildItem $path -Filter '*.app' -Recurse).FullName
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\AMCBanking365Fundamentals\Source\Microsoft_AMC Banking 365 Fundamentals.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\AMCBanking365Fundamentals\Test\Microsoft_AMC Banking 365 Fundamentals Test Automations.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\APIV1\Source\Microsoft__Exclude_APIV1_.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\APIV1\Test\Microsoft__Exclude_APIV1_ Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\APIV2\Source\Microsoft__Exclude_APIV2_.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\APIV2\Test\Microsoft__Exclude_APIV2_ Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Application\Source\Microsoft_Application.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Base Application.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Czech language (Czechia).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Danish language (Denmark).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Dutch language (Belgium).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Dutch language (Netherlands).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (Australia).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (Canada).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (New Zealand).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (United Kingdom).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (United States).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Finnish language (Finland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_French language (Belgium).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_French language (Canada).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_French language (France).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_French language (Switzerland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_German language (Austria).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_German language (Germany).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_German language (Switzerland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Icelandic language (Iceland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Italian language (Italy).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Italian language (Switzerland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Norwegian language (Norway).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Russian language (Russia).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Spanish language (Mexico).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Spanish language (Spain).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Swedish language (Sweden).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Library-NoTransactions.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Bank.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Cash Flow.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Cost Accounting.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-CRM integration.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Data Exchange.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Dimension.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-ERM.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Fixed Asset.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-General Journal.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Graph.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Integration.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Invoicing.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Job.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Local.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Marketing.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Misc.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Monitor Sensitive Fields.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Permissions.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Physical Inventory.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Prepayment.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Rapid Start.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Report.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Resource.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Reverse.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-SCM.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-SINGLESERVER.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-SMB.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-SMTP.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-TestLibraries.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Upgrade.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-User.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-VAT.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Workflow.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\ClientAddIns\Source\Microsoft__Exclude_ClientAddIns_.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\CompanyHub\Source\Microsoft_Company Hub.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\CompanyHub\Test\Microsoft_Company Hub Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\DataArchive\app\Microsoft_Data Archive.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\DataArchive\test\Microsoft_Data Archive Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\datacorrectionfa\source\Microsoft_Troubleshoot FA Ledger Entries.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Current User Connector\Source\Microsoft_Email - Current User Connector.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Current User Connector\Test\Microsoft_Email - Current User Connector Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Microsoft 365 Connector\Source\Microsoft_Email - Microsoft 365 Connector.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Microsoft 365 Connector\Test\Microsoft_Email - Microsoft 365 Connector Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Outlook REST API\Source\Microsoft_Email - Outlook REST API.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Outlook REST API\Test\Microsoft_Library Outlook REST API.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - SMTP Connector\Source\Microsoft_Email - SMTP Connector.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - SMTP Connector\Test\Microsoft_Email - SMTP Connector Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\EssentialBusinessHeadlines\Source\Microsoft_Essential Business Headlines.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\EssentialBusinessHeadlines\Test\Microsoft_Essential Business Headlines Test.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\LatePaymentPredictor\Source\Microsoft_Late Payment Prediction.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\LatePaymentPredictor\Test\Microsoft_Late Payment Prediction Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\microsoftuniversalprint\source\Microsoft_Universal Print Integration.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\onprem permissions\source\Microsoft_OnPrem Permissions.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\onprem permissions\test\Microsoft_OnPrem Permissions Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Onprem Permissions DACH\Source\Microsoft_OnPrem Permissions DACH.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\paypalpaymentsstandard\source\Microsoft_PayPal Payments Standard.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\paypalpaymentsstandard\test\Microsoft_PayPal Payments Standard Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\recommendedapps\source\Microsoft_Recommended Apps.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\recommendedapps\test\Microsoft_Recommended Apps Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\salesandinventoryforecast\source\Microsoft_Sales and Inventory Forecast.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\salesandinventoryforecast\test\Microsoft_Sales and Inventory Forecast Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\sendtoemailprinter\source\Microsoft_Send To Email Printer.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\simplifiedbankstatementimport\source\Microsoft_Simplified Bank Statement Import.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\SwissQRBill\Source\Microsoft_QR-Bill Management for Switzerland.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\SwissQRBill\Test\Microsoft_QR-Bill Management for Switzerland Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\system application\source\Microsoft_System Application.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\system application\test\Microsoft_System Application Test Library.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\system application\test\Microsoft_System Application Test.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\performancetoolkit\Microsoft_Performance Toolkit Samples.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\performancetoolkit\Microsoft_Performance Toolkit Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\performancetoolkit\Microsoft_Performance Toolkit.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestLibraries\Any\Microsoft_Any.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestLibraries\Assert\Microsoft_Library Assert.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestLibraries\permissions mock\Microsoft_Permissions Mock.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestLibraries\Variable Storage\Microsoft_Library Variable Storage.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestRunner\Microsoft_Test Runner.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\vatgroupmanagement\source\Microsoft_VAT Group Management.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\vatgroupmanagement\test\Microsoft_VAT Group Management Tests.app
Please tell me what went wrong, and if you know a way to achieve the same result more smoothly, I'm open for suggestions :)
Thank you in advance.
you need to remove parenthesis and comma in - if (!(Contains($_, $excludeList))) { $_ }
you have a problem with passing argument to a function.
look here: How do I pass multiple parameters into a function in PowerShell?
$path = 'C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications'
$excludeList = #('Test', 'Samples', 'BaseApp\Source', 'Application\Source', 'system application\source')
function Contains([string]$Fullpath, $ExcludeList) {
$result = $false
#Write-Output $ExcludeList
$ExcludeList | % { $result = ($FullPath.Contains($_) -or $result) }
return $result
}
(Get-ChildItem $path -Filter '*.app' -Recurse).FullName | % {
if (!(Contains $_ $excludeList)) { $_ }
}
$path = 'C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications'
[string[]]$excludeList = #('Test', 'Samples', 'BaseApp\\Source', 'Application\\Source', 'system application\\source')
[string]$excludeListRegex = $excludeList -join('|')
(Get-ChildItem $path -Filter '*.app' -Recurse).FullName | % {
if ($_ -notmatch $excludeListRegex) { $_ }
}
This is a trick I've used in the past. Basically you are dynamically creating one big regex query and using that to match the lines. Downside is you must format the entries in the ExcludeList array in a regex friendly manner. You'll notice I escaped all the backslashes with backslashes. Backslash IS the escape char in regex so if you just want an actual backslash you have to type two backslahes like I did. The upside is this is really fast because the alternative is nested loops where you'd have to iterate through every file in the list once for every exclusion. Another downside is that i'm sure there's a limit to how long that regex query can get (not sure offhand what it is) but it's not infinitely scalable.
I believe there may be a more elegant way to code this but I'd have to put a lot more thought into it. ;-)

I am Trying to connect to an existing BizTalk application and add Receive Ports and Send Ports and Parties etc

When run this I do not get a error, but when I create send ports and Receive locations I don't see anything
make sure the ExplorerOM assembly is loaded
[void] [System.reflection.Assembly]::LoadwithPartialName("Microsoft.BizTalk.ExplorerOM")
Connect to the BizTalk management database
$Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
$Catalog.ConnectionString = "SERVER=sql\instance; DATABASE=BizTalkMgmtDb; Integrated Security=SSPI"
Connect to an existing application in Biztalk
Create a message to let you know if powershell can't find the applicaiton on your server
If it can't find the application it gives a error and discard changes. If so it continues
$In837_2Cimor = $Catalog.Applications["In837_2Cimor"]
if ($In837_2Cimor -eq $null)
{
Write-Host "`r`nFailed to find `"In837_2Cimor`" deployed on this BizTalk server."
}
else
{
**Register a trap handler for any exceptions**
$ErrorActionPreference="silentlycontinue"
trap { "Exception encountered:`r`n"; $_; "`r`nDiscarding Changes.`r`n";$Catalog.DiscardChanges();exit; }
}
**Create a new receive port named For the new provider**
And A new receive location will also be created and associated
with the receive port.
$NewRP = $In837_2Cimor.AddNewRecievePort($false,$false)
$NewRP.Name = "RP_Pickup999_000"
Note if you don’t set the name property for the receive port,
it will create a new receive location and add it to the receive
port.
**Create a new receive location and add it to the receive port**
$NewRL = $In837_2Cimor.AddNewReceiveLocation($false,$false)
$NewRL.PrimaryTransport.TransportType = $Catalog.ProtocolTypes["FILE"]
$NewRL.PrimaryTransport.Address = "\\location\BizTalkd\"
$NewRL.SendPipeline = $Catalog.Pipelines["Microsoft.BizTalk.DefaultPipelines.PassThruTransmit"]
**Save the changes**
Write-Host "Adding $NewRP.Name..."
$catalog.SaveChanges();
Write-Host "`r`n $NewRP.Name has been created."
Create New In837 sendPorts for a new provider
$NewSP = $In837_2Cimor.AddNewSendPort($false,$false)
$NewSP.Name = "SP_999_TO_FTP_000"
$NewSP.PrimaryTransport.TransportType = $Catalog.ProtocolTypes["FILE"]
$NewSP.SendHandler = $Catalog.SendHandlers["BizTalkServerApplication"]
$NewSP.PrimaryTransport.Address = "\\location\"
$NewSP.SendPipeline = $Catalog.Pipelines["Microsoft.BizTalk.DefaultPipelines.PassThruTransmit"]
Add the filter to New send port
$NewSP.Filter = "<Filter><Group>" +
"<Statement Property='BTS.ReceivePortName' Operator='==' Value='RP_Pickup999_000'/>" +
"</Group></Filter>"
Write-Host Adding ($NewSP.Name) and ($NewRP.Name) and ($NewRL.Name) to catalog...
$Catalog.SaveChanges()

How to remove a permission, a member, or a role?

I have this script below to add roles and members and permissions
Import-Module sqlserver
$Server = new-Object Microsoft.AnalysisServices.Tabular.Server
$Server.Connect("SERVER\INSTANCE")
$TabDB = $SERVER.Databases["DATABASENAME"]
$AddRole = new-Object Microsoft.AnalysisServices.Tabular.ModelRole
$AddRole.Name = 'NewRole1'
$AddRole.ModelPermission="Read"
$RoleMember = New-Object Microsoft.AnalysisServices.Tabular.WindowsModelRoleMember
$RoleMember.MemberName = 'DOMAIN\ACCOUNT'
$TabDB.Model.Roles.Add($AddRole)
$AddRole.Members.Add($RoleMember)
$TabDB.Update([Microsoft.AnalysisServices.UpdateOptions]::ExpandFull)
$server.Disconnect()
How can I remove a permission, a member, and a role?
I tried this at least for the role, but the console returns back "False"
$TabDB.Model.Roles.Remove($AddRole)
When working with ssas from powershell (or C# for that matter) you can use the analysisservices namespace of microsoft:
Microsoft analysisservices.
This is an object oriented way of working with ssas databases.
This is an old script I wrote for maintainers:
function addRoleToDb {
$roleName = Read-Host "How should this role be called?"
if ($global:dataBase.Roles.findByName($roleName)) {
echo "This role already exists"
return
} elseif ($roleName -eq "") {
echo "You can't give an empty name for a role."
return
}
echo "Warning: this role will start out empty and will have to be modified in order to be used. (it wil have read permission)"
[Microsoft.AnalysisServices.Role] $newRole = New-Object([Microsoft.AnalysisServices.Role])($roleName)
$global:dataBase.Roles.add($newRole)
$dbperm = $global:dataBase.DatabasePermissions.add($newRole.ID)
$dbperm.Read = [Microsoft.AnalysisServices.ReadAccess]::Allowed
$global:dataBase.Roles.Update()
$dbperm.Update()
return
}
At this point I already had a global variable database.
This is a method that would add a role to the database. Deleting the database would work practically the same, you would get the instance of the role with:
role = database.roles.findbyname()
or
role = database.roles.findbyid()
and then
role.Drop(DropOptions.AlterOrDeleteDependents);
You should check that last line before using it because the alterordeletedependants is something I now use in my c# program, I don't remember if it worked in powershell.
Good luck!

Error when updating link urls in quick launch on sharepoint 2013 site with powershell

I have a problem when trying to update link urls in quick launch on sharepoint 2013 site with powershell. Basically I only want to change the url of specific links. My Powershell script code is as follows:
function FixUrlDocumentsLists() {
param([Microsoft.SharePoint.SPWeb]$SiteIdentity)
if ($SiteIdentity.Url -Like "http://mktintranet/sites/tmmkto/ITReports")
{
$quicklaunch = $SiteIdentity.Navigation.QuickLaunch
if($quicklaunch.Count -gt 0)
{
foreach($node in $quicklaunch)
{
if ($node.Title.ToUpper() -ne "HOME" -and $node.Title.ToUpper() -ne "SITE CONTENTS")
{
if($node.Url -eq $SiteIdentity.ServerRelativeUrl)
{
Write-Host "Fixing navigation links for web $($SiteIdentity.Title)" -ForegroundColor Yellow
Write-Host "Link Title: $($node.Title), OLD Link Url: $($node.Url)" -ForegroundColor Yellow
$node.Url=$node.Url.ToString()+"/_layouts/15/viewlsts.aspx"
Write-Host "Link Title: $($node.Title), NEW Link Url: $($node.Url)" -ForegroundColor Yellow
$node.Update()
$SiteIdentity.Update()
}
}
}
}
}
if($SiteIdentity.Webs.Count -gt 0)
{
foreach($subWeb in $SiteIdentity.Webs)
{
FixUrlDocumentsLists -SiteIdentity $subWeb
}
}
}
The error occurs on $node.Update() method. The error description is as follows:
Exception calling "Update" with "0" argument(s): "Cannot open "/sites/tmmkto/ITReports/_layouts/15/viewlsts.aspx": no such file or folder."
I can't realize why the Update method is making Url validation. Even though the path /sites/tmmkto/ITReports/_layouts/15/viewlsts.aspx does exists.
Thanks,
Martin
SharePoint try and verify the node url if it is an internal url. On way around this is to mark your link as external (even though it is not) by:
Node.IsExternal = true; (csom)
Please ensure Show sub sites/Show pages option is unchecked under
Site Settings =>Navigation => Current Navigation=>
in Navigation: Display only the navigation items below the current site. You will get such error, if you try to update the Navigation links generating from the Sub Site/ Page files. It make sense for giving error if this option in on and you try to modify the link for auto generated sub site/Page link.

Update TFS Build Status via Powershell

I've a TFS Build Definition setup where, I execute a powershell script for security hardening, applying final patches etc.
Sometimes this powershell script can fail and I would like to change the status of build to be un-successfull or fail. Is there a way to change this TFS Build status via a powershell?
Try this:
$script:TFServerName = $ServerName
$script:TFServer = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($ServerName)
$script:TFBuildServer = $TFServer.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
$spec = $TFBuildServer.CreateBuildDetailSpec($TeamProject, $DefinitionName)
$spec.Status = 'Failed'
Make sure you've Add-Type the right versions of the TFS assemblies. I'm assuming here that since the properties support setters, they communicate back to the server to affect the change. But I'm not 100% sure of that.
Here's what I pieced together from MSDN:
[Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")
[Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
$tfsServerAddress = "http://{tfs hostname}:{port}/{CollectionName}"
$build = "{build definition name}_{build date}.{build count for the day}"
#update build status
$tfsProject = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsServerAddress)
$tfsBuildServer = $tfsProject.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
$spec = $tfsBuildServer.CreateBuildDetailSpec($teamProject)
$spec.BuildNumber = $build
$builds = $tfsBuildServer.QueryBuilds($spec).Builds
if ($builds[0].TestStatus -eq 'Succeeded'){
echo "updating test status:" $builds[0].TestStatus "to $testOutcome"
$builds[0].TestStatus = $testOutcome
}
if ($builds[0].Status -eq 'Succeeded'){
echo "updating build status:" $builds[0].Status "to $testOutcome"
$builds[0].Status = $testOutcome
}
$tfsBuildServer.SaveBuilds($builds)
Keep in mind that you need a special permission to update build status