为了减少我们的 VPN 路由上的流量,我需要从外部服务器下载 Windows 更新,同时向我们的内部服务器报告。
所以我正在做以下事情:
创建一个 UpdateSession 并搜索更新,将它们存储在 $SearchResult 中。然后我从外部服务器下载更新,然后我想通过IUpdate2.CopyToCache(IStringCollection)将它们传递到 Windows Update Api
一切都很好,除了将 StringCollection 传递给方法 CopyToCache 并最终以“指定的强制转换无效。”-错误结束。
这是我的代码:
感谢帮助!埃尔多奥布
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
$UpdateCollection = New-Object -Com Microsoft.Update.UpdateColl
$SearchResult = $UpdateSearcher.Search("IsInstalled=0 and Type='Software'")
$AvailibleUpdates = [int16]$SearchResult.Updates.Count
$AvailibleUpdates
$WebClient = New-Object System.Net.WebClient
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
@($SearchResult.Updates.Item(0).BundledUpdates) | Foreach {
$_.DownloadContents | Foreach {
$FileName = $_.DownloadUrl.Split("/")[-1]
$downloadFrom = $_.DownloadUrl.Replace("http://contoso-intern.com","https://contoso-extern.com")
$WebClient.DownloadFile($downloadFrom,("C:\temp\WSUS\{0}" -f $FileName))
Write-Host "File Downloaded" -ForegroundColor Green
}
$StringCollection = New-Object System.Collections.Specialized.StringCollection
$StringCollection.Add(("C:\temp\WSUS\{0}" -f $FileName))
$_.CopyToCache($StringCollection)
}
错误消息:
Specified cast is not valid.
At line:24 char:19
+ $_.CopyToCache($StringCollection)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException
更新/解决方案:
# create UpdateSession
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
# create UpdateSearcher
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
# search for updates & count updates
$SearchResult = $UpdateSearcher.Search("IsInstalled=0 and Type='Software'")
$AvailibleUpdates = [int16]$SearchResult.Updates.Count
# create an WebClient instance for downloading Updates from alternative source
$WebClient = New-Object System.Net.WebClient
# fix some tls issues (not for everyone neccessary)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# iterate the updates in searchresult
$SearchResult.Updates | ForEach-Object {
# iterate bundledupdates
$_.BundledUpdates | ForEach-Object {
# create COM stringcollection
$StringCollection = New-Object -ComObject "Microsoft.Update.StringColl.1"
# iterate downloadcontents
$_.DownloadContents | ForEach-Object {
# get the filename from url
$FileName = $_.DownloadUrl.Split("/")[-1]
# create external downloadlink
$downloadFrom = $_.DownloadUrl.Replace("http://contoso-intern.com","https://contoso-extern.com/wsusreplica")
# download update with webclient
$WebClient.DownloadFile($downloadFrom,("C:\temp\WSUS\{0}" -f $FileName))
# adding downloaded filepath to stringcollection
$StringCollection.Add(("C:\temp\WSUS\{0}" -f $FileName))
}
# copy downloaded file to cache (load into wuapi)
$_.CopyToCache($StringCollection)
}
}
# create installer
$Installer = $UpdateSession.CreateUpdateInstaller()
# set the updates
$Installer.Updates = $SearchResult.Updates
# and install
$Installer.Install()
在不使用自选位置的 UpdateDownloader 的情况下成功安装的更新。所以现在我可以通过 vpn 隧道报告和搜索更新,并从外部源下载更新,我们可以在 vpn 隧道旁边路由流量。