小编Ric*_*erg的帖子

如何在TFS中批量更新多个工作项

我需要将相同的字段更新为TFS中数百个工作项的相同值.是否有任何方法可以批量执行而不是逐个手动更新?

api powershell tfs bug-tracking

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

如何从32位Powershell实例访问64位注册表?

如果您启动32位Powershell实例(%SystemRoot%\ syswow64\WindowsPowerShell\v1.0\powershell.exe),则注册表提供程序只能看到注册表的有限32位部分.

**32-bit console**
PS> (dir HKLM:\SOFTWARE | measure).count - (dir HKLM:\SOFTWARE\wow6432node | measure).count

0

**64-bit console**
PS> (dir HKLM:\SOFTWARE | measure).count - (dir HKLM:\SOFTWARE\wow6432node | measure).count

-5
Run Code Online (Sandbox Code Playgroud)

有没有办法强制提供程序进入64位模式?我可以下载到[Microsoft.Win32] .Net API,或者WMI,但我不愿意.我正在使用Powershell v2 CTP3,如果它扩大了可能性.

registry powershell 64-bit

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

可以将Powershell中的null传递给需要字符串的.Net API吗?

API:

namespace ClassLibrary1
{
    public class Class1
    {
        public static string Test(string input)
        {
            if (input == null)
                return "It's null";
            if (input == string.Empty)
                return "It's empty";
            else
                return "Non-empty string of length " + input.Length;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

脚本:

add-type -path C:\temp\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll
[classlibrary1.class1]::Test($null)
[classlibrary1.class1]::Test([object]$null)
[classlibrary1.class1]::Test([psobject]$null)
[classlibrary1.class1]::Test($dummyVar)
[classlibrary1.class1]::Test($profile.dummyProperty)
Run Code Online (Sandbox Code Playgroud)

输出:

It's empty
It's empty
It's empty
It's empty
It's empty

我错过了什么?

.net string powershell null

13
推荐指数
2
解决办法
3086
查看次数

Powershell脚本:在函数调用嵌套时实现ShouldProcess的推荐方法?

测试脚本:

function outer
{
    [cmdletbinding(supportsshouldprocess=$true)]
    param($s)

    process
    {        
        $pscmdlet.shouldprocess("outer $s", "ShouldProcess") | out-null
        "" | out-file "outer $s"

        inner ImplicitPassthru
        inner VerbosePassthru -Verbose:$Verbose 
        inner WhatifPassthru -WhatIf:$WhatIf
    }
}

function inner
{
    [cmdletbinding(supportsshouldprocess=$true)]
    param($s)

    process
    {   
        $pscmdlet.shouldprocess("inner $s", "ShouldProcess") | out-null
        "" | out-file "inner $s"
    }
}

"`n** NORMAL **"
outer normal
"`n** VERBOSE **"
outer verbose -Verbose
"`n** WHATIF **"
outer whatif -WhatIf
Run Code Online (Sandbox Code Playgroud)

输出:

** NORMAL **
VERBOSE: Performing operation "ShouldProcess" on Target "inner VerbosePassthru".
What if: Performing operation …
Run Code Online (Sandbox Code Playgroud)

error-handling powershell design-patterns cmdlet

12
推荐指数
1
解决办法
6875
查看次数

C# - 关闭初始化程序中的类字段?

请考虑以下代码:

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var square = new Square(4);
            Console.WriteLine(square.Calculate());
        }
    }

    class MathOp
    {        
        protected MathOp(Func<int> calc) { _calc = calc; }
        public int Calculate() { return _calc(); }
        private Func<int> _calc;
    }

    class Square : MathOp
    {
        public Square(int operand)
            : base(() => _operand * _operand)  // runtime exception
        {
            _operand = operand;
        }

        private int _operand;
    }
}
Run Code Online (Sandbox Code Playgroud)

(忽略课堂设计;我实际上并没有写一个计算器!这段代码只是代表了一个需要一段时间才能缩小范围的更大问题的最小代表)

我希望它能:

  • 打印"16",或
  • 如果在此方案中不允许关闭成员字段,则抛出编译时错误

相反,我得到了一个无意义的异常抛出指定的行.在3.0 CLR上,它是一个NullReferenceException ; 在Silverlight …

c# lambda constructor closures initialization

12
推荐指数
2
解决办法
1013
查看次数

是什么决定了Powershell管道是否会展开一个集合?

# array
C:\> (1,2,3).count
3
C:\> (1,2,3 | measure).count
3

# hashtable
C:\> @{1=1; 2=2; 3=3}.count
3
C:\> (@{1=1; 2=2; 3=3} | measure).count
1

# array returned from function
C:\> function UnrollMe { $args }
C:\> (UnrollMe a,b,c).count
3
C:\> (UnrollMe a,b,c | measure).count
1
C:\> (1,2,3).gettype() -eq (UnrollMe a,b,c).gettype()
True
Run Code Online (Sandbox Code Playgroud)

与HashTables的差异是众所周知的,尽管官方文档仅提到它(通过示例).

但是,功能问题对我来说是个新闻.我有点震惊,现在还没有咬过我.我们编程人员可以遵循一些指导原则吗?我知道在C#中编写cmdlet时会出现WriteObject重载,您可以在其中明确地控制枚举,但是AFAIK在Posh语言本身中没有这样的构造.正如最后一个例子所示,Posh解释器似乎相信被管道对象的类型没有区别.我怀疑在引擎盖下可能会有一些Object vs PSObject的怪异,但是当你编写纯粹的Posh并期望脚本语言"正常工作"时,这没什么用处.

/编辑/

基思是正确的指出,在我的例子中,我传入一个字符串[]参数而不是3个字符串参数.换句话说,Measure-Object说Count = 1的原因是因为它看到的是一个数组,其第一个元素是@("a","b","c").很公平.这些知识允许您以多种方式解决问题:

# stick to single objects
C:\> (UnrollMe a b c | measure).count
3 …
Run Code Online (Sandbox Code Playgroud)

collections powershell ienumerable pipeline

9
推荐指数
1
解决办法
2874
查看次数

如何在匿名方法中打破WinDbg?

标题有点说明了一切.通常的SOS命令!没有名字,bpmd没有很多好处.

我有一些想法:

  • 转储每个方法,然后在找到相应的MethodDesc时 使用!bpmd -md
    • 从我所知道的,在现实世界的使用中不实用.即使我写了一个宏来限制转储到匿名类型/方法,也没有明显的方法来区分它们.
  • 使用Reflector转储MSIL名称
    • 在处理动态程序集和/或Reflection.Emit时没有帮助.Visual Studio无法在这种情况下阅读本地变量,这是我首先转向Windbg的全部原因......
  • 在VS中设置断点,等待它击中,然后使用非侵入性技巧更改为Windbg
    • 试图从VS分离导致它挂起(与应用程序一起).我认为这是因为托管调试器是通过线程注入而不是标准"硬"调试器的"软"调试器.或者它可能只是Silverlight特有的VS错误(几乎不是我遇到过的第一个).
  • 在已知调用匿名方法的其他位置设置断点,然后单步进入
    • 我的备份计划,但如果这个Q&A揭示了一个更好的方法,我宁愿不诉诸它

c# debugging windbg anonymous-methods sos

8
推荐指数
1
解决办法
1382
查看次数

是否有二进制XML的Fiddler插件?

Fiddler有许多有用的扩展.但是,我找不到理解WCF的二进制编码SOAP信封的人.(内容类型:application/soap + msbin1)

对类似(非Fiddler)工具的建议也是受欢迎的.

xml binary wcf soap fiddler

5
推荐指数
2
解决办法
3705
查看次数

为什么Powershell的"return"关键字会导致类型错误?

$xml = [xml] '<node>foo</node>'
function foo2 { return "foo2" }

# all of these fail with the message:
# **Cannot set "foo" because only strings can be used as values to set XmlNode properties.**
$xml.node = foo2
$xml.node = foo2 -as [string]  # because of this issue[1]
$xml.node = (foo2)  

# these work
$xml.node = (foo2).tostring()
$xml.node = (foo2) -as [string]
$xml.node = [string] (foo2)

# yet, these two statements return the same value
(foo2).gettype()
(foo2).tostring().gettype()
Run Code Online (Sandbox Code Playgroud)

1:PowerShell函数返回行为

powershell

4
推荐指数
1
解决办法
1779
查看次数

Powershell:您如何在一个管道中读写I/O?

我希望能够键入快速,简单的命令来就地操作文件.例如:

# prettify an XML file
format-xml foo | out-file foo
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为管道设计为"贪婪".一旦上游cmdlet处理第一行输入,下游cmdlet就会获取对文件的写锁定,这会阻止上游cmdlet读取文件的其余部分.

有许多可能的解决方法:写入临时文件,将操作分成多个管道(将中间结果存储在变量中)或类似.但我认为这是一个非常普遍的任务,有人开发了一个快速,易于使用的快捷方式.

我试过这个:

function Buffer-Object 
{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$True, ValueFromPipeline=$True)]
        [psobject] $InputObject
    )

    begin { $buf = new-list psobject }
    process { $buf.Add($InputObject) }
    end { $buf }
}
format-xml foo | buffer-object | out-file foo
Run Code Online (Sandbox Code Playgroud)

在某些情况下它可以正常工作.映射到一个简短的别名并进入像PSCX这样的通用发行版,它对于快速交互式任务来说"足够好".不幸的是,似乎有些cmdlet(包括out-file)在Begin {}方法而不是Process {}中获取锁,因此它不能解决这个特定的例子.

其他想法?

powershell file-io pipeline

4
推荐指数
1
解决办法
2970
查看次数

PowerShell的-f运算符的RHS如何*完全*?

上次我PowerShell急切展开收藏的方式感到困惑,Keith总结了它的启发式:

将结果(数组)放在分组表达式(或子表达式,例如$())中,使其再次符合展开条件.

我已经把这个建议铭记于心,但仍然发现自己无法解释一些esoterica.特别是,格式运算符似乎不符合规则.

$lhs = "{0} {1}"

filter Identity { $_ }
filter Square { ($_, $_) }
filter Wrap { (,$_) }
filter SquareAndWrap { (,($_, $_)) }

$rhs = "a" | Square        
# 1. all succeed
$lhs -f $rhs
$lhs -f ($rhs)
$lhs -f $($rhs)
$lhs -f @($rhs)

$rhs = "a" | Square | Wrap       
# 2. all succeed
$lhs -f $rhs
$lhs -f ($rhs)
$lhs -f $($rhs)
$lhs -f @($rhs)

$rhs = …
Run Code Online (Sandbox Code Playgroud)

arrays powershell expression operator-precedence

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

如何在不在UI线程上获取silverlight依赖属性?

我在这里讨论的问题基本相同:http://khason.net/blog/dependency-property-getters-and-setters-in-multithreaded-environment/

public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.RegisterAttached("MyProperty", typeof(bool),
        typeof(MyObject), new PropertyMetadata(new PropertyChangedCallback(OnMyPropertyChanged)));

public static bool GetMyProperty(DependencyObject obj)
{
    return (bool)obj.GetValue(MyPropertyProperty);     <<<<<
}

public static void SetMyProperty(DependencyObject obj, bool value)
{
    obj.SetValue(MyPropertyProperty, value);
}
Run Code Online (Sandbox Code Playgroud)

如果标记为"<<<<<"的行从后台线程调用,Silverlight会抛出InvalidOperationException,我的应用程序可能会死锁.

遗憾的是,博客文章中的解决方案无效,因为Dispatcher类的Silverlight版本隐藏了同步的Invoke方法 - 只有BeginInvoke被标记为public.

silverlight multithreading asynchronous dependency-properties

0
推荐指数
1
解决办法
631
查看次数