【发布时间】:2015-06-23 00:37:27
【问题描述】:
我有一个运行和收集信息并将其放入 .csv 文件的 powershell 脚本。信息示例如下所示,每行以唯一的服务器名称开头,后跟包含一对 ( ) 的随机唯一标识符。
"GDR01W01SQ004 (e785754f-eeb1)","1","4","63","NY-TER-PLN-P-5N"
"GDR01L02D001 (4b889a4d-d281)","4","12","129","CO-FDP-STE-NP-5N"
我有第二个 powershell 脚本,它运行并获取此 .csv 文件及其信息并将其格式化为带有标题和适当间距的报告。
有人可以帮我删除 ( ) 和 ( ) 之间的文字吗?
我希望每行的条目如下所示:
"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
【问题讨论】:
-
你能告诉我们你的脚本吗?
-
即使我在黑暗中拍摄,因为我不太了解 powershell,但我确信正则表达式可以工作。这是一个链接,它显示了如何从一对 () 中提取内容,您可以使用该 stackoverflow.com/questions/9113231/…
-
您的 csv 数据是否包含标题?
标签: string powershell csv text replace