前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >如何验证Windows WinRM通不通

如何验证Windows WinRM通不通

原创
作者头像
Windows技术交流
修改2024-12-11 21:01:05
修改2024-12-11 21:01:05
970
举报
文章被收录于专栏:Windows技术交流

1、查监听,5985端口

代码语言:txt
复制
netstat -ato|findstr ":5985 :5986"

2、查防火墙状态,看RemoteAdmin是不是启用状态

代码语言:txt
复制
cmd.exe /c 'chcp 437 && netsh.exe firewall show state | findstr /R "\bRemote admin mode\b"'

3、powershell验证连通性

代码语言:txt
复制
$Username = 'Administrator'
$Password = '明文密码'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

$iparray = @('IP地址')

for($i=0;$i -lt $iparray.Length;$i++){
"`$iparray["+$i+"]="+$iparray[$i]+"`n"
Invoke-Command -ComputerName $iparray[$i] -Credential $Cred -ScriptBlock { 
hostname
}
}

4、winrs命令验证连通性

https://docs.vmware.com/en/VMware-Aria-Automation/8.12/Using-Automation-Orchestrator-Plugins/GUID-79518969-9B73-48E3-8B05-72C78179F555.html

https://variacom.com/index.php/solutions/98-enable-winrm-through-group-policy

代码语言:txt
复制
winrs -r:http://ip:port -u:username-p:password command
例如
winrs -r:http://192.168.211.135:5985 -u:Administrator -p:MyPassword hostname

5、光3通了还不行,还得这一步通(返回码得是0)

代码语言:txt
复制
wmic /node:"IP地址" /USER:"Administrator" /password:"密码" process call create "fsutil file createnew C:\empty.txt 0"
wmic /node:"IP地址" /USER:"Administrator" /password:"密码" process call create "cmd.exe"

注意,新版Windows系统中可能已经没有wmic了。wmic于 21H1 开始宣布弃用,并在 24H2 中被移除。

微软在 2023 年淘汰了大量 Windows 功能和组件,其中包括WordPad、Cortana、步骤记录器等,2024 年 1 月 29 日之后关闭 WMIC,从 Windows 11 预览版开始。实测24H2中(包括Server2025)已经没有wmic,更推陈出新的windows系统更不会有wmic。

可以通过wmic计算当前开机已消逝时间

代码语言:txt
复制
# 获取 LocalDateTime 的值
$currentDateTime = ((cmd.exe /c 'wmic os get LocalDateTime').Trim() | Select-Object -Skip 2 -First 1).Trim().Split('.')[0]

# 获取 LastBootUpTime 的值
$lastBootUpTime = ((cmd.exe /c 'wmic os get LastBootUpTime').Trim() | Select-Object -Skip 2 -First 1).Trim().Split('.')[0]

# 将字符串转换为 DateTime 对象
$currentDateTimeObj = [datetime]::ParseExact($currentDateTime, "yyyyMMddHHmmss", $null)
$lastBootUpTimeObj = [datetime]::ParseExact($lastBootUpTime.Substring(0, 14), "yyyyMMddHHmmss", $null)

# 计算时间差
$uptime = $currentDateTimeObj - $lastBootUpTimeObj

# 输出结果
$uptimestring=$uptime.Days.ToString() + "d" + $uptime.Hours.ToString() + "h" + $uptime.Minutes.ToString() + "m" + $uptime.Seconds.ToString() + "s"
Write-Output "本次已开机时间: $uptimestring"

对于没有wmic命令的系统,计算当前开机已消逝时间

代码语言:txt
复制
$lastBootTime = (Get-WmiObject -Class Win32_OperatingSystem | Select-Object -Property LastBootUpTime).LastBootUpTime
$bootTimeFormatted = [Management.ManagementDateTimeConverter]::ToDateTime($bootTime)
#$bootTimeString = $bootTimeFormatted.ToString("yyyyMMddHHmmss")
$currentDateTime = Get-Date -Format 'yyyyMMddHHmmss'
# 将字符串转换为 DateTime 对象
$currentDateTimeObj = [datetime]::ParseExact($currentDateTime, "yyyyMMddHHmmss", $null)
$lastBootUpTimeObj = [datetime]::ParseExact($lastBootTime.Substring(0, 14), "yyyyMMddHHmmss", $null)
Write-Output "本次开机时间: $lastBootUpTimeObj"

# 计算时间差
$uptime = $currentDateTimeObj - $lastBootUpTimeObj

# 输出结果
$uptimestring=$uptime.Days.ToString() + "d" + $uptime.Hours.ToString() + "h" + $uptime.Minutes.ToString() + "m" + $uptime.Seconds.ToString() + "s"
Write-Output "本次已开机时间: $uptimestring"

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档