我以前没有使用Powershell的经验。我正在尝试使用我从this回答
修改过的脚本#call the sp_BlitzIndex procedure and specify the names and data types of the results;
#save the result set in the $BlitzIndex variable
$BlitzIndex = Invoke-Sqlcmd -ServerInstance $SQLInstance -Database DBAdmin -Query "EXEC ('exec ozar.sp_BlitzIndex @GetAllDatabases=1, @BringThePain=1, @Mode=0')
WITH RESULT SETS
(
(
[Priority] int
,[Finding] nvarchar(max)
,[DatabaseName] nvarchar(max)
,[Details] nvarchar(max)
,[Definition] nvarchar(max)
,[SecretColumns] nvarchar(max)
,[Usage] nvarchar(max)
,[Size] nvarchar(max)
,[Info] nvarchar(max)
,[URL] nvarchar(max)
,[CreateStatement] nvarchar(max)
)
)"
#Query the data that we want out of our $BlitzIndex result set, and set the $BlitzIndex result set to just contain that data
$BlitzIndex = $BlitzIndex | SELECT Priority,Finding,DatabaseName,Details,Definition,SecretColumns,Usage,Size,Info,URL | Where-Object {$_.Priority -gt 0} | Sort-Object Priority,Finding
#Loop through our desired results
foreach ($i in $BlitzIndex) {
#we have to set these variables to keep the PowerShell from erroring when encountering sub-expression statements
$Priority = $i.Priority
$Finding = $i.Finding
$DatabaseName = $i.DatabaseName
$Details = $i.Details
$Definition = $i.Definition
$SecretColumns = $i.SecretColumns
$Usage = $i.Usage
$Size = $i.Size
$Info = $i.Info
$URL = $i.URL
$CreateStatement = $i.CreateStatement
#Insert our results into our table
Invoke-Sqlcmd -ServerInstance $SQLInstance -Database DBAdmin -Query "INSERT INTO dbo.BlitzIndexResults (Priority,Finding,DatabaseName,Details,Definition,SecretColumns,Usage,Size,Info,URL,CreateStatement) VALUES ($Priority,'$Finding','$DatabaseName','$Details','$Definition','$SecretColumns','$Usage','$Size','$Info','$URL','$CreateStatement')"
}
表的定义:
create table [dbo].[BlitzIndexResults](
[PK_IndexResultsID] [int] identity(1,1) not null,
[Priority] [int] not null,
[Finding] [nvarchar](max) not null,
[DatabaseName] [nvarchar](max) not null,
[Details] [nvarchar](max) not null,
[Definition] [nvarchar](max) not null,
[SecretColumns] [nvarchar](max) not null,
[Usage] [nvarchar](max) not null,
[Size] [nvarchar](max) not null,
[Info] [nvarchar](max) not null,
[URL] [nvarchar](max) not null,
[CreateStatement] [nvarchar](max) not null,
[CaptureDate] [datetime] not null,
primary key clustered
(
[PK_IndexResultsID] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [PRIMARY]
) on [PRIMARY] textimage_on [PRIMARY]
go
alter table [dbo].[BlitzIndexResults] add default (getdate()) for [CaptureDate]
go
我将此作为SQL代理作业的一部分,并抛出此错误:
作业步骤在PowerShell脚本的第39行收到错误。该 对应的行是'Invoke-Sqlcmd -ServerInstance $ SQLInstance -Database DBAdmin -Query“INSERT INTO dbo.BlitzIndexResults(PriorityFindingDatabaseNameDetailsDefinitionSecretColumnsUsageSizeInfoURLCreateStatement) VALUES ($优先 '$查找' '$数据库名称' '$详细信息' '$定义' '$ SecretColumns' '$使用' '$大小' '$信息' '$ URL' '$的createStatement')” ”。更正脚本并重新安排作业。
我很确定我错过了一些小事,我找不到它。
答案 0 :(得分:0)
我想到的问题是,在Info,URL和CreateStatement数据的某个地方,它通过让它认为它插入更多数据来打破PowerShell。删除这三列就可以解决问题。
要添加,最好使用预准备语句使用sql插入数据。