If I understand correctly it is just a rename of the VM's Displayname, that can be done with a Set-VM cmdlet.
No need to power on the VM for such a rename
Original Message:
Sent: Mar 05, 2025 10:57 AM
From: rgp982
Subject: Retire VMs
Excellent. That did it.
Now for a related but unrelated question.
During our waiting period where a VM is off for X amount of days prior to deleting this company also changes the name of the VM. ABC-XYZ-01 will be renamed to !DEOM_ABC-XYZ-01_datetodelete.
Do you know of a way (without turning the box back on) to rename it back to ABC-XYZ-01 ? Obviously I can call it !DEOM_ABC-XYZ-01_datetodelete in the script but that's not useful in AD, SCCM or DNS.
Original Message:
Sent: Mar 05, 2025 01:28 AM
From: LucD
Subject: Retire VMs
I assume that the line "$vm = Get-VM $server" is producing that error?
Since you import the names from a CSV, there should be a column name in the CSV that contains the name of the VM.
Did you try with "$vm = Get-VM $server.Name", provided that column is named "Name"?
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Mar 04, 2025 03:20 PM
From: rgp982
Subject: Retire VMs
I have this new retire VM script that I'm working on. It removes the server from VMware, deletes it from AD, deletes DNS entry and deletes the server from SCCM. I keep getting an error saying that 'VM with name '@{Name=legitservername}' was not found. I have no idea why as the server name most certainly exists.
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$vCenterServers =@("lots of vCenters")
# Define the list of servers to be deleted
$csvpath = ".\vmnames.csv"
$Servers = Import-csv $csvpath
# VMware - Remove VMs
#Import-Module VMware.PowerCLI
Connect-VIServer -Server $vCenterServers
foreach ($server in $servers) {
$vm = Get-VM $server
if ($vm) {
Remove-VM -VM $vm -DeletePermanently -Confirm:$false
Write-Output "VM $server removed from VMware."
} else {
Write-Output "VM $server not found in VMware."
}
}
#Disconnect-VIServer -Confirm:$false
# DNS - Remove DNS records
#Import-Module DNSServer
foreach ($server in $servers) {
Remove-DnsServerResourceRecord -ZoneName "aDNSzone -Name $server -RRType "A" -Confirm:$false
Write-Output "DNS record for $server removed."
}
# Active Directory - Remove computer accounts
#Import-Module ActiveDirectory
foreach ($server in $servers) {
$adComputer = Get-ADComputer -Identity $server -ErrorAction SilentlyContinue
if ($adComputer) {
Remove-ADComputer -Identity $server -Confirm:$false
Write-Output "AD computer account for $server removed."
} else {
Write-Output "AD computer account for $server not found."
}
}
# SCCM - Remove devices
$SiteCode = "A Site Code"
$ProviderMachineName = "SCCM server"
$initParams = @{}
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module "F:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1" @initParams
}
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}
Set-Location "$($SiteCode):\" @initParams
foreach ($server in $servers) {
$device = Get-CMDevice -Name $server
if ($device) {
Remove-CMDevice -DeviceName $server -Confirm:$false
Write-Output "SCCM device $server removed."
} else {
Write-Output "SCCM device $server not found."
}
}