【问题标题】:Removing words in a string Powershell删除字符串Powershell中的单词
【发布时间】:2016-01-21 18:05:16
【问题描述】:

我正在尝试在 Powershell 中删除行/字符串中的单词。我以几种不同的方式尝试了 -replace 运算符,但没有结果。需要删除的数据在一个包含很多行的文本文件中,每一行都有 Data: Primary: 然后是 Azure Storage Account Key。我正在运行另一个脚本,它将遍历此 txt 以获取存储帐户密钥以运行一些命令。

该行中的数据如下所示: 数据:主要:asdflkajsdflkj/$asdfASDFasdf

我需要删除 'data:' 'Primary:' 和第一个字母之前的所有空格。

下面是我的脚本:

$getAccessKey = Get-Content 'file path'
$getAccessKey | ForEach-Object{
    azure storage account keys list $_
} | Set-Content 'file path'

Get-Content 'file path' | Where-Object {$_ -match 'Primary'} | Set-Content 'file path'
Get-Content 'file path' | Where-Object {$_ -replace ".*:"} | Set-Content 'file path'

下面的脚本是我之前尝试过的:

$getAccessKey = Get-Content 'file path'
$getAccessKey | ForEach-Object{
    azure storage account keys list $_
} | Set-Content 'file path'

Get-Content 'file path' | Where-Object {$_ -match 'Primary'} | Set-Content 'file path'
Get-Content 'file path' | Where-Object {$_ -replace "Data:"} | Set-Content 'file path'

这些脚本都没有得到结果,虽然它没有选择“:”或文本“数据:”

【问题讨论】:

  • 如果您要解析存储密钥,为什么不将它们保存为更容易解析的格式,或者直接从 Azure 中提取它们?
  • 我正在尝试创建一种方式,让我再也不必这样做,或者去天蓝色获取密钥来获取我正在寻找的信息。给定 100 多个存储帐户和后续共享,检查文件共享的大小可能需要相当长的时间。那我想自动化这个过程,让它抓取未来创建的任何新股票/帐户。

标签: powershell azure text replace


【解决方案1】:

从你的评论来看,一个更好的解决方案是运行这样的东西

$StorageAccountKeys = @{}
Get-AzureRmStorageAccount | `
    foreach {$StorageAccountKeys.Add($_.StorageAccountName, `
    ((Get-AzureRmStorageAccountKey -ResourceGroupName `
    $_.ResourceGroupName -Name $_.StorageAccountName).Key1)) } 

这将创建一个包含订阅中所有存储帐户名称和密钥的对象。

Name           Value
----           -----
storagename    xxxxxxxxxxxxxxxxxxxx-storagekey-xxxxxxxxxxxxxxxxxxxxx
storagename1    xxxxxxxxxxxxxxxxxxxx-storagekey1-xxxxxxxxxxxxxxxxxxxxx
storagename 2   xxxxxxxxxxxxxxxxxxxx-storagekey2-xxxxxxxxxxxxxxxxxxxxx

您可以使用 foreach 或 $StorageAccountKeys.storagename 访问它

【讨论】:

  • 这并没有按照我想要的方式工作。但我想通了。非常感谢您的帮助。
【解决方案2】:

好消息,在摆弄了几分钟后,我找到了自己问题的答案。

$getAccessKey = Get-Content 'file path'
$getAccessKey | ForEach-Object{
azure storage account keys list $_
} | Set-Content 'file path'

Get-Content 'file path' | Where-Object {$_ -match 'Primary'} | Set-Content 'file path'
Get-Content 'file path' | ForEach-Object {$_ -replace "\w{4}:\s+\w{7}:\s+", ""} | Set-Content 'file path'

【讨论】:

    猜你喜欢
    • 2014-11-21
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多