• 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

    • The dev is a snowflake for deciding that a project he’s not getting paid to do isn’t worth the attacks? Interesting.
    • Microsoft explains how organizations can use Intune to upgrade from Windows 10 to Windows 11 by Usama Jawad The timer for Windows 10's end of life is counting down and while organizations can pay for Extended Security Updates (ESU), it might not be financially feasible for them to do so. In cases like these, it is in both the enterprise customer and Microsoft's benefit to upgrade to Windows 11 as seamlessly as possible. To that end, the Redmond tech giant has published a detailed guide explaining how companies can upgrade to Windows 11 through Intune. Microsoft has emphasized that this guide is primarily intended for domain-joined or co-joined Windows 10 PCs in order to perform a cloud-native migration to a state where the device is running on Windows 11 and is Entra-joined with Intune. To get started, customers obviously need to confirm that their hardware meets the requirements for Windows 11, which includes the dreaded TPM 2.0. This can be confirmed through Microsoft Configuration Manager or Endpoint Analytics in Intune. In addition, tools like Windows Autopatch, Configuration Manager, and Windows Server Update Services (WSUS) should be employed to update Windows 10 devices to the latest supported version, which is version 22H2. IT admins should also synchronize identities from Active Directory (AD) to Entra ID, configure and validate a hybrid join, prepare the Intune environment with the required licenses and admin roles, and enable co-management in Intune and Configuration Manager. Next, Group Policy Objects (GPOs) should be rationalized, redundant policies should be replaced, Intune configuration profiles should be set up, and Intune policies should be configured to deploy the update in phases. Then, Windows Autopatch should be leveraged to deliver the update and monitor the rollout. Applications should also be migrated from Configuration Manager to Intune for packaging, testing, deploying, and assigning them to the correct device groups. IT admins should also be vigilant in decommissioning the old deployments in Configuration Manager and updating the relevant documentation. The final step of this process involves transitioning from a domain network-joined setup to Entra ID-joined. This is a multi-step process, so make sure to check out the details here. Microsoft believes that this cloud-native migration approach will ensure centralized and streamlined management, enhanced security, an optimized UX, reduced reliance on legacy infrastructure, and allow IT admins to leverage Copilot in Intune.
    • ^ this (though I use Firefox). So tired of this "AI" being shoved into every damn thing. Copilot/Gemini....sick of it all. I am fully convinced it's just a way to mine and sell your data... unfettered ... under the guise of AI. I hate conspiracy theories...but there it is.
    • This is really the snowflake generation, isn’t it yes, whatever you do publicly, there will always be haters. Sad but true. Yes, I’ve lead such a project too.
    • I ran into this before. Clearing the cache and restarting my computer fixed it.
  • Recent Achievements

    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
    • One Month Later
      James_kobe earned a badge
      One Month Later
    • Week One Done
      James_kobe earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      656
    2. 2
      ATLien_0
      253
    3. 3
      Xenon
      167
    4. 4
      neufuse
      147
    5. 5
      +FloatingFatMan
      121
  • Tell a friend

    Love Neowin? Tell a friend!