• 0

Powershell guidance


Question

I've been tasked with trying to produce some statistics and I'd like to try and automate it rather than running these scripts and manually combining the data. I will admit I'm a complete Powershell n00b so I barely know anything so this is probably some elementary crap but trying to find examples to guide me has confused the hell out of me.

 

In a nutshell, I want my script to connect to connect to both a Virtual Machine Manager instance AND a couple of Hyper-V hosts and then export specific VM information. I can do all of this individually but I cannot make it work as one script and ideally as mentioned I want the exported data pumped into a single CSV.

 

I know my problem is going to be because I'm writing this line by line as I would by running it manually and I need to build some sort of intelligence into it. I suspect I need to go down the whole "foreach" route but the few examples I've found I just could not get my head around how you structure each variable within the collection.

 

If someone can do this and it's simple enough it would be great to have notes so I can understand what each line is doing (I understand that is asking for a lot)

 

Import-Module -Name virtualmachinemanager, hyper-v
Get-SCVMMServer -ComputerName "uk-vmmgmt-vmm"
Get-Vm -ComputerName uk-clstr-031, uk-clstr-032
get-VM | select TotalSize, Status, ComputerNameString, CreationTime, CPUCount, Memory, Cloud, Owner | export-csv "C:\Temp\HVExport.csv"

I suspect my next issue will be that some of the attributes are not going to be the same between SCVMM and Hyper-V but one problem at a time :D

Link to comment
https://www.neowin.net/forum/topic/1309540-powershell-guidance/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

What seems to be stopping me is that the Get-SCVMMServer seems to be interfering in some way with the Get-VM in that it makes the -ComputerName invalid. If I run the Get-VM -ComputerName separately it works fine but when used in conjunction with the SCVMM Server line it stops being valid.

 

I tried my hand at the foreach stuff but I'm pretty much in the same place.

 

Import-Module -Name virtualmachinemanager, hyper-v
$VMM = Get-SCVMMServer -ComputerName "uk-vmmgmt-vmm"
$Host = Get-Vm -ComputerName uk-clstr-031, uk-clstr-032
 $Hosts = @($VMM, $Host)
 foreach ($thing in $Hosts) {
 get-VM | select TotalSize, Status, ComputerNameString, CreationTime, CPUCount, Memory, Cloud, Owner | export-csv "C:\Temp\HVExport.csv"
}

 

However, if someone can explain the element where I've used $thing and that seeming to be accepted despite not being defined anywhere that would be great. It just doesn't seem to make sense. I tried to use the example I found below in which this case $tree is the non-defined variable and can be changed to anything I want and still function.

 

Example;

 

 $trees = @("Alder","Ash","Birch","Cedar","Chestnut","Elm")

 foreach ($tree in $trees) {
   "$tree = " + $tree.length
 }

 

This topic is now closed to further replies.
  • Posts

    • GitHub customers rejoice as Command Palette deprecation is delayed by Usama Jawad GitHub Command Palette was announced back in 2021, and has been in preview ever since. For those unaware, this capability allows you to launch a search bar UX through the Ctrl + K shortcut, which offers you suggestions based on your current context. So you can quickly clone repositories, edit pull requests (PRs), and more directly through your keyboard, without navigating through cumbersome nested menus. Almost a week ago, GitHub announced that it is deprecating Command Palette due to low usage and popularity. This led to major backlash from the development community who criticized the move, noting that a preview feature that is disabled by default will obviously have low usage. Many customers cited its usefulness, noting that Command Palette has no alternative that is as powerful, and it's unfair for the company to remove a product due to low adoption, especially since it had not been marketed. Now, GitHub has decided to reverse course on its earlier decision (thanks, The Register) and delayed the deprecation of Command Palette indefinitely. It has updated its previous blog post and shared an identical announcement on a dedicated GitHub thread to indicate that after listening to customer feedback and specialized use-cases, it has realized that its own usage metrics do not reflect the current popularity of Command Palette. It understands that the tool is critical in many workflows and that it needs to re-evaluate its overall approach to navigation. During this period of assessment, GitHub Command Palette will continue to be available, much to the joy of GitHub customers, who have responded very positively to the thread. That said, it is important to note that GitHub's wording is a bit vague, and it doesn't concretely say that Command Palette is here to stay. It's possible that the company re-evaluates its usage metrics sometime in the future and reaches the same conclusion that it did last week.
    • I'd say "no thank you" to Recall. Let's turn it off as quicly as possible when installed. And Microsoft -please- don't let a "bug" re-install/re-configure it again.
    • Let's just wait until Patch Tuesday and have most of the bugs on this KB/download be taken care of & ironed out.
    • Just read that before coming here. So sad 😪 Always been one of my all time favorites whether with Sabbath or solo Glad he got the chance to do his 1 last show. Truly going home now.  
    • It comes just weeks after the Black Sabbath star took to the stage one final time with his band mates at Villa Park in Birmingham. He died "surrounded by love", a statement from his family said on Tuesday night. The family said: "It is with more sadness than mere words can convey that we have to report that our beloved Ozzy Osbourne has passed away this morning. "He was with his family and surrounded by love. We ask everyone to respect our family privacy at this time. "Sharon, Jack, Kelly, Aimee and Louis."     https://www.thesun.co.uk/tvandshowbiz/23619758/ozzy-osbourne-dead-black-sabbath-parkinsons/
  • Recent Achievements

    • Week One Done
      SmileWorks Dental earned a badge
      Week One Done
    • Community Regular
      vZeroG went up a rank
      Community Regular
    • Collaborator
      Snake Doc earned a badge
      Collaborator
    • Week One Done
      Snake Doc earned a badge
      Week One Done
    • One Month Later
      Johnny Mrkvička earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      586
    2. 2
      Michael Scrip
      198
    3. 3
      ATLien_0
      196
    4. 4
      +FloatingFatMan
      134
    5. 5
      Xenon
      122
  • Tell a friend

    Love Neowin? Tell a friend!