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

远程获取Win10激活码的PowerShell脚本文件名命名问题求助

问题根因

你当前脚本的核心问题是:生成文件名、执行slmgr.vbs的逻辑都在本地运行,$env:computername读取的是执行脚本的本地主机名,Invoke-Command调用的$Using:GetActID只是传递了本地执行的结果,没有实际在远程主机上运行命令,所以才会出现所有文件重名的问题。

单设备修正脚本

推荐将结果返回本地再写入中央仓库,不需要给远程主机开放共享目录写入权限,安全性更高:

# 配置全局参数
$AdminCred = Get-Credential -Message "输入域管理员权限账号"
$CentralDir = '\\computer01\c$\temp'
$targetHost = "computer01"

# 确保中央仓库目录存在
if (-not (Test-Path $CentralDir)) {
    New-Item -Path $CentralDir -ItemType Directory -Force | Out-Null
}

# 先检测网络连通性
if (Test-Connection -ComputerName $targetHost -Count 2 -Quiet) {
    try {
        # 建立远程会话
        $session = New-PSSession -ComputerName $targetHost -Credential $AdminCred -ErrorAction Stop
        # 远程执行命令获取激活ID,结果返回本地
        $activationID = Invoke-Command -Session $session -ScriptBlock {
            cscript.exe C:\Windows\System32\slmgr.vbs -dti
        }
        # 销毁会话
        Remove-PSSession $session
        # 本地用远程主机名命名文件写入中央仓库
        $outputPath = Join-Path $CentralDir "$targetHost.txt"
        $activationID | Out-File -FilePath $outputPath -Encoding UTF8
        Write-Host "$targetHost 激活标识获取完成"
    }
    catch {
        Write-Host "$targetHost 执行失败:$_"
    }
}
else {
    Write-Host "$targetHost 网络不可达,已跳过"
}

批量遍历Excel主机列表实现方案

前置依赖

如果使用Excel直接读取,需要先安装ImportExcel模块:

Install-Module -Name ImportExcel -Scope CurrentUser -Force

如果不想安装第三方模块,可将Excel另存为UTF8编码的CSV文件,后续用Import-Csv读取即可。

批量脚本

假设Excel/CSV中存储主机名的列名为主机名:

# 配置全局参数
$AdminCred = Get-Credential -Message "输入域管理员权限账号"
$CentralDir = '\\computer01\c$\temp'
$hostListPath = "C:\AD导出的主机列表.xlsx" # 存为CSV则改后缀为.csv即可

# 确保中央仓库目录存在
if (-not (Test-Path $CentralDir)) {
    New-Item -Path $CentralDir -ItemType Directory -Force | Out-Null
}

# 读取主机列表
if ($hostListPath.EndsWith('.xlsx')) {
    $hostList = Import-Excel -Path $hostListPath | Select-Object -ExpandProperty "主机名"
}
elseif ($hostListPath.EndsWith('.csv')) {
    $hostList = Import-Csv -Path $hostListPath -Encoding UTF8 | Select-Object -ExpandProperty "主机名"
}
else {
    Write-Error "仅支持xlsx或csv格式的主机列表"
    exit
}

# 批量遍历执行
foreach ($targetHost in $hostList) {
    # 跳过空行
    if ([string]::IsNullOrWhiteSpace($targetHost)) { continue }

    if (Test-Connection -ComputerName $targetHost -Count 2 -Quiet) {
        try {
            $session = New-PSSession -ComputerName $targetHost -Credential $AdminCred -ErrorAction Stop
            $activationID = Invoke-Command -Session $session -ScriptBlock {
                cscript.exe C:\Windows\System32\slmgr.vbs -dti
            }
            Remove-PSSession $session
            $outputPath = Join-Path $CentralDir "$targetHost.txt"
            $activationID | Out-File -FilePath $outputPath -Encoding UTF8
            Write-Host "$targetHost 激活标识获取完成"
        }
        catch {
            Write-Host "$targetHost 执行失败:$_"
        }
    }
    else {
        Write-Host "$targetHost 网络不可达,已跳过"
    }
}

注意事项

  • 所有远程主机需要提前开启WinRM服务,防火墙放行WinRM端口(HTTP 5985、HTTPS 5986),域环境可通过组策略批量配置
  • 如果执行时出现权限报错,可确认运行脚本的账号对中央共享目录有写入权限、远程主机的管理员凭证有效

内容的提问来源于stack exchange,提问作者Mooseheads36

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 23:54:03