简体   繁体   English

在Powershell中替换

[英]Replace in Powershell

I am trying to print out Microsoft update hot fix URLs and change them 我正在尝试打印并更新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
}

I have problem because it prints it out like this and it is not replacing: 我有问题,因为它像这样打印出来,并且不能替换:

@{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}

If I just print it without -replace it looks ok. 如果我只打印而不用-replace,就可以了。

I am trying to get full URL of KB Article 我正在尝试获取知识库文章的完整URL

I am trying to create a script with will print out all hot fixes with links and names from title if possible 我正在尝试创建一个脚本,如果可能的话,它将打印出所有带有标题的链接和名称的热修复程序

Thanks 谢谢

Yep you have to build the regular expression, RegEx101 是的,您必须构建正则表达式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
}

or you use substring: 或者您使用子字符串:

$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
}

cut the strings by first 35 characters and add it to your url. 将字符串切成35个字符,然后将其添加到您的网址中。

EDIT: 编辑:

very interesting, what also works is the other kind of replace... 非常有趣,另一种替代方法也是有效的...

$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
}

I'm a bit confused... 我有点困惑...

if you use -replace you have to take a regex if you call the function .replace() you need to give a string. 如果使用-replace,则必须使用正则表达式;如果调用函数.replace(),则需要提供字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM