我有一个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文件及其信息,并将其格式化为带有标题和适当间距的报告。
有人可以帮我删除()和()之间的文本吗?
我希望每行的条目如下所示:
"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
答案 0 :(得分:0)
我认为您需要添加更多信息,但只需使用您所拥有的信息即可尝试这种方法
Import-Csv C:\temp\test.csv -Header server,1,2,3,4 | ForEach-Object{
$_.server = (($_.server).split("(")[0]).Trim()
$_
}
我们导入csv数据并分配标头。如果您已经有一个,则可以省略此参数。
然后我们将每行数据作为对象进行检查。通过将空格分开来更改server
数据。如果此数据用于服务器名称,则可以安全地假设第一个空格之前的所有内容都是服务器名称。这种方法取决于那里的空间。我们也可以使用与(
相同的逻辑,但如果空间是保证,这将更容易。
因此,我们更新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
运算符将逻辑更改为分割。