我尝试使用Restore-SqlDatabase cmdlet还原数据库.我需要重新定位文件,但我得到以下错误
Restore-SqlDatabase : Cannot bind parameter 'RelocateFile'. Cannot convert the
"Microsoft.SqlServer.Management.Smo.RelocateFile" value of type
"Microsoft.SqlServer.Management.Smo.RelocateFile" to type
"Microsoft.SqlServer.Management.Smo.RelocateFile".
At line:25 char:108
+ ... e -RelocateFil $RelocateData
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Restore-SqlDatabase], ParameterBindingException
+ FullyQualifiedErrorId CannotConvertArgumentNoMessage,Microsoft.SqlServer.Management.PowerShell.RestoreSqlDatabaseCommand
Run Code Online (Sandbox Code Playgroud)
我的powershell代码看起来像这样
$RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("MyDB_Data", "c:\data\MySQLServerMyDB.mdf")
$RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("MyDB_Log", "c:\data\MySQLServerMyDB.ldf")
$file = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile($RelocateData,$RelocateLog)
$myarr=@($RelocateData,$RelocateLog)
Restore-SqlDatabase -ServerInstance DEV\DEMO -Database "test" -BackupFile $backupfile -RelocateFile $myarr
Run Code Online (Sandbox Code Playgroud) 我有sqlcmd.exeSQLServer 2008和SQLServer 2012:
PS C:\> Get-Command sqlcmd.exe
Definition
----------
C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE
Run Code Online (Sandbox Code Playgroud)
通过修改$env:PATH我强制使用sqlcmd.exeSQL Server 2012:
PS C:\> $env:PATH = ($env:PATH -split ";" | Where-Object { $_ -notlike "*\Microsoft SQL Server\100\*" }) -join ";"
PS C:\> Get-Command sqlcmd.exe
Definition
----------
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE
Run Code Online (Sandbox Code Playgroud)
LocalDB的默认实例已启动并正在运行,并由当前用户拥有:
PS C:\> sqllocaldb i v11.0
Name: v11.0
Version: 11.0.2318.0
Shared name:
Owner: DOMAIN\me
Auto-create: Yes
State: Running
Last start time: 12/06/13 18:17:57
Instance pipe name: …Run Code Online (Sandbox Code Playgroud) 以前使用PowerShell 3.升级到PowerShell 4并在面临错误时将其卸载.
Import-Module : The specified module 'SQLPS' was not loaded because no valid module file was found in any module directory.但是所有与SQL相关的东西都没有错误.PowerShell控制台中出现此错误的根本原因是什么?
我正在尝试为不同的登录帐户编写用户映射脚本.我编写了用户和单个服务器角色的创建脚本,但我无法弄清楚如何使用Powershell设置用户映射,我还需要设置Database Role membership,特别是db_backupoperator
任何人都知道如何使用Powershell做到这一点?

Windows 7 工作站 POSH 3.0 SS 2012 SP1
摘要:
Developer 1 无法运行 Invoke-SQLCmd
Developer 2 具有类似 SS Client 工具安装可以运行 Invoke-SQLCmd
Developer 1 无法运行 Import-Module SQLPS
当我尝试从笔记本电脑运行查询时,如下所示:
Invoke-sqlcmd -ServerInstance <ServerName> -Query "Select top 10 * from <SomeTable>;"
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Invoke-sqlcmd : The 'Invoke-sqlcmd' command was found in the module 'SQLPS',
but the module could not be loaded. For more information, run 'Import-Module
SQLPS'.
Run Code Online (Sandbox Code Playgroud)
另一位开发人员可以成功运行该命令,并且我们都有本地
由于明显的原因,我无法在我的计算机上运行导入模块 sqlps - ExecutionPolicy 受到限制。
我不确定为什么其他开发人员的机器允许查询,而我的机器不允许。这是 SQL 客户端安装问题吗?
由于错误,我无法运行 Backup-SqlDatabase。我得到以下信息:
PS C:\Users\Mik> Backup-SqlDatabase
Backup-SqlDatabase : The 'Backup-SqlDatabase' command was found in the module 'SqlServer', but the module could not be
loaded. For more information, run 'Import-Module SqlServer'.
At line:1 char:1
+ Backup-SqlDatabase
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Backup-SqlDatabase:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
Run Code Online (Sandbox Code Playgroud)
我按照建议运行 Import-Module 并得到:
PS C:\Users\Mik> Import-Module SqlServer
Import-Module : The following error occurred while loading the extended type data file: Error in TypeData
"Microsoft.SqlServer.Management.Smo.NamedSmoObject": The member DefaultKeyPropertySet is already present.
Error in TypeData …Run Code Online (Sandbox Code Playgroud) 我正在运行以下命令(我能想到的最小的可重复示例):
Invoke-Sqlcmd "select * from sys.databases" | %{
Invoke-Sqlcmd "select 1"
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Invoke-Sqlcmd:找不到适合指定文化或中性文化的任何资源.确保"pipeline.resources"在编译时正确嵌入或链接到程序集"System.Management.Automation",或者所有所需的附属程序集都是可加载和完全签名的.在行:1 char:1 + Invoke-Sqlcmd"select*from sys.databases"| %{+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ClassInfo:InvalidResult :(:) [Invoke-Sqlcmd],MissingManifestResourceException + FullyQualifiedErrorId:ExecutionFailed,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
累了这个:
在所有情况下,结果都是相同的.
为什么以及如何诊断?
我用巧克力安装了 ms sql server:
choco install SQLServer2012DeveloperEditionWithSP1 -y -f -source 'http://choco.developers.tcpl.ca/chocolatey' -c "$env:WINDIR\temp"
Run Code Online (Sandbox Code Playgroud)
SQL 似乎已安装并在无法正常工作的 powershell 之外运行良好。我可以看到 sqlps 模块:
Get-Module -listavailable
Run Code Online (Sandbox Code Playgroud)
...
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.0 SQLASCMDLETS
Manifest 1.0 SQLPS
Run Code Online (Sandbox Code Playgroud)
但是命令似乎丢失了。我没有 invoke-sqlcmd 等。理论上,如果我安装模块,我应该可以访问它们,但是当我尝试导入模块 sqlps 时,却收到关于没有 sqlserver 驱动器的错误:
PS C:\WINDOWS\system32> Import-Module SQLPS
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 …Run Code Online (Sandbox Code Playgroud) 我尝试通过以管理员身份(win10,64Bit)执行以下命令来安装 Powershell SQLSERVER 模块,但它失败了
PS C:\WINDOWS\system32> Install-Module -Name SqlServer
PackageManagement\Install-Package : No match was found for the specified search
criteria and module name 'SqlServer'.
Try Get-PSRepository to see all available registered module repositories.
At C:\Program
Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809
char:21
+ ... $null = PackageManagement\Install-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound:
(Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex
ception
+ FullyQualifiedErrorId :
NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.
InstallPackage
Run Code Online (Sandbox Code Playgroud)
我在网上搜索过,尝试了一些解决方案,我什至与公司管理员交谈过,但没有运气。正如他们告诉我的那样,公司没有代理,只有防火墙。其他同事可以毫无问题地运行该命令。下一个命令也失败
PS C:\WINDOWS\system32> Get-PSrepository
WARNING: Unable to find module repositories.
Run Code Online (Sandbox Code Playgroud)
任何的想法?