You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell远程安装SQL Server时遭遇WS-Management插件拒绝远程连接请求的问题排查求助

PowerShell远程安装SQL Server时遭遇WS-Management插件拒绝远程连接请求的问题排查求助

我最近在写一个PowerShell脚本,用来给远程服务器批量安装SQL Server。手里有两台Windows Server 2019虚拟机,是同一个同事同时搭建的——除了我要求调整过的CPU数量和内存大小,其他配置应该完全一致。不过其中一台我已经手动安装过SQL Server了,另一台还没碰过。

脚本的执行流程是这样的:

  • 先创建远程会话和自定义会话配置,解决PowerShell的双跳问题
  • 关闭当前会话,用刚创建的新会话配置重新建立远程连接
  • 从第三台服务器挂载SQL安装ISO镜像(这也是必须解决双跳的原因)
  • 执行SQL安装程序
  • 卸载ISO镜像
  • 关闭远程会话

诡异的是,脚本在那台手动安装过的服务器上跑起来完全没问题,但在另一台全新的服务器上,虽然能成功创建会话配置,却卡在了建立第二个远程会话的步骤。我搜了好多资料,大部分结果都是说“WS-Management服务配置为不接受任何远程shell请求”,但这根本不是我的情况——我能正常连接、创建甚至更新会话配置,就是第二次用新配置连的时候失败,报错信息是:

"The service is configured to reject remote connection requests for this plugin"

找了半天都没找到关于这个错误的有效信息,实在卡壳了。

下面是脚本的核心代码部分:

# Start remote session $ServerName

$cred = Get-Credential $env:USERDOMAIN\$env:USERNAME

$remote = New-PSSession -ComputerName $ServerName -Credential $cred

$ConfigurationName = "DBAdmin"

# Create configuration on remote server if one doesn't exist, else update it

# (This allows double-hop to installation media)

"Starting remote session ...."

Invoke-Command -Session $remote -ScriptBlock {

param($configname, [PSCredential]$cred)

$config = Get-PSSessionConfiguration -Name $configname

if($config) {

if($config.RunAsUser -ne $cred.UserName) {

"Updating remote session configuration ...."

Set-PSSessionConfiguration -Name $configname -RunAsCredential $cred

}

} else {

"Creating remote session configuration ...."

Register-PSSessionConfiguration -Name $configname -RunAsCredential $cred

}

} -ArgumentList $ConfigurationName, $cred

# Stop remote session

Remove-PSSession -Session $remote

### Fails on this next line - on one server only ###

# Run installation on remote machine using configuration

$remote = New-PSSession -ComputerName $ServerName -Credential $cred -ConfigurationName $ConfigurationName

Invoke-Command -Session $remote -ScriptBlock {

param($path, $params)

# Mount ISO to drive letter

"Mounting installation ISO ...."

$isopath = $path + "*.iso"

$iso = (Get-ChildItem -Path $isopath).Name

$path += $iso

$mnt = Mount-DiskImage -ImagePath $path -PassThru

$drive = ($mnt | Get-Volume).DriveLetter

# Check for PID

if($params.Contains("pid-goes-here")) {

$pidpath = $drive[0] + ":\x64\DefaultSetup.ini"

$pidvalue = Get-Content -Path $pidpath | Where-Object {$_ -match "PID="} | ConvertFrom-StringData

$params = $params.Replace("pid-goes-here", $pidvalue.PID)

$params = $params.Replace("`"", "")

}

# Run installation

"Running installation ...."

# Write out command and parameters for testing

Write-Host ($drive[0] + ":\setup.exe")

Write-Host $params

# Uncomment following line to actually run setup

#Start-Process -FilePath ($drive[0] + ":\setup.exe") -ArgumentList $params -Wait

# Un-mount ISO

"Un-mounting ISO ...."

$mnt = Dismount-DiskImage -ImagePath $path

} -ArgumentList $InstallMediaPath, $Parameters

# Stop remote session

"Closing remote session ...."

Remove-PSSession -Session $remote

我已经试过把组策略里的“允许远程访问”从“未配置”改成“已启用”,但没效果,而且我觉得这也不是问题根源。毕竟这是我第一次正经写PowerShell脚本,可能漏掉了一些很基础的点?

接下来我打算先给那台有问题的服务器手动安装一次SQL,如果之后脚本能正常运行,再去排查手动安装到底做了什么配置改变——但手动安装完全违背了我写自动化脚本的初衷啊!

有没有大佬能给点排查方向或者解决思路?

备注:内容来源于stack exchange,提问作者Dave Harding

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 07:59:52