tag:blogger.com,1999:blog-25016517801272749312024-03-07T23:20:05.865-08:00> Powershell-Everyday-FAQParadisJnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-2501651780127274931.post-13815040467715538422014-06-13T07:39:00.001-07:002014-06-13T07:39:21.851-07:00How to call a Powershell script from the command line and return an ERRORLEVEL<p>Sometimes you want to call a powershell script from the command line and get an ERRORLEVEL a the exit if an exception is throw in the script (ex: With a scheduled task).</p> <p>In Powershell v2 you could do this :</p> <div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:1642c6a6-c19b-4f99-b754-20617d676629" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: powershell;gutter:false;auto-links:false;toolbar:false;wrap-lines:false;">powershell.exe -noprofile -command ". c:\test-exit.ps1 -erroraction stop;exit $LastExitCode"</pre></div>
<p>Unfortunately this does not work anymore in Powershell v4 (always return ERRORLEVEL 0). It looks like the <span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; font-weight: bold !important; color: #069 !important">$LastExitCode</span> is not define anymore for a script by Powershell (only for an external process).  But Powershell seems to now behave as expected and exit with an ERRORLEVEL=1 if any error occurs in your script. So now, the following code should works :</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d4aa64af-d30e-4cb3-94bf-e7b41453c4c6" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: powershell;">powershell.exe -noprofile -command ". c:\test-exit.ps1 -erroraction stop"</pre></div>
<p>If you really want to check if your script was successful and return a special ERRORLEVEL, you should use the <span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; font-weight: bold !important; color: #069 !important">$?</span> variable :</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7f251c4e-54ef-45c3-9dd2-5029cf4901a1" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: powershell;">powershell.exe -noprofile -command ". c:\test-exit.ps1 -erroraction stop;if ($?){exit 0}else{exit 99}"
</pre></div>
<p>Don’t forget :</p>
<ul>
<li>To use dote-sourcing, the small dot (.) in  front of the script name.</li>
<li>To use the <span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; font-weight: bold !important; color: #069 !important">–command</span> parameter instead of the <span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; font-weight: bold !important; color: #069 !important">–file</span> parameter. The two parameters behave differently.</li>
<li>To set the <span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; font-weight: bold !important; color: #069 !important">–erroraction stop</span>. If you keep the default value of <span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; font-weight: bold !important; color: #069 !important">continue</span>, your script will not stop and the ERRORLEVEL will not be set to 1.</li>
</ul> ParadisJnoreply@blogger.com0tag:blogger.com,1999:blog-2501651780127274931.post-79003316357568304512013-02-13T12:33:00.001-08:002013-02-13T12:33:45.136-08:00How to add –whatif support to a function<p> </p> <div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:a07fc79b-bf0f-4ebf-81fb-185fcd1da5e3" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: powershell;gutter:false;auto-links:false;wrap-lines:false;">Function Get-HelloWorld {
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="Low")]
param()
$message="Hello world"
if ($PSCmdlet.ShouldProcess($message,"Write-host")) {
write-host $message
}
}</pre></div>
<p><span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; font-weight: bold !important; color: #069 !important">Get-HelloWorld -whatif</span> will produce :
<br />
<table style="border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; vertical-align: top; border-collapse: collapse; border-bottom: #cccccc 1px solid; color: #ffffff; border-left: #cccccc 1px solid; background-color: #012456"><tbody>
<tr>
<td style="padding-bottom: 4px; padding-top: 4px; padding-left: 4px; padding-right: 4px">
<pre style="font-family: consolas, 'Courier New', courier, monospace">What if: Performing operation "Write-host" on Target "Hello world".</pre>
</td>
</tr>
</tbody></table>
</p>
<p>As a bonus, you will also be able to use the <span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; font-weight: bold !important; color: #069 !important">-confirm</span> and –<span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; font-weight: bold !important; color: #069 !important">verbose</span> parameter</p> ParadisJnoreply@blogger.com0tag:blogger.com,1999:blog-2501651780127274931.post-74533524980854485722010-04-07T18:05:00.001-07:002010-04-07T18:06:36.839-07:00How to get the script’s directory ?<p>Sometimes you want to know what directory you script is in, here’s a function that return that value :</p> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:1ea97659-91f2-4eb7-8459-fb95668bd888" class="wlWriterEditableSmartContent"><pre class="brush: powershell;">function get-scriptdirectory {
# .SYNOPSIS
# Return the current script directory path, compatible with PrimalScript 2009
# Equivalent to VBscript fso.GetParentFolderName(WScript.ScriptFullName)
# Requires PowerShell 2.0
#
# .DESCRIPTION
# Author : Jean-Pierre.Paradis@fsa.ulaval.ca
# Date : March 31, 2010
# Version : 1.01
#
# .LINK
# http://blog.sapien.com/index.php/2009/09/02/powershell-hosting-and-myinvocation/
if (Test-Path variable:\hostinvocation)
{$FullPath=$hostinvocation.MyCommand.Path}
Else {
$FullPath=(get-variable myinvocation -scope script).value.Mycommand.Definition }
if (Test-Path $FullPath) {
return (Split-Path $FullPath)
}
Else {
$FullPath=(Get-Location).path
Write-Warning ("Get-ScriptDirectory: Powershell Host <" + $Host.name + "> may not be compatible with this function, the current directory <" + $FullPath + "> will be used.")
return $FullPath
}
}</pre></div> ParadisJnoreply@blogger.com0tag:blogger.com,1999:blog-2501651780127274931.post-13051323104403773992010-04-07T17:47:00.001-07:002010-04-07T17:50:04.969-07:00How to create a collection of custom objects ?<p>First you have to create a collection object :</p> <p> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:577037b2-afe8-4329-9b13-d6ad7b8a7c0d" class="wlWriterEditableSmartContent"><pre class="brush: powershell;">$psobjectStrongName = (New-Object psobject).psobject.GetType().AssemblyQualifiedName
$Col = New-Object "System.Collections.ObjectModel.Collection``1[[$psobjectStrongName]]"</pre></div>
</p>
<p>Than you have to add an item (object) to the collection.</p>
<h5>Method 1 :</h5>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7bf72ea5-10fe-4a9c-8722-c7ad2cb34790" class="wlWriterEditableSmartContent"><pre class="brush: powershell;">$colItem = New-Object psobject -Property @{ObjRef=1;AddedBy="New-Object"}
$Col.Add($colItem)
$colItem =$null</pre></div>
</p>
<h5>Method 2 :</h5>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:1f65b145-5034-4f43-b7e8-408e6c87a8ac" class="wlWriterEditableSmartContent"><pre class="brush: powershell;">$colItem = New-Object psobject
Add-Member -InputObject $colItem -MemberType noteproperty -Name ObjRef -Value 2
Add-Member -InputObject $colItem -MemberType noteproperty -Name AddedBy -Value "Add-Member"
$Col.Add($colItem)
$colItem =$null</pre></div>
</p>
<h5>Method 3 (my personnal favorite) :</h5>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d472c87b-bc4f-49c8-bb61-b6f5c1ff2bde" class="wlWriterEditableSmartContent"><pre class="brush: powershell;">$Col.add((New-Object psobject -Property @{ObjRef=3;AddedBy="add with New-Object"}))</pre></div>
</p>
<p>Source : <a title="http://www.vistax64.com/powershell/25307-how-put-psobject-into-system-collections-objectmodel-collection.html" href="http://www.vistax64.com/powershell/25307-how-put-psobject-into-system-collections-objectmodel-collection.html">http://www.vistax64.com/powershell/25307-how-put-psobject-into-system-collections-objectmodel-collection.html</a></p> ParadisJnoreply@blogger.com0tag:blogger.com,1999:blog-2501651780127274931.post-56765151438267118972010-04-07T12:15:00.001-07:002010-04-07T17:37:01.724-07:00How to send an email ?<p> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:2fd0e9d1-2df0-4e47-b057-78757c92d06a" class="wlWriterEditableSmartContent"><pre class="brush: powershell;">$message = '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><html><head></head><body lang=FR-CA>This is the body of the message</body></html>"
send-MailMessage -to "toto@domain.com" -Encoding ([System.Text.Encoding]::UTF8) -subject "PowerShell Rocs" -from "bozo@domain.com" -bodyasHTML -body $message -smtpserver "mysmtp.domain.com"</pre></div>
</p>
<p>If you want to send your mail to multiple recipients, use the following syntax for the <span style="font-family: 'Consolas', 'Bitstream Vera Sans Mono', 'Courier New', courier, monospace !important; color: #069 !important; font-weight: bold !important">-to</span>  parameter :</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:189a040e-74ce-48a7-bf4f-41111d6975a9" class="wlWriterEditableSmartContent"><pre class="brush: powershell;">@("bozo@domain.com","toto@domain.com")</pre></div> ParadisJnoreply@blogger.com0