无法识别Backup-SqlDatabase

时间:2018-03-19 09:39:28

标签: powershell backup-sqldatabase

我有一个PowerShell脚本来备份数据库。但今天它已停止处理下一个错误:

Backup-SqlDatabase : The term 'Backup-SqlDatabase' 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.

我没有更改脚本。可能是什么原因?

更新: 安装SqlServer模块。现在我有下一个:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Temp> import-module sqlserver -erroraction stop -verbose
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\sqlserver\21.0.17224\sqlserver.psd1'.
VERBOSE: Loading 'TypesToProcess' from path 'C:\Program
Files\WindowsPowerShell\Modules\sqlserver\21.0.17224\sqlprovider.types.ps1xml'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\Program
Files\WindowsPowerShell\Modules\sqlserver\21.0.17224\sqlprovider.format.ps1xml'.
VERBOSE: Populating RepositorySourceLocation property for module sqlserver.
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\sqlserver\21.0.17224\SqlServer.psm1'.
VERBOSE: Exporting function 'SQLSERVER:'.
VERBOSE: Exporting alias 'Encode-SqlName'.
VERBOSE: Exporting alias 'Decode-SqlName'.
VERBOSE: Importing function 'SQLSERVER:'.
VERBOSE: Importing alias 'Decode-SqlName'.
VERBOSE: Importing alias 'Encode-SqlName'.
PS C:\Temp> Get-Command -Name Backup-SqlDatabase
Get-Command : The term 'Backup-SqlDatabase' 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.
At line:1 char:1
+ Get-Command -Name Backup-SqlDatabase
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Backup-SqlDatabase:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

更新2:已卸载的SqlServer模块,目录已被删除。之后重新安装它。安装日志:

PS C:\WINDOWS\system32> Install-Module -Name SqlServer -Repository PSGallery -Verbose
VERBOSE: Repository details, Name = 'PSGallery', Location = 'https://www.powershellgallery.com/api/v2/'; IsTrusted
 = 'False'; IsRegistered = 'True'.
VERBOSE: Using the provider 'PowerShellGet' for searching packages.
VERBOSE: Using the specified source names : 'PSGallery'.
VERBOSE: Getting the provider object for the PackageManagement Provider 'NuGet'.
VERBOSE: The specified Location is 'https://www.powershellgallery.com/api/v2/' and PackageManagementProvider is
'NuGet'.
VERBOSE: Searching repository 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='SqlServer'' for ''.
VERBOSE: Total package yield:'1' for the specified package 'SqlServer'.
VERBOSE: Performing the operation "Install-Module" on target "Version '21.0.17224' of module 'SqlServer'".

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y
VERBOSE: The installation scope is specified to be 'AllUsers'.
VERBOSE: The specified module will be installed in 'C:\Program Files\WindowsPowerShell\Modules'.
VERBOSE: The specified Location is 'NuGet' and PackageManagementProvider is 'NuGet'.
VERBOSE: Downloading module 'SqlServer' with version '21.0.17224' from the repository
'https://www.powershellgallery.com/api/v2/'.
VERBOSE: Searching repository 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='SqlServer'' for ''.
VERBOSE: InstallPackage' - name='SqlServer',
version='21.0.17224',destination='C:\Users\Oleg\AppData\Local\Temp\1981035148'
VERBOSE: DownloadPackage' - name='SqlServer',
version='21.0.17224',destination='C:\Users\Oleg\AppData\Local\Temp\1981035148\SqlServer\SqlServer.nupkg',
uri='https://www.powershellgallery.com/api/v2/package/SqlServer/21.0.17224'
VERBOSE: Downloading 'https://www.powershellgallery.com/api/v2/package/SqlServer/21.0.17224'.
VERBOSE: Completed downloading 'https://www.powershellgallery.com/api/v2/package/SqlServer/21.0.17224'.
VERBOSE: Completed downloading 'SqlServer'.
VERBOSE: Hash for package 'SqlServer' does not match hash provided from the server.
VERBOSE: InstallPackageLocal' - name='SqlServer',
version='21.0.17224',destination='C:\Users\Oleg\AppData\Local\Temp\1981035148'
VERBOSE: Catalog file 'SqlServer.cat' is not found in the contents of the module 'SqlServer' being installed.
VERBOSE: Valid authenticode signature found in the file 'SqlServer.psd1' for the module 'SqlServer'.
VERBOSE: Module 'SqlServer' was installed successfully to path 'C:\Program
Files\WindowsPowerShell\Modules\SqlServer\21.0.17224'.

即使在Import-Module SqlServer -ErrorAction Stop -Verbose没有改变且Backup-SqlDatabase仍然无法使用之后。可能是什么原因?

3 个答案:

答案 0 :(得分:1)

该功能由sqlps(旧的和已破坏的)和sqlserver(当前)模块提供。 sqlpssqlserver的旧版本由SQL Server Management Studio安装提供,但sqlserver现在位于PowerShell库中。假设您拥有当前版本的PowerShell / Windows Management Framework,您可以install-module sqlserver(在管理员PowerShell会话中运行)并获取全局安装的最新版本。

关于你的剧本发生了什么:

可能性#1:您使用的是非常旧版本的PowerShell,它不会自动加载模块,而且您没有明确导入sqlserversqlps模块进入会话/脚本,您将调用此函数。解决方案:升级到当前版本的PowerShell,它支持自动加载模块和/或使用import-module将正确的模块明确导入到脚本/会话中。

可能性#2:有人卸下或移动了您正在从中获取该功能的模块,或者它不在您的模块搜索路径中。解决方案:检查$PSModulePath,然后查看模块的每个位置。最简单的方法是在全球范围内重新安装它。

答案 1 :(得分:0)

您需要导入SQL Server PowerShell模块才能访问其包含的cmdlet:

Import-Module SQLPS -ErrorAction Stop

运行此代码以查看该功能是否可用:

Get-Command -Name Backup-SqlDatabase

以下是我的机器的结果:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Backup-SqlDatabase                                 14.0       SQLPS

答案 2 :(得分:0)

我也有同样的问题,但使用 SQLPS 的 Delete-SqlDatabase

就我而言,我试图调用我在代码中声明和使用的函数 Delete-SqlDatabase

我犯的错误是调用了下面的函数。

您在 Powershell 中看到为了使函数可见,您必须在顶部声明它。 main 函数应该是最后一个部分的层次结构。

这是一件多么愚蠢的事情。我相信你会在一天之内解决这个问题。

我 100% 确定这是您的问题。我知道这个帖子太旧了,但它可能会帮助像我这样可以节省一个小时时间的其他人。