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

1

2 Answers 2

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%' "
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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()}

1 Comment

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()}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.