[reflection.assembly] :: LoadWithPartialName(“ Microsoft.SqlServer.Smo”)不返回VersionString

时间:2019-05-28 11:08:45

标签: sql-server powershell sql-server-2012

我有以下在许多服务器上运行的powershell脚本,用于检索SQL VersionString:

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" "."
$srv.VersionString

它适用于除一台服务器外的所有服务器,该服务器返回空白行。

仅运行:

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
New-Object "Microsoft.SqlServer.Management.Smo.Server" "."

返回:

AuditLevel                  : 
BackupDirectory             : 
BrowserServiceAccount       : 
BrowserStartMode            : 
BuildClrVersionString       : 
BuildNumber                 : 
ClusterName                 : 
ClusterQuorumState          : 
ClusterQuorumType           : 
Collation                   : 
CollationID                 : 
ComparisonStyle             : 
ComputerNamePhysicalNetBIOS : 
DefaultFile                 : 
DefaultLog                  : 
Edition                     : 
ErrorLogPath                : 
FilestreamLevel             : 
FilestreamShareName         : 
HadrManagerStatus           : 
InstallDataDirectory        : 
InstallSharedDirectory      : 
InstanceName                : 
IsCaseSensitive             : 
IsClustered                 : 
IsFullTextInstalled         : 
IsHadrEnabled               : 
IsSingleUser                : 
Language                    : 
LoginMode                   : 
MailProfile                 : 
MasterDBLogPath             : 
MasterDBPath                : 
MaxPrecision                : 
NamedPipesEnabled           : 
NetName                     : 
NumberOfLogFiles            : 
OSVersion                   : 
PerfMonMode                 : 
PhysicalMemory              : 
PhysicalMemoryUsageInKB     : 
Platform                    : 
Processors                  : 
ProcessorUsage              : 
Product                     : 
ProductLevel                : 
ResourceLastUpdateDateTime  : 
ResourceVersionString       : 
RootDirectory               : 
ServerType                  : 
ServiceAccount              : 
ServiceInstanceId           : 
ServiceName                 : 
ServiceStartMode            : 
SqlCharSet                  : 
SqlCharSetName              : 
SqlDomainGroup              : 
SqlSortOrder                : 
SqlSortOrderName            : 
Status                      : 
TapeLoadWaitTime            : 
TcpEnabled                  : 
VersionMajor                : 
VersionMinor                : 
VersionString               : 
Name                        : SERVERDR
Version                     : 
EngineEdition               : 
ResourceVersion             : 
BuildClrVersion             : 
DefaultTextMode             : True
Configuration               : Microsoft.SqlServer.Management.Smo.Configuration
AffinityInfo                : 
ProxyAccount                : 
Mail                        : 
Databases                   : 
Endpoints                   : 
Languages                   : 
SystemMessages              : 
UserDefinedMessages         : 
Credentials                 : 
CryptographicProviders      : 
Logins                      : 
Roles                       : 
LinkedServers               : 
SystemDataTypes             : 
JobServer                   : 
ResourceGovernor            : 
ServiceMasterKey            : 
Settings                    : Microsoft.SqlServer.Management.Smo.Settings
Information                 : Microsoft.SqlServer.Management.Smo.Information
UserOptions                 : Microsoft.SqlServer.Management.Smo.UserOptions
BackupDevices               : 
FullTextService             : 
ActiveDirectory             : 
Triggers                    : 
Audits                      : 
ServerAuditSpecifications   : 
AvailabilityGroups          : 
ConnectionContext           : Data Source=.;Integrated Security=True;MultipleActiveResultSets=False;Application Name="SQL Management"
Events                      : Microsoft.SqlServer.Management.Smo.ServerEvents
OleDbProviderSettings       : 
Urn                         : 
Properties                  : 
UserData                    : 
State                       : Existing
IsDesignMode                : False
DomainName                  : SMO
DomainInstanceName          : 

问题是,为什么它不返回有关此服务器上安装的SQL的信息?

服务器详细信息: Windows 2008 R2 Microsoft SQL Server 2012-11.0.5058.0(X64) Powershell 5.1版

1 个答案:

答案 0 :(得分:0)

显然,对于SQL Server 2008,您还需要加载辅助程序集:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
#Need SmoExtended for smo.backup
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null

至少,当您想在SQL Server中进行SQL Server还原时,您需要这样做,所以也许出于您的目的也需要它。

我发现了信息here

我自己也无法测试。