
close
close
In “A PowerShell Tip for Selecting Data Easily,” I guided you from the process of working with objects in the pipeline. I want to bring that topic and scenario back up to explore a few more reasons why I think this is important. If you missed the first article, take a moment to get caught up so you’ll understand my examples.
In the original code sample, the author was creating a tabular separated file. That is, a CSV that uses a tab instead of a comma as the delimiter. This is something I see people struggle with all the time. They spend a lot of energy trying to create strings and saving them to a file when all they need to do is take some time to read PowerShell help and experiment with the Export-CSV cmdlet. This is a one line expression that creates practically the same result as the original, text-oriented code.
Get-VM -name "chi*" | where {$_.state -eq "running"} |
Select @{Name="Date";Expression={Get-Date}},Status,Uptime,State,Name |
Export-Csv -Path c:\work\running.csv -Delimiter "`t" -NoTypeInformation
Personally, I would have stuck with the default comma delimiter, but as you can see it isn’t difficult to modify.
A tab delimited file (Image Credit: Jeff Hicks)
advertisment
Get-VM -name "chi*"| foreach -begin {[email protected]()} -process { if ($_.state -eq "running") { #log it $running+= $_ } #if running else { #start it $_ | Start-VM -WhatIf } #else start it } -end { #log running $running | Select @{Name="Date";Expression={Get-Date}},Status,Uptime,State,Name | Format-Table -AutoSize | Out-File -FilePath C:\work\vmreport.txt -Encoding ascii }
As I’ve said before, this isn’t a Hyper-V tutorial. I just happen to be using Get-VM and Start-VM for my example. When I run this in the PowerShell ISE, I get the WhatIf message for the single VM that’s not running:
The WhatIf scenario (Image Credit: Jeff Hicks)
#requires -version 4.0 #requires -module Hyper-V [cmdletbinding(SupportsShouldProcess)] Param( [Parameter(Position=0,HelpMessage="Enter a virtual machine name")] [ValidateNotNullorEmpty()] [string]$Name = "*", [Alias("cn")] [ValidateNotNullorEmpty()] [string]$Computername = $env:Computername, [ValidateNotNullorEmpty()] [string]$Path = "D:\work\vms.txt" ) Try { Write-Verbose "Getting virtual machines $Name from $Computername" $vms = Get-VM -name $name -ComputerName $Computername -ErrorAction Stop $vms | foreach -begin {[email protected]()} -process { if ($_.state -eq "running") { #log it $running+= $_ } #if running else { #start it Write-Verbose "Starting virtual machine $($_.Name)" $_ | Start-VM } #else start it } -end { #log running Write-Verbose "Creating log file $path" $running | Select @{Name="Date";Expression={Get-Date}},Status,Uptime,State,Name | Format-Table -AutoSize | Out-File -FilePath $Path -Encoding ascii } } #Try Catch { Write-Warning "Failed to get virtual machines on $computername. $($_.exception.message)" } #Catch #end of script
I’ve added some parameters, including validation tests, error handling, and support for WhatIf. This makes it flexible and easy-to-use.
Using a PowerShell script to save typing (Image Credit: Jeff Hicks)
More from Jeff Hicks
advertisment
Petri Newsletters
Whether it’s Security or Cloud Computing, we have the know-how for you. Sign up for our newsletters here.
advertisment
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri
Log in to save content to your profile.
Article saved!
Access saved content from your profile page. View Saved
Join The Conversation
Create a free account today to participate in forum conversations, comment on posts and more.
Copyright ©2019 BWW Media Group