参考此链接,我试图在我的脚本中引入详细模式.
当我有一个像这样定义的函数时 -
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:在目标系统上安装模块
Cannot find path 'C:\Users\angshuman\Documents\WindowsPowerShell\Modules\MyPSModules\MyPsModules.??psd1' because it does not exist. 我在Windows 7 64位操作系统上通过C#执行相同的代码
_ps = PowerShell.Create();
_ps.AddScript("Import-Module MyPSModules -PassThru");
Collection<PSObject> psObjects = _ps.Invoke();
Run Code Online (Sandbox Code Playgroud) 我想组织功能集成到多个名为.psm1文件,并让他们通过加载一个模块清单文件(的.psd1) -使得只有在的.psd1文件就需要有相同的名称作为模块.
我认为这应该是可能的.有人可以帮帮我吗?
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.
编辑 - 为了完整性,再次,这将无法编译 …
下面的查询试图选择给定节点的子节点.如何使用变量而不是硬编码子节点,以便我可以将它们作为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) 我想在我的应用程序中创建多个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个这样的窗口并行显示实时数据.
解决方案 - 这样好吗?(根据msdn和Hans在下面的评论)设置了公寓状态(见上面的代码)
protected override void OnClosed(EventArgs e)
{
Application.Exit();
}
Run Code Online (Sandbox Code Playgroud) 这样做我过去几个月一直在运行我的代码 -
Set-ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)
但是,一些奇怪的事情正在发生,我总是得到这个错误 -
Windows PowerShell已成功更新您的执行策略,但该设置被更具体范围内定义的策略覆盖.由于覆盖,您的shell将保留其当前有效的"Unrestricted"执行策略.键入"Get-ExecutionPolicy -List"以查看执行策略设置.有关详细信息,请参阅"Get-Help Set-ExecutionPolicy".
我已经提到这些链接但没有运气 -
http://blog.whatsupduck.net/2010/09/issues-with-configuring-powershell.html
http://www.howtogeek.com/106273/how-to-allow-the-execution-of-powershell-scripts-on-windows-7/
我使用.Net 4.0通过32位Winforms应用程序在64位Windows 7上使用C#加载Powershell
我需要做的确切设置是什么,以至于我在任何系统上都没有出现此错误?[一切都很好].这些值应该在任何系统上应该是什么,以便我能够无缝地从C#调用Powershell -
Get-ExecutionPolicy -List
Run Code Online (Sandbox Code Playgroud)MachinePolicy UserPolicy Process CurrentUser LocalMachine
我在我的系统上有这个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)
我该如何正确加载?
我在我使用Visual Studio添加的资源[.resx文件]中有一些图像文件.但是,现在我需要以编程方式将图像添加到资源中.
我怎么这样?
我在十多年前的代码中有以下逻辑,我必须编写单元测试.它是一个具体的类,以下逻辑在于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# ×5
powershell ×5
c#-4.0 ×1
mstest ×1
resources ×1
rhino-mocks ×1
sql ×1
sql-server ×1
unit-testing ×1
winforms ×1
xquery-sql ×1