Hey everyone! I'm reaching out for some help with a challenging asset-management script I've been developing. I've been working as a sysadmin for a medium-sized company in Europe, and I need this script to monitor our virtual machines (VMs). It connects to VMware's API to pull various data, then cross-references it with Active Directory and a few other tools. The end goal is to generate a daily CSV file for our asset database. However, I'm facing some headaches:
1. When the script identifies a VM that's "powered off," it should set a timestamp. The next time the script runs, if this timestamp is over 14 days old, the VM's status should change to "inoperative." Unfortunately, the timestamp isn't being interpreted correctly. It either gets overwritten or fails to update after 14 days. I suspect this issue arises when the timestamp is exported to a CSV and imported back.
2. There should always be two output files created: one for today and a master file for comparison with the previous day. However, sometimes, the script inexplicably fails to create the master file even though my code doesn't report any errors. Oddly, if I rerun the command, it appears.
I'd appreciate any advice or debugging tips you can offer. Thanks in advance!
2 Answers
Sounds like you’re on the right path, but those little details can be tricky. For the timestamp issue, definitely double-check the formatting as mentioned. About the master file, it might be helpful to log more about what's happening immediately before the file creation—maybe there’s an exception being swallowed somewhere—that way you can pinpoint the failure.
Remember to also test each environment change separately to see where it breaks! Good luck!
That’s solid advice. Always helps to be methodical!
Hey, nice job tackling such a big project for your first complex script! From what I see, there are a couple of areas that could be causing your issues:
1. **Timestamp Problem**: In your `ParseExact`, you're using `"dd.MM.YYYY HH:mm:ss"`. The `"YYYY"` is meant for week years in .NET. Try changing it to `"dd.MM.yyyy HH:mm:ss"`. This should help stabilize how the timestamp is managed and prevent it from getting overwritten unintentionally.
Also, when setting the current date, make sure it's formatted similarly:
`$currentDate = (Get-Date).ToString("dd.MM.yyyy HH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture)`
This ensures the format remains consistent throughout.
2. **Master File Not Being Created**: It sounds like this might happen if `$newestFile` ends up being empty or if `$filename_latest` doesn't exist at the time it's being referenced. You might want to add some checks, like:
`if ($newestFile -and $newestFile.Count -gt 0) { ... }`
Additionally, if `$filename_latest` isn't present, create it before the comparisons begin:
`if (-not (Test-Path $filename_latest)) { Copy-Item $newestfilename -Destination $filename_latest }`
Lastly, sometimes file writing isn't instantaneous in PowerShell, so consider adding `Start-Sleep -Seconds 2` after file operations as a precaution.
Hope that helps clear up some of your frustrations!
This is a super helpful response; thanks for sharing your knowledge!
Definitely need to keep those formatting rules in mind. It's so easy to overlook!

Exactly! Isolate your changes and find where things go awry.