【问题标题】:Backup-SqlDatabase is not recognized无法识别 Backup-SqlDatabase
【发布时间】:2018-03-19 09:39:27
【问题描述】:

我有一个 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 仍然不可用。可能是什么原因?

【问题讨论】:

  • 你的版本是什么?操作系统、PowerShell 和 SQL Server。

标签: powershell backup-sqldatabase


【解决方案1】:

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

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

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

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

【讨论】:

  • 感谢重播。我正在使用 PS 5.1.16299。我已经按照docs.microsoft.com/en-us/sql/powershell/… 中的说明安装了 SqlServer,我正在显式导入 SqlServer 模块(没有任何错误),但仍然无法识别“Backup-SqlDatabase”。现在可能是什么原因? sqlps 会不会和 SqlServer 模块冲突?
  • 你可以同时安装这两个,你可能需要做额外的工作来运行一个与另一个。你从import-module sqlserver -erroraction stop -verboseget-command -name backup-sqldatabase 得到什么?
  • Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\sqlserver\21.0.17224\sqlserver.psd1'. Loading 'TypesToProcess' from path '...\sqlprovider.types.ps1xml'. Loading 'FormatsToProcess' from path '...\sqlprovider.format.ps1xml'. Populating RepositorySourceLocation property for module sqlserver. Loading module from path '...\SqlServer.psm1'. Exporting function 'SQLSERVER:'. Exporting alias 'Encode-SqlName'. Exporting alias 'Decode-SqlName'. Importing function 'SQLSERVER:'. Importing alias 'Decode-SqlName'. Importing alias 'Encode-SqlName'.
  • ... 是-C:\Program Files\WindowsPowerShell\Modules\sqlserver\21.0.17224\
  • 您不能将完整的输出塞进评论中。有任何错误吗?将它们放在您的原始帖子中
【解决方案2】:

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

Import-Module SQLPS -ErrorAction Stop

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

Get-Command -Name Backup-SqlDatabase

这是我机器上的结果:

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

【讨论】:

  • 当我运行 Import-Module 命令时失败并显示下一条消息:Set-Location : Cannot find drive. A drive with the name 'SQLSERVER' does not exist. At C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\SqlPsPostScript.ps1:1 char:1 + Set-Location SQLSERVER: + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (SQLSERVER:String) [Set-Location], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
  • @OlegOshkoderov 所以基本上模块由于错误无法导入;这就是您无法使用 cmdlet 的原因。尝试重新安装模块。
  • sqlps 已死,已被sqlserver 取代(可在 PS 库中找到)。请不要鼓励人们使用那个旧的、被破坏的模块。
  • 我已经安装了sqlserver 模块,并导入了它,但是Backup-SqlDatabase 仍然无法识别。可能是什么原因?
【解决方案3】:

我也有同样的问题,但来自 SQLPS 的 Delete-SqlDatabase

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

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

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

真是个傻事。我相信你会在一天之内解决这个问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2021-11-15
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多