【发布时间】:2021-05-12 20:45:26
【问题描述】:
为了减少我们的 VPN 路由上的流量,我需要从外部服务器下载 Windows 更新,同时向我们的内部服务器报告。
所以我正在做以下事情:
创建一个 UpdateSession 并搜索更新,将它们存储在 $SearchResult 中。 然后我从外部服务器下载更新,然后我想通过 IUpdate2.CopyToCache(IStringCollection) 将它们传递到 Windows Update Api
一切都很好,除了将 StringCollection 传递给方法 CopyToCache 并最终以“指定的强制转换无效。”-错误结束。
这是我的代码:
感谢您的帮助! eldo-ob
$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 隧道旁边。
【问题讨论】:
-
重复创建一个集合,将一个项目添加到该集合,然后将该集合作为参数传递是没有意义的......
CopyToCache()期望什么样的参数?它可以接受纯字符串(filenmae)吗?它需要一个数组吗? )$StringArray = @(("C:\temp\WSUS\{0}" -f $FileName)) -
查看接口后...IUpdate2::CopyToCache(),它需要一个IStringCollection接口...但那是一个COM接口。需要找到实现 IStringCollection 接口的 COM 对象。我不认为 .NET 对象 StringCollection 实现了 IStringCollection 接口。
标签: powershell com windows-update stringcollection