使用 Powershell 删除字符串中两个字符之间的文本
Removing text in a string between two characters using Powershell
我有一个 powershell 脚本,可以运行和收集信息并将其放入 .csv 文件中。信息示例如下所示,每行以唯一的服务器名称开头,后跟包含一对 ( ) 的随机唯一标识符。
"GDR01W01SQ004 (e785754f-eeb1)","1","4","63","NY-TER-PLN-P-5N"
"GDR01L02D001 (4b889a4d-d281)","4","12","129","CO-FDP-STE-NP-5N"
我有第二个 powershell 脚本运行并获取此 .csv 文件及其信息并将其格式化为具有 header 和适当间距的报告。
有人可以帮我删除 ( ) 和 ( ) 之间的文字吗?
我希望每一行的条目如下所示:
"GDR01W01SQ004","1","4","63","NY-TER-PLN-P-5N"
非常感谢您!
这是我一直在使用的脚本。
####################PowerCLI Check####################
# Verify whether the PowerCLI module is loaded, if not load it.
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin VMware.VimAutomation.Core -ErrorAction Stop
}
################### Run Time Section ####################
#This script should be run from a server that has DNS records for all entries in vcenters.txt
$file = get-Content c:\reports\vcenter\vcenters.txt
foreach ( $server in $file) {
Connect-VIserver -Server $server
Get-VM | select Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes | Out-Null
}
# Command for Custom Annotations.
Get-VM | select Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes -expandproperty customfields | Export-Csv -path “c:\reports\vcenter\vcenall.csv” -NoTypeInformation
# Takes vcenall.csv and sorts only the Name and Notes columns and selects all but the custom fields. Capacity Reporting script caprep.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Notes, Name | Select-Object Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes |Export-csv capacity.csv -NoTypeInformation
#Used to remove domain from server name
(Get-Content capacity.csv) | ForEach-Object { $_ -replace ".domain.local", "" } | Set-Content capacity.csv
# Takes vcenall.csv and sorts only the Notes column and selects only the Name and Notes columns. Nimsoft comparison script nimcomp.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Notes | Select-Object Name, Notes | Export-csv nimsoft.csv -NoTypeInformation
# Takes vcenall.csv and sorts only the Name columns and exports all fields. Backup/Restore comparison script bure.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Name | Export-csv bure.csv -NoTypeInformation
我认为您需要添加更多信息,但只需使用您已有的信息就可以尝试这种方法
Import-Csv C:\temp\test.csv -Header server,1,2,3,4 | ForEach-Object{
$_.server = (($_.server).split("(")[0]).Trim()
$_
}
我们导入 csv 数据并分配一个 header。如果您已经有一个,则可以省略此参数。
然后我们将每一行数据作为 object 检查。更改 server
数据,将其拆分为 space。如果此数据用于服务器名称,则可以安全地假设第一个 space 之前的所有内容都是服务器名称。这种方法取决于 space 在那里。我们也可以对 (
使用相同的逻辑,但如果 space 是一个保证,这会更容易。
因此我们更新 server
,然后使用 $_
将数据发送回管道。
示例输出
server 1 2 3 4
------ - - - -
GDR01W01SQ004 1 4 63 NY-TER-PLN-P-5N
GDR01L02D001 4 12 129 CO-FDP-STE-NP-5N
根据评论编辑
因为它是服务器显示名称,所以我将逻辑更改为基于“(”进行拆分。还使用 Split()
方法而不是 -split
运算符。
我有一个 powershell 脚本,可以运行和收集信息并将其放入 .csv 文件中。信息示例如下所示,每行以唯一的服务器名称开头,后跟包含一对 ( ) 的随机唯一标识符。
"GDR01W01SQ004 (e785754f-eeb1)","1","4","63","NY-TER-PLN-P-5N"
"GDR01L02D001 (4b889a4d-d281)","4","12","129","CO-FDP-STE-NP-5N"
我有第二个 powershell 脚本运行并获取此 .csv 文件及其信息并将其格式化为具有 header 和适当间距的报告。
有人可以帮我删除 ( ) 和 ( ) 之间的文字吗?
我希望每一行的条目如下所示:
"GDR01W01SQ004","1","4","63","NY-TER-PLN-P-5N"
非常感谢您!
这是我一直在使用的脚本。
####################PowerCLI Check####################
# Verify whether the PowerCLI module is loaded, if not load it.
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin VMware.VimAutomation.Core -ErrorAction Stop
}
################### Run Time Section ####################
#This script should be run from a server that has DNS records for all entries in vcenters.txt
$file = get-Content c:\reports\vcenter\vcenters.txt
foreach ( $server in $file) {
Connect-VIserver -Server $server
Get-VM | select Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes | Out-Null
}
# Command for Custom Annotations.
Get-VM | select Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes -expandproperty customfields | Export-Csv -path “c:\reports\vcenter\vcenall.csv” -NoTypeInformation
# Takes vcenall.csv and sorts only the Name and Notes columns and selects all but the custom fields. Capacity Reporting script caprep.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Notes, Name | Select-Object Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes |Export-csv capacity.csv -NoTypeInformation
#Used to remove domain from server name
(Get-Content capacity.csv) | ForEach-Object { $_ -replace ".domain.local", "" } | Set-Content capacity.csv
# Takes vcenall.csv and sorts only the Notes column and selects only the Name and Notes columns. Nimsoft comparison script nimcomp.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Notes | Select-Object Name, Notes | Export-csv nimsoft.csv -NoTypeInformation
# Takes vcenall.csv and sorts only the Name columns and exports all fields. Backup/Restore comparison script bure.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Name | Export-csv bure.csv -NoTypeInformation
我认为您需要添加更多信息,但只需使用您已有的信息就可以尝试这种方法
Import-Csv C:\temp\test.csv -Header server,1,2,3,4 | ForEach-Object{
$_.server = (($_.server).split("(")[0]).Trim()
$_
}
我们导入 csv 数据并分配一个 header。如果您已经有一个,则可以省略此参数。
然后我们将每一行数据作为 object 检查。更改 server
数据,将其拆分为 space。如果此数据用于服务器名称,则可以安全地假设第一个 space 之前的所有内容都是服务器名称。这种方法取决于 space 在那里。我们也可以对 (
使用相同的逻辑,但如果 space 是一个保证,这会更容易。
因此我们更新 server
,然后使用 $_
将数据发送回管道。
示例输出
server 1 2 3 4
------ - - - -
GDR01W01SQ004 1 4 63 NY-TER-PLN-P-5N
GDR01L02D001 4 12 129 CO-FDP-STE-NP-5N
根据评论编辑
因为它是服务器显示名称,所以我将逻辑更改为基于“(”进行拆分。还使用 Split()
方法而不是 -split
运算符。