HackGh Community Forum
Topics tagged under requires on HackGh Community Forum Empty


Free counters!
Staff Online
Staff Online
Members2390
Most Online179
Newest Member
https://hackgh.forumotion.com/u2487


You are not connected. Please login or register

HackGh Community Forum » Advanced Search

Search found 2 matches for Requires

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.....
LONG DESCRIPTION

A script is a plain text file that contains one or more Windows PowerShell commands. Windows PowerShell scripts have a .ps1 file name extension.

Writing a script saves a command for later use and makes it easy to share with others. Most importantly, it lets you run the commands simply by typing the script path and the file name. Scripts can be as simple as a single command in a file or as extensive as a complex program.

Scripts have additional features, such as the #Requires special comment, the use of parameters, support for data sections, and digital signing for security. You can also write Help topics for scripts and for any functions in the script.

HOW TO WRITE A SCRIPT

A script can contain any valid Windows PowerShell commands, including single commands, commands that use the pipeline, functions, and control structures such as If statements and For loops.

To write a script, start a text editor (such as Notepad) or a script editor (such as the Windows PowerShell Integrated Scripting Environment [ISE]). Type the commands and save them in a file with a valid file name and the .ps1 file name extension.

The following example is a simple script that gets the services that are running on the current system and saves them to a log file. The log file name is created from the current date.

$date = (get-date).dayofyear
get-service | out-file "$date.log"

To create this script, open a text editor or a script editor, type these commands, and then save them in a file named ServiceLog.ps1.


HOW TO RUN A SCRIPT

Before you can run a script, you need to change the default Windows PowerShell execution policy. The default execution policy, "Restricted" prevents all scripts from running, including scripts that you write on the local computer. For more information, see about_Execution_Policies.

To run a script, type the full name and the full path to the script file.

For example, to run the ServicesLog script in the C:\Scripts directory, type:

c:\scripts\ServicesLog.ps1

To run a script in the current directory, type the path to the current directory, or use a dot to represent the current directory, followed by a path backslash (.\).

For example, to run the ServicesLog.ps1 script in the local directory, type:

.\ServicesLog.ps1

As a security feature, Windows PowerShell does not run scripts when you double-click the script icon in Windows Explorer or when you type the script name without a full path, even when the script is in the current directory. For more information about running commands and scripts in Windows PowerShell, see about_Command_Precedence.

Part 2 to follow.......

Search found 2 matches for Requires

Back to top