我曾经不得不做很多重复的打字工作,包括为
数据库中的每个表。所以我开发了一个通用工具,它适合这种类型的工作。我按原样包括该工具,以及该工具的示例运行,旨在
为每个表和每个数据库用户类别生成一系列授权命令。
我的工具使用 CSV 文件而不是直接使用数据库。我发现为许多不同的任务生成 CSV 文件和模板相当容易。你的旅费可能会改变。也许您可以从这个工具入手并根据您的需要进行调整。
这是工具,以及运行示例。
<#
.SYNOPSIS
Generates multiple expansions of a template,
driven by data in a CSV file.
.DESCRIPTION
This function is a table driven template tool.
It generates output from a template and
a driver table. The template file contains plain
text and embedded variables. The driver table
(in a csv file) has one column for each variable,
and one row for each expansion to be generated.
#>
function Expand-csv {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string] $driver,
[Parameter(Mandatory=$true)]
[string] $template
)
Process
{
$xp = (Get-Content $template) -join "`r`n"
Import-Csv $driver | % {
$_.psobject.properties | % {Set-variable -name $_.name -value $_.value}
$ExecutionContext.InvokeCommand.ExpandString($xp)
}
}
}
# Now do a sample run of Expand-csv
# then display inputs and output
Expand-csv grants.csv grants.tmplt > grants.sql
get-content grants.tmplt
import-csv grants.csv
get-content grants.sql
这是上面运行的结果:
PS> C:\Users\David\Software\Powershell\test\sample.ps1
grant $privs
on $table
to $user;
privs table user
----- ----- ----
ALL Employees DBA
READ Employees Analyst
READ, WRITE Employees Application
ALL Departments DBA
READ Departments Analyst, Application
grant ALL
on Employees
to DBA;
grant READ
on Employees
to Analyst;
grant READ, WRITE
on Employees
to Application;
grant ALL
on Departments
to DBA;
grant READ
on Departments
to Analyst, Application;
PS>
在现实生活中,我在我的 $profile 文件中定义了该工具,因此只要我在 powershell 中就可以使用它。
我不确定这是否适用于您的情况。这并不能解决您描述的确切情况,但您可以调整该技术。