I am building a script to grab DNS A record > ping them all > if they do not ping write to a file > get-vm cmdlet > if they do not exists write to another file.
Below is the part that is not working. It is writing all the VM's to the file. Can someone help me get this to work. I have tried try catch loops as well and can not get it to work.
Connect-VIServer $vcenter -User $username -Password $password
$Exists = Import-Csv C:\tmp\test1.csv
$array = @()
foreach ($VMName in $Exists){
$not = get-vm -name $VMName -ErrorAction SilentlyContinue
$writeme = $VMName
If ($not){
# Write "VM is there $VMName"
}
Else {
# Write "VM not there $VMName $writeme"
$array += $writeme
}
}
$array | Export-Csv c:\tmp\test2.csv -NoTypeInformation -UseCulture
Disconnect-VIServer $vcenter -Confirm $false -Force
That is probably because of the layout of your input CSV file.
If it looks like this
Name
vm1
vm2
The following script should do the trick
Connect-VIServer $vcenter -User $username -Password $password
$Exists = Import-Csv C:\tmp\test1.csv
$array = @()
foreach ($VM in $Exists){
$not = get-vm -name $VM.Name -ErrorAction SilentlyContinue
If ($not){
# Write "VM is there $VMName"
}
Else {
# Write "VM not there $VMName $writeme"
$array += $VM.Name
}
}
$array | Export-Csv c:\test2.csv -NoTypeInformation -UseCulture
Disconnect-VIServer $vcenter -Confirm $false -Force
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I hope I understood the question correctly.
Try like this
Connect-VIServer $vcenter -User $username -Password $password
$Exists = Import-Csv C:\tmp\test1.csv
$array = @()
foreach ($VMName in $Exists){
$not = get-vm -name $VMName -ErrorAction SilentlyContinue
If ($not){
# Write "VM is there $VMName"
}
Else {
# Write "VM not there $VMName $writeme"
$array += $VMName
}
}
$array | Export-Csv c:\tmp\test2.csv -NoTypeInformation -UseCulture
Disconnect-VIServer $vcenter -Confirm $false -Force
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD
It did the same thing. It adds to the array when the vm is their or not. I need it to only add the vm to the array if the vm is not in vCenter.
That is probably because of the layout of your input CSV file.
If it looks like this
Name
vm1
vm2
The following script should do the trick
Connect-VIServer $vcenter -User $username -Password $password
$Exists = Import-Csv C:\tmp\test1.csv
$array = @()
foreach ($VM in $Exists){
$not = get-vm -name $VM.Name -ErrorAction SilentlyContinue
If ($not){
# Write "VM is there $VMName"
}
Else {
# Write "VM not there $VMName $writeme"
$array += $VM.Name
}
}
$array | Export-Csv c:\test2.csv -NoTypeInformation -UseCulture
Disconnect-VIServer $vcenter -Confirm $false -Force
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
it was the layout but i still need the variable in-between the array and the csv for some reason or it would only print the number of characters.
$Exists = Import-Csv C:\tmp\noping.csv -Header "VMs"
$array = @()
foreach ($VMName in $Exists){
$not = get-vm -name $VMName.VMs -ErrorAction SilentlyContinue
If ($not){
}
Else {
Write "VM not there $VMName $writeme"
$writeme = $VMName
$array += $writeme
}
}
$array | Export-Csv c:\tmp\nodns.csv -NoTypeInformation -UseCulture
Disconnect-VIServer "vcenter" -Confirm $false -Force