编程语言

1,377 次浏览

Dynamics 365: Three way to get organization unique name

We have three ways to get organization unique name in Microsoft Dynamics 365.

A. Just click on the top right corner of the user avatar, and right here (org64e4ed31).

B. Get it with PowerShell, At first, you need to download the Dynamics 365 SDK to local, and then Run the PowerShell as administrator, cd to the SDK folder, and run .\RegisterXRMTooling.ps1

Add-PSSnapin Microsoft.Xrm.Tooling.Connector
Add-PSSnapin Microsoft.Xrm.Tooling.PackageDeployment

$Cred = Get-Credential
Get-CrmOrganizations -Credential $Cred -DeploymentRegion NorthAmerica –OnlineType Office365

C. Get it from the settings page of the Dynamics 365

Click Settings -> Customization -> Developer Resources -> Unique Name

2,486 次浏览

JavaScript: 注册粘贴事件,将剪切板中的内容去除HTML标签,并粘贴到光标位置

$("#Subject").bind('paste', function (e) {
        var pasteData = "";
        if (e.originalEvent.clipboardData) {
            pasteData = e.originalEvent.clipboardData.getData('text');
        } else {
            // for IE
            pasteData = window.clipboardData.getData("Text");
        }
        var self = this;

        // 在等待一段时间后,在当前光标位置,粘贴处理后的文本
        setTimeout(function () {
            var selection = document.getSelection();
            var cursorPos = selection.anchorOffset;
            var oldContent = selection.anchorNode.nodeValue;
        // 通过 Jquery的text() 去除所有样式
            var toInsert = $("<div>" + pasteData + "</div>").text();
            var newContent = oldContent.substring(0, cursorPos) + toInsert + oldContent.substring(cursorPos);
            selection.anchorNode.nodeValue = newContent;            
        }, 200);
        return false;
    });

1,512 次浏览
45,741 次浏览

通过 Excel 使用 VBA 计算协方差

作为一名执著的 C# 程序员,当一位学经济的老友从国外找来,让我帮他写完 VBA 作业的时候,一开始,其实我是拒绝的。
各路VB大神,不要嘲笑在下啊,我可是足足用了一个小时才在 Excel 里到了编写 VB 代码的地方 (默认 Office 2013 的那个开发者选项是隐藏的…)
我把一夜写的代码在这里Mark一下,万一以后再有类似的VB任务也不至于抓瞎,哈哈,我是一晚上没睡觉活活憋出来的,以下代码在Excel里计算协方差。
不过话说话来,感觉各种语言都是相同的,学会一种,换成另一种也不会太恐怖。

下载这个 Excel VBA 文件:Spreadsheeting assi
Continue reading

1,372 次浏览
3,051 次浏览

C#: 一个方法执行超时 timeout 检查的实现

我们经常有这样的需求:如果一个方法的执行,超过了一个设定时间(timeout)就需要立即返回不再继续,这里我利用 C# 异步委托的 AsyncWaitHandle 来尽量简便的实现这一需求。

具体实现如下。注意,这里需要被调用的方法遵守 delegate TR TimeOutDelegate(T param); 形式的方法签名,如有其他需要,可以自行定制也很方便。

namespace TimeOutHelper
{
    #region using directives

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Threading;

    #endregion using directives

    internal class Program
    {
        public delegate TR TimeOutDelegate<in T, out TR>(T param);

        private static void Main()
        {
            Dictionary<Guid, string> result;
            Console.WriteLine(TimeoutFunction.Execute(Test, "Hello, World!", out result, TimeSpan.FromSeconds(3)));
            Console.WriteLine("Hello, World!");
            Console.ReadKey();
        }

        public static Dictionary<Guid, string> Test(string sourceString)
        {
            var result = sourceString.ToDictionary(
                character => Guid.NewGuid(),
                character => character.ToString(CultureInfo.InvariantCulture));
            Thread.Sleep(4000);
            return result;
        }

        public static class TimeoutFunction
        {
            /// <summary>
            ///     Execute a method with timeout check
            /// </summary>
            /// <typeparam name="T">Target method parameter type</typeparam>
            /// <typeparam name="TR">The result type of execution</typeparam>
            /// <param name="timeoutMethod">Target method</param>
            /// <param name="param">Target method parameter</param>
            /// <param name="result">The result of execution</param>
            /// <param name="timeout">Set timeout length</param>
            /// <returns>Is timeout</returns>
            public static Boolean Execute<T, TR>(
                TimeOutDelegate<T, TR> timeoutMethod, T param, out TR result, TimeSpan timeout)
            {
                var asyncResult = timeoutMethod.BeginInvoke(param, null, null);
                if (!asyncResult.AsyncWaitHandle.WaitOne(timeout, false))
                {
                    result = default(TR);
                    return true;
                }
                result = timeoutMethod.EndInvoke(asyncResult);
                return false;
            }
        }
    }
}
3,870 次浏览

PowerShell: 隐藏明文密码

本文描述如何加密 PowerShell 中的敏感信息,从而避免敏感信息(诸如密码)在代码中被显式的展现出来。这里我们使用 ConvertFrom-SecureString 命令来完成加密操作,需要注意的是,如果我们没有指定Key,那么,将采用基于 Windows 内置的 DPAPI 进行数据的加密。这种情况下,原文的加密和解密必须在同一台机器上,且基于同一个User去进行。

ConvertFrom-SecureString [-SecureString] <SecureString> [[-SecureKey] <SecureString> ] [ <CommonParameters>]

你可以从这里了解该命令的更多信息。

  • If no key is specified, the Windows Data Protection API (DPAPI) is used to encrypt the standard string representation.
    Your secret was automatically encrypted by the built-in Windows data protection API (DPAPI), using your identity and your machine as encryption key. So only you (or any process that runs on your behalf) can decipher the secret again, and only on the machine where it was encrypted.
    Continue reading
2,582 次浏览

PowerShell: 远程调用

通过本地程序调用 PowerShell 自定义脚本,我们可以有周期、计划性的执行一些扩展的操作,这在一定程度上提高了本地程序自身逻辑的扩展性。而在编写一段 PowerShell 脚本的过程中,也难免会遇到需要一段脚本在另一台计算机上远程执行的情形。例如,我们在执行一段 PowerShell 脚本时,需要向某个 AD Group 中添加一个 AD User,而这一操作需要在域控机器上才能执行,而此时,我们就会用到 PowerShell 远程调用执行代码。

在调用之前,我们需要保证 PowerShell 的远程调用设置是开启的。
1 启用远端计算机的 Windows Remote Management (WS-Management) service.
(1) 通过 services.msc 启动服务管理器
(2) 在其中找到 Windows Remote Management (WS-Management) 这一项
(3) 确定该项服务正处于运行状态,并且启动类型被设置为 Automatic

2 设定本地组策略.
(1) 通过 gpedit.msc 开启本地组策略编辑器
(2) 找到 Computer Configuration > Administrative Templates > Windows Components > Windows Remote Management (WinRM) > WinRM
Service
(3) 在右侧的项目中,双击 “Allow remote server management through WinRM”
(4) 在新弹出的窗口中,选择Enabled,从而启用策略,然后再Options的设置区域,位IPv4和IPv6 filter添加规则”*”,从而使所有IP都不受到过滤器的限制。当然,这里也可以根据具体的环境进行配置,相关配置可参考下图。
Continue reading