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!