我有一个脚本,我利用函数来包装部分代码,允许我在指定的点移动部分.我发现我必须在脚本中首先列出函数才能正确运行.
$stepChoice = read-host 'Where would you like to start.'
switch($stepChoice)
{
1{Step1}
2{Step2}
3{Step3}
}
# Steps.ps1
function Step1 {
'Step 1'
Step2
}
function Step2 {
'Step 2'
Step3
}
function Step3 {
'Step 3'
'Done!'
}
Run Code Online (Sandbox Code Playgroud)
这给我以下错误:
术语"Step1"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
Run Code Online (Sandbox Code Playgroud)At C:\Tools\Scripts\functiontest.ps1:7 char:12 + 1{Step1 <<<< } + CategoryInfo : ObjectNotFound: (Step1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException*
如果我改变它的顺序它工作正常:
# Steps.ps1
function Step1 {
'Step 1'
Step2
}
function Step2 {
'Step 2'
Step3
}
function …Run Code Online (Sandbox Code Playgroud) 当我从脚本运行下面的行时,文件最终会在我的本地计算机上创建.
$cred = Get-Credential domain\DanTest
Enter-PSSession -computerName xsappb01 -credential $cred
New-Item -type file c:\temp\blahxsappk02.txt
exit-pssession
Run Code Online (Sandbox Code Playgroud)
当我从powershell控制台单独运行每一行时,将正确创建远程会话,并在远程计算机上创建该文件.有什么想法吗?时间问题是脚本也许吗?
我们的测试和开发环境中存在一个问题,其功能在从.Net应用程序调用时运行得非常慢.当我们直接从管理工作室调用此函数时,它工作正常.
以下是它们分析时的差异:来自应用程序:
CPU:906
读取:61853
写入:0
持续时间:926
从SSMS:
CPU:15
阅读:11243写道
:0
持续时间:31
现在我们已经确定,当我们重新编译该函数时,性能将返回到我们期望的结果,并且从应用程序运行时的性能配置文件与从SSMS运行时获得的性能配置文件相匹配.它会以随机间隔的形式再次开始减速.
我们还没有在生产中看到这一点,但它们可能部分是因为每周都会重新编译所有内容.
那么什么可能导致这种行为?
编辑 -
我们终于能够解决这个问题并重组变量来处理参数嗅探似乎已经完成了诀窍......我们在这里做了一个片段:感谢您的帮助.
-- create set of local variables for input parameters - this is to help performance - vis a vis "parameter sniffing"
declare @dtDate_Local datetime
,@vcPriceType_Local varchar(10)
,@iTradingStrategyID_Local int
,@iAccountID_Local int
,@vcSymbol_Local varchar(10)
,@vcTradeSymbol_Local varchar(10)
,@iDerivativeSymbolID_Local int
,@bExcludeZeroPriceTrades_Local bit
declare @dtMaxAggregatedDate smalldatetime
,@iSymbolID int
,@iDerivativePriceTypeID int
select @dtDate_Local = @dtDate
,@vcPriceType_Local = @vcPriceType
,@iTradingStrategyID_Local = @iTradingStrategyID
,@iAccountID_Local = @iAccountID
,@vcSymbol_Local = @vcSymbol
,@vcTradeSymbol_Local = …Run Code Online (Sandbox Code Playgroud) 我有一个PowerShell脚本,我希望能够定义不同的起点.一旦命中起点,脚本就会从该点开始接收并继续执行脚本中的其余代码.我不相信case语句会起作用,因为我认为不会让脚本从任何起点定义出来.
我希望在脚本启动时会看到类似的内容.
请选择您的起点:
选择完成后,脚本跳转到该点,然后将运行脚本的其余部分.
答:代码最终会看起来像这样:
#steps
$stepChoice = read-host 'Where would you like to start.'
switch($stepChoice)
{
1{Step1}
2{Step2}
3{Step3}
}
function Step1 {
'Step 1'
Step2
}
function Step2 {
'Step 2'
Step3
}
function Step3 {
'Step 3'
'Done!'
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我无法让这段代码工作.
DataTable dt = DataManager.GetSectionIdByEmail(test2PortalConnectionString, email);
Dictionary<int,int> clientViewIds = dt.Select(p => new {p.Key, p.Value })
.AsEnumerable()
.ToDictionary(kvp => kvp.key as int, kvp => kvp.Value as int);
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:无法将lambda表达式转换为类型'string',因为它不是委托类型
解决方法:我在语句中的AsEnumberable()处于错误的位置,我需要处理数据行.
Dictionary<int,int> clientViewIds = dt.AsEnumerable()
.Select(dr => new { Key = dr["SectionID"], Value = dr["SectionTypeID"] })
.ToDictionary(kvp =>(int)kvp.Key, kvp => (int)kvp.Value);
Run Code Online (Sandbox Code Playgroud)