小编Ang*_*wal的帖子

Powershell:在函数中定义详细开关

参考链接,我试图在我的脚本中引入详细模式.

当我有一个像这样定义的函数时 -

function TestVerbose
{
    param(
        [switch]$verbose,
        [Parameter(Mandatory = $True)] 
        $p1
    )

    if($verbose)
    {
     Write-Verbose "Verbose Mode"
    }
}

Get-Help TestVerbose
Run Code Online (Sandbox Code Playgroud)

我收到以下错误 -

Get-Help:为命令多次定义名为'Verbose'的参数.在行:12 char:9 + Get-Help <<<< TestVerbose + CategoryInfo:MetadataError:(:) [Get-Help],MetadataException + FullyQualifiedErrorId:ParameterNameAlreadyExistsForCommand,Microsoft.PowerShell.Commands.GetHelpCommand

但是,如果我定义这样的函数[删除参数强制属性],它工作正常

function TestVerbose
{
    param(
        [switch]$verbose,
        $p1
    )
    if($verbose)
    {
     Write-Verbose "Verbose Mode"
    }    
}

Get-Help TestVerbose
Run Code Online (Sandbox Code Playgroud)

知道为什么会出现这种行为吗?我想保留强制切换,并希望用户执行我的功能 -

TestVerbose -verbose

powershell powershell-2.0

11
推荐指数
1
解决办法
6555
查看次数

Powershell v2.0模块:默认加载路径(用户/ Windows系统文件夹)?

这是我之前提出的这个问题 - Powershell:在目标系统上安装模块

  • 什么是默认模块加载路径?现在经过这么多天,它已经开始发出这个错误(来自我的C#代码)

    Cannot find path 'C:\Users\angshuman\Documents\WindowsPowerShell\Modules\MyPSModules\MyPsModules.??psd1' because it does not exist.

    从SysWow64文件夹路径一直很好地加载
  • 为什么它突然在用户文件夹而不是Windows文件夹中搜索?

我在Windows 7 64位操作系统上通过C#执行相同的代码

    _ps = PowerShell.Create();   
    _ps.AddScript("Import-Module MyPSModules -PassThru");
    Collection<PSObject> psObjects = _ps.Invoke();
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0

11
推荐指数
2
解决办法
4万
查看次数

使用单个psd1文件加载多个模块(.psm1)

我想组织功能集成到多个名为.psm1文件,并让他们通过加载一个模块清单文件(的.psd1) -使得只有的.psd1文件就需要有相同的名称作为模块.

我认为这应该是可能的.有人可以帮帮我吗?

powershell powershell-2.0

10
推荐指数
2
解决办法
2万
查看次数

C# - 编译器错误 - 将int []赋给object []

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] obj = new object[3];
            obj[0] = new object();
            obj[1] = "some string";
            obj[2] = 10;

            string[] strings = new string[] { "one", "two", "three" };
            obj = strings; //---> No Error here, Why ?

            int[] ints = new int[] { 1, 2, 3 };
            obj = ints; /*-> Compiler error - Cannot implicitly convert type 'int[]' to 'object[]', Why ?*/ 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在执行上述步骤时遇到编译器错误.但是,在上一步中,没有错误.有人能解释一下这种行为吗?我正在使用VS 2010.

编辑 - 为了完整性,再次,这将无法编译 …

c# compiler-errors

7
推荐指数
1
解决办法
1858
查看次数

XQUERY - 如何在'value()'函数中使用sql:variable?

下面的查询试图选择给定节点的子节点.如何使用变量而不是硬编码子节点,以便我可以将它们作为SProc中的参数传递?

declare @T table(XMLCol xml)
insert into @T values
('<Root xmlns="http://tempuri.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Elem1 type="T1">
    <Name type="string" display="First name">John</Name>
    <TimeZone display="Time zone">
      <children>
      <DisplayName type="string" display="Display name">GMT Standard Time</DisplayName>
      <HiddenName type="string" display="Hidden name">GMT</HiddenName>
      </children>
    </TimeZone>
  </Elem1>
</Root>') 

declare @Node varchar(50)
set @Node = 'TimeZone'

select N.value('(children/DisplayName)[1]', 'varchar(100)') as Value
from @T as T
  cross apply T.XMLCol.nodes('//*[local-name()=sql:variable("@Node")]') as X(N)
Run Code Online (Sandbox Code Playgroud)

sql sql-server sql-server-2008 xquery-sql

6
推荐指数
1
解决办法
7953
查看次数

多个UI线程 - Winforms

我想在我的应用程序中创建多个UI线程.我已经模拟了如下方案.我正在后台线程中单击按钮创建一个新窗口/窗体

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var thread = new Thread(() =>
            {
                Form f = new Form();
                Application.Run(f);
            });

            // thread.IsBackground = true; -- Not required. See Solution below
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
    }  
}
Run Code Online (Sandbox Code Playgroud)

请注意 - 我正在做IsBackground = true因为当用户在主窗体上关闭时,子窗体/窗口也应该关闭.是否有更清洁/优雅的方式来实现同样的目标?

编辑 - 我想为每个窗口创建专用的UI线程.我将有10个这样的窗口并行显示实时数据.

解决方案 - 这样好吗?(根据msdnHans在下面评论)设置了公寓状态(见上面的代码)

protected override void OnClosed(EventArgs e)
{
    Application.Exit();
}
Run Code Online (Sandbox Code Playgroud)

c# user-interface multithreading winforms

5
推荐指数
1
解决办法
1万
查看次数

Powershell:执行政策

这样做我过去几个月一直在运行我的代码 -

Set-ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)

但是,一些奇怪的事情正在发生,我总是得到这个错误 -

Windows PowerShell已成功更新您的执行策略,但该设置被更具体范围内定义的策略覆盖.由于覆盖,您的shell将保留其当前有效的"Unrestricted"执行策略.键入"Get-ExecutionPolicy -List"以查看执行策略设置.有关详细信息,请参阅"Get-Help Set-ExecutionPolicy".

我已经提到这些链接但没有运气 -

Get-ExecutionPolicy -List

MachinePolicy                                                           
UserPolicy                                                           
Process                                                        
CurrentUser                                                        
LocalMachine
Run Code Online (Sandbox Code Playgroud)

c# powershell powershell-2.0

5
推荐指数
1
解决办法
3278
查看次数

Powershell v2 ::加载COM Interop DLL

我在我的系统上有这个DataLink DLL - Interop.MSDASC.dll我试图从像这样的Powershell加载相同的 -

[Reflection.Assembly]::LoadFile("C:\Interop.MSDASC.dll") | out-null
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误 -

Exception calling "LoadFile" with "1" argument(s): "Could not load file or assembly 'Interop.MSDASC.dll' or one of its dependencies.  is not a 
valid Win32 application. (Exception from HRESULT: 0x800700C1)"
At line:1 char:32
+ [Reflection.Assembly]::LoadFile <<<< ("C:\Interop.MSDASC.dll") | out-null
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Run Code Online (Sandbox Code Playgroud)

我该如何正确加载?

powershell powershell-2.0

3
推荐指数
1
解决办法
5511
查看次数

如何以编程方式将图像添加到资源中?

我在我使用Visual Studio添加的资源[.resx文件]中有一些图像文件.但是,现在我需要以编程方式将图像添加到资源中.

我怎么这样?

c# resources

3
推荐指数
1
解决办法
5047
查看次数

C# - 单元测试/模拟 - 遗留代码

我在十多年前的代码中有以下逻辑,我必须编写单元测试.它是一个具体的类,以下逻辑在于ctor.是否有一种很好的方法可以为这样的遗留代码编写单元测试/模拟.我正在使用MSTest/RhinoMocks框架和VS 2010 IDE与.Net framework 4.0

public class SomeClass
    {
        /// ctor
        public SomeClass(XmlNode node)
        {
            //Step 1: Initialise some private variable based on attributes values from the node

            //Step 2: Lot of If , else -if statements ---> something like - 

            if (/*attributeValue is something*/)
            {
                // Connect to Db, fetch  some value based on the attribute value. 
                // Again the logic of connecting and fetching is in another concrete class
            }
            else if (/*attributeValue is somthing else*/)
            {
                // …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing mstest rhino-mocks c#-4.0

2
推荐指数
1
解决办法
1655
查看次数