• 0

Powershell guidance


Question

Skiver

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 post
Share on other sites

1 answer to this question

Recommended Posts

  • 0
Skiver

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
 }

 

Link to post
Share on other sites
This topic is now closed to further replies.
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By News Staff
      Save 95% off this Complete Computer Networking eBook & Video Course Bundle
      by Steven Parker

      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store where you can save 95% off this Complete Computer Networking eBook & Video Course Bundle. Attain systems efficiency & security with 14+ hours of video content and 5 comprehensive e books on DevOps, Programming, AWS, CCNA, and more.



      This bundle consists of the following courses:

      The Ultimate Kubernetes Bootcamp by School of Devops [Video]
      Prepare for the CKA Exam — Master Container Orchestration with Kubernetes One Step at a Time AWS Certified Advanced Networking: Specialty Exam Guide [eBook]
      Build Your Knowledge & Technical Expertise as an AWS-Certified Networking Specialist Hands-On Network Programming with C [eBook]
      Learn Socket Programming in C & Write Secure and Optimized Network Codes Analyzing Network Traffic with Wireshark 2.6 [Video]
      Delve Into Network Traffic & Analyze Individual Protocol Data Units Active Directory Administration Cookbook [eBook]
      Actionable, Proven Solutions to Identity Management & Authentication on Servers and in the Cloud Hands-On PowerShell for Active Directory [Video]
      Use PowerShell for Active Directory to Eliminate Manual Labor with Quick Automation Tasks & Functions Effective Jenkins: Getting Started with Continuous Integration [Video]
      Learn Continuous Integration, Automate Your Jenkins Projects & Get Continuous Feedback for Your Upstream/Downstream Projects Hands-On Kubernetes Networking [Video]
      Unravel the Mystery of Networking in Your Kubernetes Cluster in a Pragmatic Manner CCNA Cyber Ops SECOPS: Certification Guide 210-255 [eBook]
      Develop Your Cybersecurity Knowledge to Obtain CyberOps Certification Hands-On Linux for Architects [eBook]
      Design & Implement Linux-Based IT Solutions Good to know
      Updates included Length of time users can access after purchase: lifetime Redemption deadline: redeem your code within 30 days of purchase For a full description, specs, and author info please click here.

      Here's the deal:
      This Complete Computer Networking eBook & Video Course Bundle normally costs* $746 but it can be yours for just $29.99 for a limited time, that's a saving of $716.01 (95%) off the price.

      >> Get this deal, or learn more about it here <<
      See all Online Courses on offer. This is a time limited deal.
      Get $1 credit for every $25 spent · Give $10, Get $10 · 10% off for first-time buyers.

      Not for you?
      If this offer doesn't interest you, why not check out the following offers:



      The Win Your Dream 2020 Tesla Model 3 Giveaway Ivacy VPN - 5 year subscription for just $0.99 per month NordVPN - 2 year subscription at up to 68% off +3 months for free! Private Internet Access VPN - subscriptions at up to 71% off Unlocator VPN or SmartDNS - unblock Geoblock with 7-day free trial Subscribe to Neowin - for $14 a year, or $28 a year for Ad-Free experience Disable Sponsored posts · Neowin Deals · Free eBooks · Neowin Store

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs.

    • By Abhay V
      Microsoft's next-gen Azure Stack HCI is now generally available
      by Abhay Venkatesh



      Back during its inspire conference this year, Microsoft announced a preview of the next generation of Azure Stack HCI (hyperconverged infrastructure), a service that lets businesses leverage Azure services with their hybrid infrastructure and on-premise options through Azure Arc. This helps customers view and manage virtual desktops (VDIs) and databases through the Azure portal, run virtualized workloads on-premises, and more.

      Today, the company announced that Azure Stack HCI is now generally available, adding that it tweaked the service with feedback from its customers during the preview period. This helped deliver a “full-featured offering” that simplifies management, offers stretch clustering to easily extend clusters to multiple sites and ease disaster recovery, and integrates natively with Azure Arc and other services.

      Advantages of Stack HCI include the ability for customers to run the offering on their existing hardware – in addition to the various environments – that are validated by Microsoft, further reducing the costs of deploying the solution and integrating with Azure services. The Redmond firm says that the service is “familiar” for “Hyper-V and server admins” and also integrates with other services from the company such as System Center, Active Directory, and more.

      Additionally, the company also touts its partnerships with vendors such as Dell, Lenovo, and DataON that helps provide an “integrated system experience” through systems that are pre-racked and come with software, hardware, and drivers pre-installed. These systems also add a new Azure Stack HCI full-stack updates feature that helps admins easily apply validated full-stack updates to the clusters. Lastly, the firm says that it is also bringing Independent Software Vendor support for Azure Stack HCI with the addition of ISVs such as CommVault, DataDog, Veeam, and Veritas, providing users with more options.

      Those interested to try out the service can head here to register for a free 30-day trial. Earlier this year, the company touted the savings advantages of the service’s “flexible per core subscription model “. You can head to the pricing information page here for more details.

    • By zikalify
      How to install and use Neofetch on desktop and mobile
      by Paul Hill

      If you’ve spent any time looking around Linux subreddits, you may have seen some desktop screenshots where the user has their terminal displaying their system’s specs next to the logo of the operating system they use. While other programs can display information like this, one of the common options is called Neofetch – a program written in bash and available on all the popular operating systems and niche ones.

      In this guide, I’ll show you how to install Neofetch on Windows, Mac, popular Linux distributions, Android and even iOS. In addition to installation, we’ll walk through some of the more advanced commands that you can run with Neofetch to customise the output.



      Neofetch on Windows
      On Windows, you will need to install Neofetch using a tool called Scoop, a command-line installer for Windows. To install Scoop, you must be using Windows 7 SP1+ / Windows Server 2008+ and have PowerShell 5 and .NET Framework 4.5. Once you’ve got these, you’ll need to enter the following command in PowerShell: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser, affirm the changes if it asks.

      Next, you’ll want to install Scoop using PowerShell. To do this, use the following command: Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh'). You’ll be able to tell if Scoop has been installed by running: scoop help. Before installing Neofetch, you'll need to install git using Scoop, just type: scoop install git.

      If all of those are installed properly, you’re now ready to install Neofetch by typing: scoop install neofetch. Once that is installed just type: neofetch into PowerShell and it will display your system’s specs and the Windows logo.

      Neofetch on Mac
      Getting Neofetch working on a Mac is quite a bit easier than the process on Windows. Simply open the Mac Terminal and paste the following command to install Homebrew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". With that installed you can type: brew install neofetch and once it’s done just type: neofetch to display your specs next to the Apple logo.



      Neofetch on Linux
      Neofetch is easy to install on most Linux distribution, this guide includes instructions for installing Neofetch on Arch, Debian, Fedora, OpenSUSE, Ubuntu and their derivatives. For most of these, you will be asked to provide an administrator password.

      Arch
      Install the package from the command line with the following command:

      pacman -S neofetch

      Debian
      Install the package from the command line with the following commands:

      sudo apt-get update

      sudo apt-get install neofetch

      Fedora
      Install the package from the command line with the following command:

      sudo dnf install neofetch

      OpenSUSE
      Install the package from the command line with the following command:

      sudo zypper install neofetch

      Ubuntu
      Install the package from the command line with the following commands:

      sudo apt update

      sudo apt install neofetch

      Once you have completed the Neofetch installation on any Linux system, you can run the program by just typing: neofetch into the command line. You will be shown the specs of your computer next to the logo of your particular Linux distribution.

      Neofetch on Android


      Using Neofetch on Android is fairly straight forward, just head to the Google Play Store and download the Termux terminal emulator, it can also be downloaded from F-Droid if you do not have access to the Play Store for some reason.

      Once that’s installed, open it up and type: pkg update and select yes to any questions, this is usually achieved by typing y and pressing enter. Run this command twice just to make sure everything is ready. After you’ve done that, type: pkg install neofetch, when that’s complete type: neofetch. You can pinch to zoom out if any of the output is cut off.

      Neofetch on iOS
      Neofetch can only be downloaded on iOS with a jailbroken device. You should use a package manager like Sileo to search for and install neofetch. Jailbreaking and installing a package manager is beyond the scope of this tutorial but you can find more information about Sileo on the project’s website.

      Advanced options
      Neofetch’s default settings should be sufficient for most people but it does come with a decent amount of customisability. If you are comfortable reading help files just type: neofetch --help to see the range of options available to you, if not, here are some pretty cool selections.

      When you run Neofetch it will display your operating system’s logo by default but you can make it display any logo by adding to the command. To do this just type: neofetch --ascii_distro distroname and replace distroname with something like ubuntu, fedora, windows etc. Amending _old to the operating system's name will load the old ASCII image if your selection has one. You can find a full list of supported operating systems within the help file, to access that type: neofetch --help.

      If you want to personalise your Neofetch output you can create your own ASCII art and supply that to Neofetch. Once you have your ASCII art ready save it a .asc file, then type: neofetch --ascii /path/to/filename.asc.

      Conclusion
      The advanced features outlined above are not exhaustive by any means, there are lots of settings that you can choose by diving into the Neofetch help file but the ones outlined above will help you get started. If you enjoy tweaking your desktop and want to show it off online, be sure to have your Neofetch output in the foreground so you can show off your specs too!

    • By Usama Jawad96
      Microsoft announces Az Predictor to improve usability of Azure PowerShell
      by Usama Jawad

      Azure PowerShell is a very handy utility that allows you to manage Azure resources directly from the PowerShell commandline. The module currently offers over 4,000 cmdlets that users can utilize to effectively manage Azure resources, with 10 parameters being available on average per cmdlet.

      To make Azure PowerShell easier to use for beginners and experts alike, Microsoft has today announced the availability of Az Predictor.

      Microsoft says that people often make mistakes when experimenting with new modules due to the large number of cmdlets and parameters available, and then have to refer to the documentation to achieve their goals. To tackle this problem via a more streamlined solution, the company has announced Az Predictor, which it describes as an "intelligent command completion module", specifically for Azure PowerShell. Microsoft says:

      As of now, Az Predictor offers two modes: the first is inline mode which offers suggestions with a reduced level of opacity, while the second is listview mode which lists all suggestions starting with the next line.

      Although Az Predictor is available for use right now, Microsoft has clarified that it is currently in preview, and depending upon feedback, it may be merged with the Az wrapper module. In order to utilize Az Predictor today, you need the following configuration:

      PowerShell 7.1+ PSReadline 2.2.0-beta1 In order to make full use of Az Predictor, an internet connection is necessary as it makes calls to an API that returns result based on the current session's context. However, Microsoft is exploring ways to support environments which offer limited or no internet availability. The company further stated that:

      Az Predictor is open source, just like Azure PowerShell itself. Microsoft has outlined how to get started with the module in its blog post and has also encouraged users to visit the GitHub repository in case they would like to raise issues or suggest changes to the utility.

    • By News Staff
      Get the Complete Microsoft Azure eBook & Video Course Bundle for just $29.99
      by Steven Parker

      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store, where you can save 95% off this Complete Microsoft Azure eBook & Video Course Bundle. Lead your business towards success with 15+ hours of video content and six eBooks on Azure cloud solutions, integration, networks, and more.



      This bundle consists of the following courses:

      Implementing Azure Solutions [eBook]
      Deploy & Manage Azure Containers and Build Azure Solutions with Ease Serverless Integration Design Patterns with Azure [eBook]
      Build Powerful Cloud Solutions That Sustain Next-Generation Products Azure Networking Cookbook [eBook]
      Distribute Network Traffic, Optimize Application Delivery & Defend Network-Level Threats Azure PowerShell Quick Start Guide [eBook]
      Leverage PowerShell to Perform Many Day-to-Day Tasks in Microsoft Azure Migrating Applications to the Cloud with Azure [eBook]
      Modernize Your Apps with Microsoft Azure by Moving Web, Desktop, & Mobile Apps to the Cloud Mastering Identity & Access Management with Microsoft Azure [eBook]
      Empower Users by Managing & Protecting Identities and Data DevOps with Azure [Video]
      Plan, Manage & Monitor Your Applications with Azure Modernize Node.js Web Apps with Azure App Service [Video]
      Refactor Nodes.js Web Apps & Deploy, Scale, and Monitor Them on Azure App Store Azure Platform as a Service, Web & API Application Deployment [Video]
      Build, Manage & Deploy Your Own PaaS Cloud with Help of Azure Services Azure Cognitive Services for Developers [Video]
      Make Your Applications Smarter, The Easy Way with Azure Cognitive Services Good to know
      Updates included Length of time users can access this course: lifetime Redemption deadline: redeem your code within 30 days of purchase For terms, certification and instructor info, click here.

      Here's the deal:
      This Complete Microsoft Azure eBook & Video Course Bundle normally costs $703 but it can be yours for only $29.99, that's a saving of $673.01 (95%) off!

      >> Get this deal, or learn more about it <<
      See all Online Courses on offer. This is a time limited deal.
      Get $1 credit for every $25 spent · Give $10, Get $10 · 10% off for first-time buyers.

      Not for you?
      If this offer doesn't interest you, why not check out the following offers:



      The Win Your Dream 2020 Tesla Model 3 Giveaway 20% off Ivacy VPN subscription with coupon code IVACY20 NordVPN subscription at up to 68% off for a 2 year plan Private Internet Access VPN subscription at up to 77% off Unlocator VPN or SmartDNS unblock Geoblock with 7-day free trial Subscribe to Neowin for $14 a year, or $28 a year for Ad-Free experience Disable Sponsored posts · Neowin Deals · Free eBooks · Neowin Store

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs.