Pass a file path as a variable?

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

  • Author
    Posts
  • #23820
    Profile photo of Rocky Cabral
    Rocky Cabral
    Participant

    I have several restart scripts that specify a particular service and text file with specific host names. I like to have a single script that's more generic.

    Unfortunately, it doesn't seem I can pass a file path as a variable like so:

    $boxes = read-host -Prompt "Enter source path"

    In this example, I would enter "D:\TestBoxes.txt" as the source path. And then the script would or should restart the services for each of host names specified in "TestBoxes.txt". But, it doesn't work. What does work is if I do: "$boxes = gc D:\TestBoxes.txt".

    Again, I like to get away from specifying a particular path and make it more generic.

    Here is my restart services script:
    #################################################################
    #Variables
    write-host -ForegroundColor Cyan "Get restart services!"
    $Svc = read-host "Enter service name to restart"
    $boxes = gc D:\TestBoxes.txt

    #The following four lines only need to be declared once in your script.
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
    $cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel","Description."
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $cancel)

    #Declare Workflow
    workflow RestartServices{
    param([string[]]$Servs,[string]$Svs)
    foreach -parallel ($computer in $Servs) {

    Restart-Service -Verbose -InputObject (get-Service -ComputerName $computer -Name $Svs)

    }
    }

    #Use the following each time your want to prompt the use
    $title = "Restarting services"
    $message = "Proceed with restart?"
    $result = $host.ui.PromptForChoice($title, $message, $options, 0)
    measure-command{
    switch ($result) {
    0{
    write-host -foregroundcolor cyan "Restarting $Svc on all Servers "
    RestartServices -Servs $boxes -Svs $Svc
    write-host -foregroundcolor cyan "Service restarted!"
    }

    1{
    Write-Host "No"
    }

    2{
    Write-Host "Cancel"
    }
    }
    }
    ##############################################################################

    Thanks for the help on this!

  • #23826
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Your trouble is that you're working a bit "against" how the shell likes to work. Write-Host is one clue; Read-Host is another :).

    At the top of your script:

    [CmdletBinding()]
    Param(
      [Parameter(Mandatory=$True)][string]$boxes
    )
    

    Then simply run your script. PowerShell will prompt you for "boxes," and you'll type your path. Or...

    ./MyScript.ps1 -boxes d:\whatever
    

    All helpfully covered in [i]Learn PowerShell Toolmaking in a Month of Lunches[/i] if you're in a reading mood.

    I'd be happier if you used Write-Verbose instead of Write-Host, too. You could then enable the screen output by adding -Verbose when you run the script, and you become less tied to the console host for executing your script.

You must be logged in to reply to this topic.