更新:修改脚本以按照 SQLPS 的要求在 PS1 范围内工作。
更改:
IF($property.Value -match $regex){
$currentBadLine = (ConvertTo-Csv $_ -NoTypeInformation -Delimiter $delimiter);
$badLines += $currentBadLine[1,2]
};
收件人:
IF($property.Value -match $regex){
$badLines += $_ | Select-Object | ft -autoSize;
};
为每条错误的行打印一个新的标题,但这不是世界末日,不值得努力去阻止。
我有一个 Powershell 脚本,可以在 CSV 文件有机会搞砸我的数据导入之前对其进行预处理。
现在我已经连续在两台服务器上确认 PS 主版本至少为 2,并且以下代码片段在 Powershell ISE 中运行良好。该代码的目的是读取 CSV 的每一行,然后遍历列以在 $regex 变量中查找正则表达式模式。当它找到一个错误时,我希望它在修复错误之前跟踪错误,这样我可以稍后在输出一个准备导入的清理文件之前写一个错误日志。
%{
Foreach($Property in $_.PSObject.Properties){
IF($property.Value -match $regex){
$currentBadLine = (ConvertTo-Csv $_ -NoTypeInformation -Delimiter $delimiter);
$badLines += $currentBadLine[1,2]
};
$property.Value = $property.Value -replace $regex;
}
$_
}
但是一旦我将该代码放入代理作业中,代理就会提示:
'The term 'ConvertTo-Csv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. '
接下来的问题是:Agents Powershell 子系统是否使用与系统其余部分不同版本的 Powershell?如果是这样,我如何找出子系统正在使用哪个版本,如果可能的话升级它以便我可以解决这个问题。
服务器正在运行:
Windows Server 2008 R2 企业版
PS Major version 2, Minor 0 Build -1 revision -1
SQL Server 2008 R2 SP1 10.5.2500.0 64 位
谢谢!
最佳答案
是的,正确的 PowerShell 支持直到 SQL Server 2012 才真正实现(即使它支持哪些 cmdlet 也有点古怪)
在 2008 年和 R2 中,代理的 powershell 实现实际上是一个 minishell,由现在(谢天谢地)弃用的 make-shell.exe 实用程序创建,它只允许运行 v1 cmdlet,并且不允许使用 Add-PSSnapin,因此您可以'再添加 cmdlet。
要获得适当的 powershell 支持,您要么需要 shell out 并调用外部脚本,要么将作业作为 Windows 计划任务而不是代理作业运行。
下面的文章解释了为什么 2008 R2 中的 powershell 支持没有像您认为的那样工作:
关于sql-server - SQL Server 代理作业的 Powershell 无法识别 ConvertTo-CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19957788/