StackOverflowの別の投稿にスクリプトがありますが、必要なものと同じように見えますが、.ps1ファイルの種類を実行しようとするとエラーが表示されますSSMS内のPowerShellPowershellからSSMS内の.PS1ファイルを実行できません
$server = "APPDEV2014"
cls
Import-Module "sqlps" -DisableNameChecking
$databases = Invoke-Sqlcmd -Query "select name from sys.databases where name not in ('master', 'tempdb', 'model', 'msdb')" -ServerInstance $server
foreach ($db in $databases) {
$dbName = $db.name
$procedures = Invoke-Sqlcmd -Query "select SCHEMA_NAME(schema_id) as [schema], name from $dbName.sys.procedures" -ServerInstance $server
foreach ($proc in $procedures) {
if ($schema) {
$shortName = $proc.schema + "." + $proc.name
$procName = $db.name + "." + $shortName
try {
$result = Invoke-Sqlcmd -Database $dbName -Query "sys.sp_refreshsqlmodule '$shortName'" -ServerInstance $server -ErrorAction Stop
Write-Host "SUCCESS|$procName"
}
catch {
$msg = $_.Exception.Message.Replace([Environment]::NewLine, ",")
Write-Host "FAILED|$procName|$msg" -ForegroundColor Yellow
}
}
}
}
コマンド私は実行していますされています:
環境は、2014年
マイFindBrokenObjectsInSQLServer.ps1ファイルは、次のように定義されたSQL Serverです
PS SQLSERVER:\SQL\APPDEV2014\DEFAULT> PowerShell -command
"H:\SQLScripts\FindBrokenObjectsInSQLServer.ps1"
最後にエラーI取得する:
Import-Module : A positional parameter cannot be found that accepts argument
'?'.
At H:\SQLScripts\FindBrokenObjectsInSQLServer.ps1:3 char:1
+ Import-Module "sqlps"? -DisableNameChecking
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Import-Module], ParameterB
indingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
.Commands.ImportModuleCommand
ご提案/ご指摘ありがとうございます。これがPowerShellを使って作業する最初の試みです。ありがとう。
すでにPowerShellのプロンプトが表示されています(プロンプトの冒頭にある「PS」を参照)ので、 'PowerShell -command ... 'と入力する必要はありません。実行したいスクリプトの名前を入力し、パラメータを入力してEnterキーを押してください。 –
ありがとう、ビル。応答を感謝します。 – Melinda