locked
Powershell command line parameters not working on some systems RRS feed

  • Question

  • I have a powershell script which works on the system I am using but not on other systems. I cannot see why this is. The command line options are:

    PS C:\Windows\System32\WindowsPowerShell\v1.0> get-help D:\PS\MyScript.ps1
    MyScript.ps1 [-TestMode] [<CommonParameters>]
    MyScript.ps1 [-Full] [-Diff] [-Inc] [-Shutdown] [-Suspend] [-Hibernate] [-Comment <string>] [-DefaultComment <string>] [-FileName <string>] [-Delay <string>]
    [-RunOncePerDay] [-Pause] [-TestMode] [<CommonParameters>]
    MyScript.ps1 [-Help] [-TestMode] [<CommonParameters>]
    MyScript.ps1 [-Modify] [-TestMode] [<CommonParameters>]
    MyScript.ps1 [-ResetXML] [-TestMode] [<CommonParameters>]
    MyScript.ps1 [-EventLog <string>] [-Format] [-TestMode] [<CommonParameters>]
    MyScript.ps1 [-DriveList] [-TestMode] [<CommonParameters>]
    MyScript.ps1 [-DriveAdd <string>] [-TestMode] [<CommonParameters>]
    MyScript.ps1 [-DriveRemove <string>] [-TestMode] [<CommonParameters>]
    MyScript.ps1 [-DriveChange <string>] [-TestMode] [<CommonParameters>]

    These are the parameter definitions:

    [CmdletBinding(DefaultParameterSetName="Default",PositionalBinding=$False)]
    Param(
    #[parameter(Mandatory=$False)]
    #    [switch]$S,
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [switch]$Full,
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [switch]$Diff,
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [switch]$Inc,
    [parameter(Mandatory=$False,ParameterSetName="Help")]
        [switch]$Help,
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [switch]$Shutdown,
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [switch]$Suspend,
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [switch]$Hibernate,
    [parameter(Mandatory=$False,ParameterSetName="Modify")]
        [switch]$Modify,
    [parameter(Mandatory=$False,ParameterSetName="Reset")]
        [switch]$ResetXML,
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [string]$Comment="",
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [string]$DefaultComment="",
    [parameter(Mandatory=$False,ParameterSetName="Event",HelpMessage="Date can be d/m/yy, d/m/yyyy, m/d/yy, m/d/yyyy")]
        [ValidatePattern("(([nN][eE][wW][eE][sS][tT]:\d*)|([dD][aA][tT][eE]:)((\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}\b)|(\b(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}\b)|((19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))))")]
        [string]$EventLog="",
    [parameter(Mandatory=$False,ParameterSetName="Event")]
        [switch]$Format=$False,
    [parameter(Mandatory=$False,ParameterSetName="backup",HelpMessage="The filename cannot contain the characters `"<>:;/\?*`"")]
        [ValidateLength(1,40)]
        [ValidatePattern("^([^<>:;`"\/\\?\*])*$")]
        [string]$FileName,
    [parameter(Mandatory=$False,ParameterSetName="backup",HelpMessage="Use the format of: [m:s] where [m] is minutes and [s] is seconds")]
        [ValidatePattern("([0]:[1-9][\d]*$)|([1-9]:[0-9]$)|([1-9][0-9]*:[0-5][0-9]$)")]
        [string]$Delay,
    [parameter(Mandatory=$False,ParameterSetName="Dlist")]
        [switch]$DriveList=$False,
    [parameter(Mandatory=$False,ParameterSetName="Dadd",HelpMessage="Give a drive path in the form of Z:\")]
        [ValidatePattern("^([A-Z]:[\\])$")]
        [ValidateScript({Test-Path -path ($_) -PathType Container -IsValid})]
        [string]$DriveAdd="",
    [parameter(Mandatory=$False,ParameterSetName="Dremove",HelpMessage="You just need to give a single letter e.g. Z.")]
        [ValidateLength(1,1)][ValidatePattern("([A-Z])")]
        [string]$DriveRemove="",
    [parameter(Mandatory=$False,ParameterSetName="Dchange",HelpMessage="You just need to give a single letter e.g. Z.")]
        [ValidateLength(1,1)][ValidatePattern("([A-Z])")]
        [string]$DriveChange="",
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [switch]$RunOncePerDay,
    [parameter(Mandatory=$False,ParameterSetName="backup")]
        [switch]$Pause,
    [parameter(Mandatory=$False)]
        [switch]$TestMode=$False
    )

    Even the basic option -help fails let alone anything more complicated. Can anyone explain this please I am at a loss as to why.

    The script is not blocked and the user is running with current user = remotesigned. The script runs (as it can) without any parameters and does what it is supposed to.

    Thanks for any help/suggestions.

    Image


    Regards Ray

    Friday, March 25, 2016 2:45 PM

Answers

  • To invoke a script that's enclosed in quotes, you must use the & (call or invocation) operator, as in:


    PS C:\> & "C:\directory name\my script name.ps1"

    If you just write the script name followed by parameters, PowerShell will simply output the string.

    See


    PS C:\> help about_Operators

    for more information.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by ray.g Friday, March 25, 2016 4:50 PM
    Friday, March 25, 2016 4:40 PM

All replies

  • Please do not post pictures.  They are completely  unreadable.

    Copy and paste the exact error message from the console.


    \_(ツ)_/

    Friday, March 25, 2016 3:03 PM
  • To invoke a script that's enclosed in quotes, you must use the & (call or invocation) operator, as in:


    PS C:\> & "C:\directory name\my script name.ps1"

    If you just write the script name followed by parameters, PowerShell will simply output the string.

    See


    PS C:\> help about_Operators

    for more information.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by ray.g Friday, March 25, 2016 4:50 PM
    Friday, March 25, 2016 4:40 PM
  • Thanks Bill - that was indeed the solution. Not one I would have thought of. Still learning the intricasies of powershell

    Regards Ray

    Friday, March 25, 2016 4:51 PM