通过 SCCM 发送 WoL 的 Powershell 脚本

Powershell script to send WoL via SCCM

我希望将一个简短的 Powershell 脚本放在一起,询问资产编号,然后通过 Config Manager/SCCM 向该资产发送 WoL 请求以将其唤醒。我见过的大多数 Powershell WoL 脚本似乎在没有 SCCM 的情况下实现了这一点,通过创建通过 mac 地址发送它们的魔法数据包——我不想这样做。

我在网上搜索,看看是否有人已经做到了这一点并看到了这个页面:

https://ccmexec.com/2019/01/wake-up-single-computer-or-collection-of-computers-in-configmgr-1810-using-powershell/

在该页面上,他们演示了由 Johan Schrewelius 和 Jörgen Nilsson 编写的脚本,其中包含以下内容:

[CmdletBinding()]
Param(
    $CmpName = $Null,
    $CollId = "SMS00001",
    $SiteServer = "<site server fgdn>"
)

if (!$CmpName -and $CollId -eq "SMS00001") {

    Write-Host "Seems wrong to wake every single computer in the environment, refusing to perform."
    exit 1
}

$SiteCode = (Get-WmiObject -ComputerName "$SiteServer" -Namespace root\sms -Query 'SELECT SiteCode FROM SMS_ProviderLocation').SiteCode

if ($CmpName) {

    $ResourceID = (Get-WmiObject  -ComputerName "$SiteServer" -Namespace "Root\SMS\Site_$($SiteCode)" -Query "Select ResourceID from SMS_R_System Where NetBiosName = '$($CmpName)'").ResourceID

    if ($ResourceID) {
        $CmpName = @($ResourceID)
    }
}

$WMIConnection = [WMICLASS]"\$SiteServer\Root\SMS\Site_$($SiteCode):SMS_SleepServer"
$Params = $WMIConnection.psbase.GetMethodParameters("MachinesToWakeup")
$Params.MachineIDs = $CmpName
$Params.CollectionID  = $CollId
$return = $WMIConnection.psbase.InvokeMethod("MachinesToWakeup", $Params, $Null) 

if (!$return) {
    Write-Host "No machines are online to wake up selected devices"
}

if ($return.numsleepers -ge 1) {
    Write-Host "The resource selected are scheduled to wake-up as soon as possible"
}

此脚本可以 运行 通过将文件保存为类似 wol.ps1 的文件,然后将 $SiteServer 变量设置为 SCCM FQDN 然后运行宁 .\wol.ps1 -CmpName 'asset number' 将 WoL 请求发送到单个资产,或 .\wol.ps1 -CmpName 'SCCM collection ID' 将 WoL 请求发送到 SCCM 资产集合。

这已经很不错了,但我正在寻找的是一个基本的 Read-Host 脚本,它请求一个资产编号,然后将 WoL 请求发送到该资产,仅此而已。我已经尝试拆除此脚本中的参数以获得实现此目的所需的最少部分,但除了它从 SCCM 服务器请求的信息和请求资产的 SCCM 资源 ID 之外,我无法理解它,我不太明白它是如何工作的。

我可能最终会弄清楚,但我想我会把它扔在这里看看是否有人以前做过这个,或者是否有人可以看到这个脚本是如何实现它的功能并能帮助我的将其转化为一系列简单的简短操作 post 用户输入。

提前干杯

Param(
    $SiteServer = "Your SCCM Server"
)

$CmpName = Read-host "Enter asset number"
$SiteCode = (Get-WmiObject -ComputerName "$SiteServer" -Namespace root\sms -Query 'SELECT SiteCode FROM SMS_ProviderLocation').SiteCode

if ($CmpName) {

    $ResourceID = (Get-WmiObject  -ComputerName "$SiteServer" -Namespace "Root\SMS\Site_$($SiteCode)" -Query "Select ResourceID from SMS_R_System Where NetBiosName = '$($CmpName)'").ResourceID

    if ($ResourceID) {
        $CmpName= @($ResourceID)
    }
}
$WMIConnection = [WMICLASS]"\$SiteServer\Root\SMS\Site_$($SiteCode):SMS_SleepServer"
$Params = $WMIConnection.psbase.GetMethodParameters("MachinesToWakeup")
$Params.MachineIDs = $CmpName
$return = $WMIConnection.psbase.InvokeMethod("MachinesToWakeup", $Params, $Null) 

if (!$return) {
    Write-Host "No machines are online to wake up selected devices"
}

if ($return.numsleepers -ge 1) {
    Write-Host "The resource selected are scheduled to wake-up as soon as possible"
}