3

Good Day All

I want to set up a common script where I can pass the server name from a .bat script when calling the .ps1

In my bat script I have this statement:

set setver_name=our2008server
powershell -ExecutionPolicy RemoteSigned 
    -NonInteractive 
    -NoProfile 
    -command 
        "& '\\serverd1\d$\Program Files\%run_dir%\Dcls\PS\Stop_Bkgnd_%run_env%_01.ps1' " 
    -server_name %server_name%

in my ps1 script I have:

gwmi win32_service -Computername $server_name -filter "name = 'BackgroundQueue'"  | 
    % {$_.StopService()}

If I replace the $server_name with the actual server name it works fine. Just cannot get the variable from the .bat file to be recognized in the .ps1.

Any help would be greatly appreciated.

BobZ

RB.
  • 36,301
  • 12
  • 91
  • 131
BobZ
  • 31
  • 1
  • 2
  • [Duplicate question](http://stackoverflow.com/questions/5592531/how-to-pass-an-argument-to-this-powershell-script/5596698#5596698). – Emiliano Poggi May 09 '11 at 20:40

2 Answers2

4

Update your script to use param:

param($server_name)
gwmi win32_service -Computername $server_name -filter "name = 'BackgroundQueue'"  | 
% {$_.StopService()}

And while calling, move the -server_name %server_name% to within the command.

set setver_name=our2008server
powershell -ExecutionPolicy RemoteSigned 
    -NonInteractive 
    -NoProfile 
    -command 
        "& '\\serverd1\d$\Program Files\%run_dir%\Dcls\PS\Stop_Bkgnd_%run_env%_01.ps1 -server_name %server_name%' "
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Got an error, missing the terminator. I'll look into using a param later. Since I did not mention that I'll be passing multiples, I'm going to use the arg as explained. – BobZ May 10 '11 at 13:32
  • The error might be the misplaced quote. Try: "& '\\serverd1\d$\Program Files\%run_dir%\Dcls\PS\Stop_Bkgnd_%run_env%_01.ps1' -server_name %server_name%" instead (see the single-quote moved back to the end of the filename) manojlds is right about the arguments within the double-quotes. – Jay Jan 09 '12 at 13:48
1

The easiest way is to use the $args variable. This is an array containing all the arguments passed to the PowerShell script.

Sample usage:

Write-Host "Num Args:" $args.Length;
foreach ($arg in $args)
{
  Write-Host "Arg: $arg";
}

$args[0]
$args[1]

So, in your example, I would change the command parameter to read

// Note the change at the end of this string
-command "& '\\path\to\my\powershell_script.ps1' %setver_name%"

and change the PS1 file to be

// Note I have used $args[0].
gwmi win32_service -Computername $args[0] -filter "name = 'BackgroundQueue'"  | 
    % {$_.StopService()}
RB.
  • 36,301
  • 12
  • 91
  • 131
  • Thank you very much, this works well In my bat script I have this statement: set setver_name=our2008server powershell -ExecutionPolicy RemoteSigned -NonInteractive -NoProfile -command "& '\\serverd1\d$\Program Files\%run_dir%\Dcls\PS\Stop_Bkgnd_%run_env%_01.ps1' %server_name%" $Server_name=$args[0] gwmi win32_service -Computername $server_name -filter "name = 'BackgroundQueue'" | % {$_.StopService()} – BobZ May 10 '11 at 13:34