Computer startup script with parameters

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 1 year, 2 months ago.

  • Author
    Posts
  • #31464
    Profile photo of Brian Sprogø
    Brian Sprogø
    Participant

    Having a script that is set to run as a computer startup script from a GPO.

    The script is kept inside the GPO.

    The script accepts a single parameter that contains a path to a CSV file in this form '\\servername\sharename\file.csv'

    When I configure the GPO computer startup script I configure the parameter as a parameter outside the script. The idear being that the final user do not have to edit the script itself.

    Inside the script I have a normal Param section taking the parameter.

    Later in the script I import the CSV file.

    A simplified version of the script is like this:

    Param(
        [Parameter(Mandatory=$true,Position=0)]
        [String]$File
    )
    
    $List = Import-Csv $File
    

    The trouble I face is that the path provided is appended to the path to the GPO path.

    I would expect the parameter to contain '\\servername\sharename\file.csv'

    But from the powershell eventlog I can see that it contains '\\domain.local\SysVol\domain.local\Policies\{96F2E885-66E4-4B9B-8CE7-C765CBE612EA}\Machine\Scripts\Startup\'\servername\sharename\file.csv'

    I spotted that the first \ is removed from the path – checked that it is not as simple as me typing it wrong 🙂

    I tried defining the paramenter with a default value in the Param section – same thing

    Tried defining the parameter inside the script but outside the Param section – that works as expected.

    Can anyone explain this behaviour to me ?

  • #31935
    Profile photo of Don Jones
    Don Jones
    Keymaster

    The value you're getting it what is being passed to $File. I'm not sure how you're passing in the value, but whatever you're doing is including all of that. This isn't something wrong with your parameter definition.

    I'm not sure what you mean by "defining the parameter inside the script but outside the Param section."

  • #31973
    Profile photo of Brian Sprogø
    Brian Sprogø
    Participant

    Thank you Don for following up on unanswered questions 🙂

    I guess that if I paste in the full error message from the eventlog it will be a little clearer what's going on...

    ——-

    Error Message = Could not find a part of the path '\\mailtilmig.local\SysVol\mailtilmig.local\Policies\{96F2E885-66E4-4B9B-8CE7-C765CBE612EA}\Machine\Scripts\Startup\'\dc01.mailtilmig.local\files$\driverlist.csv".
    Fully Qualified Error ID = FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand

    Context:
    Severity = Warning
    Host Name = ConsoleHost
    Host Version = 4.0
    Host ID = fe6ea948-a928-4b72-ae52-39ea2b44a1df
    Host Application = -ExecutionPolicy ByPass -File Import-PvsPrinterDrivers.ps1 -PrinterFile '\\dc01.mailtilmig.local\files$\driverlist.csv'
    Engine Version = 4.0
    Runspace ID = 14ce40eb-df92-4f16-95a0-da65d17db99a
    Pipeline ID = 1
    Command Name = Import-Csv
    Command Type = Cmdlet
    Script Name = \\mailtilmig.local\SysVol\mailtilmig.local\Policies\{96F2E885-66E4-4B9B-8CE7-C765CBE612EA}\Machine\Scripts\Startup\Import-PvsPrinterDrivers.ps1
    Command Path =
    Sequence Number = 15
    User = MAILTILMIG\SYSTEM
    Shell ID = Microsoft.PowerShell

    User Data:

    ——-

    I tried piping the $PrinterFile variable out to a file to see what was in it.

    It contains '\\dc01.mailtilmig.local\files$\driverlist.csv' as I would expect.

    Somehow Import-CSV does not use the string as it is when it is defined in the param block.

    What I meant about outside the param block is that if I leave out the param block and just manually define the variable – then the Import-CSV is using the string as it is and it works without errors.

    I tried using push-location and can see that affects the outcome as the path is no longer to the GPO in SysVol but to the pushed location – but still in a funny way.

    Here I pushed location to C:\temp\

    ——-
    Error Message = Could not find a part of the path 'C:\temp\'\dc01\files$\driverlist.csv".
    Fully Qualified Error ID = FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
    ——-

    I think I'm very close to file this issue on MS Connect as I cannot see this as anything other than a bug.

    Please correct me if I am wrong 🙂

  • #32007
    Profile photo of Don Jones
    Don Jones
    Keymaster

    No, I think I understood what's happening, and I don't think you're looking at a bug.

    In your original code, you're using $file to contain the filename. How is $file being populated? Where is it getting its value from? Obviously, if you define $file with a value in the script it works; when you define $file as a parameter, how are you actually passing a value to that parameter?

    This:

    ——-
    Error Message = Could not find a part of the path 'C:\temp\'\dc01\files$\driverlist.csv".
    Fully Qualified Error ID = FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
    ——-

    I would fully expect not to work. The path is obviously malformed. What I'm not seeing is HOW the path got that way.

You must be logged in to reply to this topic.