在 Powershell 中替换
Replace in Powershell
我正在尝试打印出 Microsoft 更新修补程序 URL 并进行更改
$link=Get-MSHotfix|Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-20000))}|Select-Object -Property KBArticle
foreach($line in $link){
[String]$line = $line -replace 'http://support.microsoft.com/?kbid=','https://support.microsoft.com/en-us/kb/'
[String]$line
}
我有问题,因为它是这样打印出来的,它没有替换:
@{KBArticle=http://support.microsoft.com/?kbid=3045992}
@{KBArticle=http://support.microsoft.com/?kbid=3045999}
@{KBArticle=http://support.microsoft.com/?kbid=3046017}
@{KBArticle=http://support.microsoft.com/?kbid=3046359}
@{KBArticle=http://support.microsoft.com/?kbid=3046737}
如果我不带 -replace 打印它看起来没问题。
我正在尝试获取完整的 URL 知识库文章
我正在尝试创建一个脚本,如果可能的话,它会打印出标题中带有链接和名称的所有修补程序
谢谢
是的,您必须构建正则表达式,
RegEx101
$link=Get-MSHotfix|Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-20000))}|Select-Object -Property KBArticle
foreach($line in $link){
[String]$line = $line -replace "http:\/\/support\.microsoft\.com\/\?kbid=",'https://support.microsoft.com/en-us/kb/'
[String]$line
}
或者您使用子串:
$link=Get-MSHotfix|Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-20000))}|Select-Object -Property KBArticle
foreach($line in $link){
[String]$line = 'https://support.microsoft.com/en-us/kb/' + $line.substring(35)
[String]$line
}
将字符串剪切前 35 个字符并将其添加到您的 url。
编辑:
非常有趣,另一种替换也有效...
$link=Get-MSHotfix|Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-20000))}|Select-Object -Property KBArticle
foreach($line in $link){
[String]$line = $line.replace("http://support.microsoft.com/?kbid=",'https://support.microsoft.com/en-us/kb/')
[String]$line
}
我有点糊涂...
如果你使用 -replace 你必须使用正则表达式如果你调用函数 .replace() 你需要给出一个字符串。
我正在尝试打印出 Microsoft 更新修补程序 URL 并进行更改
$link=Get-MSHotfix|Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-20000))}|Select-Object -Property KBArticle
foreach($line in $link){
[String]$line = $line -replace 'http://support.microsoft.com/?kbid=','https://support.microsoft.com/en-us/kb/'
[String]$line
}
我有问题,因为它是这样打印出来的,它没有替换:
@{KBArticle=http://support.microsoft.com/?kbid=3045992}
@{KBArticle=http://support.microsoft.com/?kbid=3045999}
@{KBArticle=http://support.microsoft.com/?kbid=3046017}
@{KBArticle=http://support.microsoft.com/?kbid=3046359}
@{KBArticle=http://support.microsoft.com/?kbid=3046737}
如果我不带 -replace 打印它看起来没问题。
我正在尝试获取完整的 URL 知识库文章
我正在尝试创建一个脚本,如果可能的话,它会打印出标题中带有链接和名称的所有修补程序
谢谢
是的,您必须构建正则表达式, RegEx101
$link=Get-MSHotfix|Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-20000))}|Select-Object -Property KBArticle
foreach($line in $link){
[String]$line = $line -replace "http:\/\/support\.microsoft\.com\/\?kbid=",'https://support.microsoft.com/en-us/kb/'
[String]$line
}
或者您使用子串:
$link=Get-MSHotfix|Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-20000))}|Select-Object -Property KBArticle
foreach($line in $link){
[String]$line = 'https://support.microsoft.com/en-us/kb/' + $line.substring(35)
[String]$line
}
将字符串剪切前 35 个字符并将其添加到您的 url。
编辑:
非常有趣,另一种替换也有效...
$link=Get-MSHotfix|Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-20000))}|Select-Object -Property KBArticle
foreach($line in $link){
[String]$line = $line.replace("http://support.microsoft.com/?kbid=",'https://support.microsoft.com/en-us/kb/')
[String]$line
}
我有点糊涂...
如果你使用 -replace 你必须使用正则表达式如果你调用函数 .replace() 你需要给出一个字符串。