How to write and run scripts in Windows PowerShell - Part 2 - Tue Oct 18, 2016 7:23 am
RUNNING SCRIPTS REMOTELY
To run a script on a remote computer, use the FilePath parameter of the Invoke-Command cmdlet.
Enter the path and file name of the script as the value of the FilePath parameter. The script must reside on the local computer or in a directory that the local computer can access.
The following command runs the ServicesLog.ps1 script on the Server01 remote computer.
invoke-command -computername Server01 -filepath C:\scripts\servicesLog.ps1
PARAMETERS IN SCRIPTS
To define parameters in a script, use a Param statement. The Param statement must be the first statement in a script, except for comments and any #Requires statements.
Script parameters work like function parameters. The parameter values are available to all of the commands in the script. All of the features of function parameters, including the Parameter attribute and its named arguments, are also valid in scripts.
When running the script, script users type the parameters after the script name.
The following example shows a Test-Remote.ps1 script that has a ComputerName parameter. Both of the script functions can access the ComputerName parameter value.
param ($ComputerName = $(throw "ComputerName parameter is required."))
function CanPing {
$error.clear()
$tmp = test-connection $computername -erroraction SilentlyContinue
if (!$?)
{write-host "Ping failed: $ComputerName."; return $false}
else
{write-host "Ping succeeded: $ComputerName"; return $true}
}
function CanRemote {
$s = new-pssession $computername -erroraction SilentlyContinue
if ($s -is [System.Management.Automation.Runspaces.PSSession])
{write-host "Remote test succeeded: $ComputerName."}
else
{write-host "Remote test failed: $ComputerName."}
}
if (CanPing $computername) {CanRemote $computername}
To run this script, type the parameter name after the script name. For example:
C:\PS> .\test-remote.ps1 -computername Server01
Ping succeeded: Server01
Remote test failed: Server01
For more information about the Param statement and the function parameters see about_Functions and about_Functions_Advanced_Parameters. You can find this additional information in you windows folder , usually on your drive C
Part 3 to follow.....
To run a script on a remote computer, use the FilePath parameter of the Invoke-Command cmdlet.
Enter the path and file name of the script as the value of the FilePath parameter. The script must reside on the local computer or in a directory that the local computer can access.
The following command runs the ServicesLog.ps1 script on the Server01 remote computer.
invoke-command -computername Server01 -filepath C:\scripts\servicesLog.ps1
PARAMETERS IN SCRIPTS
To define parameters in a script, use a Param statement. The Param statement must be the first statement in a script, except for comments and any #Requires statements.
Script parameters work like function parameters. The parameter values are available to all of the commands in the script. All of the features of function parameters, including the Parameter attribute and its named arguments, are also valid in scripts.
When running the script, script users type the parameters after the script name.
The following example shows a Test-Remote.ps1 script that has a ComputerName parameter. Both of the script functions can access the ComputerName parameter value.
param ($ComputerName = $(throw "ComputerName parameter is required."))
function CanPing {
$error.clear()
$tmp = test-connection $computername -erroraction SilentlyContinue
if (!$?)
{write-host "Ping failed: $ComputerName."; return $false}
else
{write-host "Ping succeeded: $ComputerName"; return $true}
}
function CanRemote {
$s = new-pssession $computername -erroraction SilentlyContinue
if ($s -is [System.Management.Automation.Runspaces.PSSession])
{write-host "Remote test succeeded: $ComputerName."}
else
{write-host "Remote test failed: $ComputerName."}
}
if (CanPing $computername) {CanRemote $computername}
To run this script, type the parameter name after the script name. For example:
C:\PS> .\test-remote.ps1 -computername Server01
Ping succeeded: Server01
Remote test failed: Server01
For more information about the Param statement and the function parameters see about_Functions and about_Functions_Advanced_Parameters. You can find this additional information in you windows folder , usually on your drive C
Part 3 to follow.....