我需要将相同的字段更新为TFS中数百个工作项的相同值.是否有任何方法可以批量执行而不是逐个手动更新?
如果您启动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,如果它扩大了可能性.
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
我错过了什么?
测试脚本:
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) 请考虑以下代码:
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)
(忽略课堂设计;我实际上并没有写一个计算器!这段代码只是代表了一个需要一段时间才能缩小范围的更大问题的最小代表)
我希望它能:
相反,我得到了一个无意义的异常抛出指定的行.在3.0 CLR上,它是一个NullReferenceException ; 在Silverlight …
# 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) 标题有点说明了一切.通常的SOS命令!没有名字,bpmd没有很多好处.
我有一些想法:
Fiddler有许多有用的扩展.但是,我找不到理解WCF的二进制编码SOAP信封的人.(内容类型:application/soap + msbin1)
对类似(非Fiddler)工具的建议也是受欢迎的.
$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)
我希望能够键入快速,简单的命令来就地操作文件.例如:
# 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急切展开收藏的方式感到困惑,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) 我在这里讨论的问题基本相同: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
powershell ×8
c# ×2
pipeline ×2
.net ×1
64-bit ×1
api ×1
arrays ×1
asynchronous ×1
binary ×1
bug-tracking ×1
closures ×1
cmdlet ×1
collections ×1
constructor ×1
debugging ×1
expression ×1
fiddler ×1
file-io ×1
ienumerable ×1
lambda ×1
null ×1
registry ×1
silverlight ×1
soap ×1
sos ×1
string ×1
tfs ×1
wcf ×1
windbg ×1
xml ×1