I found a script in another post in StackOverflow which appears like it will do exactly what I need, however, I'm getting an error when attempting to execute the .ps1 file type from PowerShell within SSMS.
The environment is SQL Server 2014.
My FindBrokenObjectsInSQLServer.ps1 file is defined as:
$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
}
}
}
}
The command I'm executing is:
PS SQLSERVER:\SQL\APPDEV2014\DEFAULT> PowerShell -command
"H:\SQLScripts\FindBrokenObjectsInSQLServer.ps1"
Finally the error I'm getting is:
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
Any suggestions/direction would be appreciated. This is my first attempt at working with PowerShell. Thanks.
PSat the beginning of the prompt), so you don't need to typePowerShell -command .... Just type the name of the script you want to run, followed by its parameters, and pressEnter.