Powershell v2.0 passing parameter of computername variable from batch cmd file

Good afternoon everyone

I want to set up a general script where I can pass the server name from a .bat script when calling .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 $ server_name with the actual server name, it works fine. I just can’t get the variable from the .bat file for recognition in .ps1.

Any help would be greatly appreciated.

Bobz

+3
source share
2 answers

Update your script to use the parameter:

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

And when called, move -server_name %server_name%to the team.

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%' "
+4
source

- $args. , , PowerShell script.

:

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

$args[0]
$args[1]

, command,

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

PS1

// Note I have used $args[0].
gwmi win32_service -Computername $args[0] -filter "name = 'BackgroundQueue'"  | 
    % {$_.StopService()}
+1

All Articles