PowerCLI读取数据的CSV文件需要保存文件操作

时间:2016-12-01 19:28:19

标签: vba excel-vba powershell csv powercli

我正在使用VBA脚本将数据从Excel电子表格导出到CSV文件,以便通过带有PowerCLI脚本的Import-CSV命令自动执行VM创建过程。我正在使用的VBA脚本是:

Function BuildValuesString(colIndex As String, rows As String) As String
    Dim val As Variant

    For Each val In Split(rows, ",")
        If Cells(val, colIndex) <> "" Then BuildValuesString = BuildValuesString & Cells(val, colIndex).Value & ","
    Next val
End Function
Function BuildNullStrings(numNullStrings As Long) As String
    Dim iNullStrings As Long

    For iNullStrings = 1 To numNullStrings
        BuildNullStrings = BuildNullStrings & "" & ","
    Next iNullStrings
End Function


Sub WriteCSVFile2()

    Dim My_filenumber As Integer
    Dim logSTR As String

    My_filenumber = FreeFile

    logSTR = logSTR & "1." & ","
    logSTR = logSTR & "2." & ","
    logSTR = logSTR & "3." & ","
    logSTR = logSTR & "4." & ","
    logSTR = logSTR & "5." & ","
    logSTR = logSTR & "6." & ","
    logSTR = logSTR & "Name" & ","
    logSTR = logSTR & "Cluster" & ","
    logSTR = logSTR & "VLAN" & ","
    logSTR = logSTR & "NumCPU" & ","
    logSTR = logSTR & "MemoryGB" & ","
    logSTR = logSTR & "C" & ","
    logSTR = logSTR & "D" & ","
    logSTR = logSTR & "App" & ","

    logSTR = logSTR & Chr(13)

    logSTR = logSTR & BuildValuesString("C", "18,19,20,21,22")
    logSTR = logSTR & BuildValuesString("C", "26,27,28,29,30,31,32,33")

    logSTR = logSTR & Chr(13)

    logSTR = logSTR & BuildValuesString("C", "18,19,20,21,22")
    logSTR = logSTR & BuildValuesString("C", "37,38,39,40,41,42,43,44")

    logSTR = logSTR & Chr(13)

    logSTR = logSTR & BuildValuesString("C", "18,19,20,21,22")
    logSTR = logSTR & BuildValuesString("C", "48,49,50,51,52,53,54,55")


Open "Z:\2016\Requests(Test)\" & ThisWorkbook.Name & ".csv" For Append As #My_filenumber
    Print #My_filenumber, logSTR
Close #My_filenumber

End Sub

将数据导出到CSV文件后,我运行PowerCLI脚本,使用在使用VBA脚本提取的“名称”列下输入的名称创建VM:

# Specify vCenter Server, vCenter Server username and vCenter Server user password
 write-host “Please specify vCenter Server and enter credentials” -foreground green
 $vc = read-Host "Connect to which vCenter Server?"
 write-host “Connecting to vCenter Server $vc” -foreground green

 $CSVPath = "$ScriptRoot\PROD_VMRequestTEST.xlsx.csv"    

 Connect-VIServer -Server $vc

 #$CSVFile = Import-Csv -Path $CSVPath

 Import-Csv -Path $CSVPath

 # Specify vCenter Server Virtual Machine & Templates folder
 $Folder = “Discovered virtual machine”
#
# Specify the vSphere Cluster

$Cluster = $_.Cluster

$esx = Get-Cluster $Cluster | Get-VMHost -State connected

Import-Csv -Path $CSVPath | %{

  Write-Host “Creation of VM $($_.Name) initiated”  -ForegroundColor green

  New-VM -Name $_.Name -VMHost ($esx | Get-Random) -Location $Folder

  Write-Host "Designating VLAN - '$($_.VLAN)'" -ForegroundColor green

  $VMhost = Get-vmhost $_.VMHost  
  $PortGroup = Get-VirtualPortgroup -name $_.VLAN -VMhost $VMhost  

  Write-Host "Power On of the VM $($_.Name)initiated" -ForegroundColor green

  Start-VM -VM $_.Name-confirm:$false -RunAsync

}

如果我在将数据导出到CSV文件后尝试直接运行PowerCLI脚本,则会遇到错误。错误说:

“Import-Csv:成员”1“已经存在。”

如果我手动打开CSV文件并手动在CSV文档中重新键入列名“名称”并保存文件,则可以解决此错误。如果我只是按下退格键并重新保存文档,它似乎也可以绕过这个错误,不做任何改动。可能导致这种情况的原因是什么?

我想避免这样做,有什么办法可以避免每次在运行PowerCLI脚本之前手动重新保存文档吗?

0 个答案:

没有答案