I’m writing a PowerShell script for a particular client. I tested the script on several of our desktop machines, and the tried the script on the client’s system. I ran into the following issues:
Issue #1
As a good little Do Bee, I defined a bunch of help text, so when you run Get-Help on my script, it would give you detailed help. I used the following syntax:
<#
Get-Help
.SYNOPSIS
C> FileWatcher.ps1 -FilePath <FileName> -SenderEmail Bob@Foo.com ^
-ToEmail Joe@Bar.com -SmtpServer smtp.foo.com
.DESCRIPTION
Blah, blah, blah...
#>
On my machine, it works, and this is recognized as comment. However, on the client’s machine, this produced an error as soon as it saw the C> which it thought was a redirect. Getting rid of the #> and <# and putting # in front of each line got rid of this problem, and brought us Issue #2.
Issue #2
I defined a bunch of parameters like this:
Param (
[ValidateScript({Test-Path $_ -PathType 'Leaf'})]
[Parameter(
Position=0,
HelpMessage="File you want to watch")]
$FilePath = "\\rke032\QuickCon\wincommlog.000",
[String]
[Parameter(
blah, blah, blah
PowerShell coughed on [ValidateScript({Test-Path $_ -PathType 'Leaf'})] saying it wasn’t a valid type.
As I said, we tested this on a wide variety of Windows machines. I have a Windows XP machine. It’s PowerShell version # 6.0.6002.1811. On another machine that’s running Windows 7, the PowerShell version is 6.1.7600.
On the client’s machine (a Windows 2008 Server) which is giving us these errors, the version is 6.0.6001.18000.
We ran the Powershell scripts by bringing up a PowerShell window, and then typing in the script’s name. The ExecutionPolicy is set to Unrestricted. The script has a *.ps1 suffix on the end. I can’t believe there’s that big a difference between revision 6.0.6002 and 6.0.6001 to have a problem with unrecognized syntax. Is there something else going on?
Compare the output of
$PSVersionTableand not the build version. In particular thePSVersionproperty is interesting. I guess you have PS1 on one machine and PS2 on another. The extension is.ps1regardless of the PowerShell version.This guess is reinforced by noticing that block comments don’t work and neither do parameter attributes.