Windows Powershell execute files and scripts _powershell

Source: Internet
Author: User

Like running an executable file, PowerShell runs files and scripts, must also use absolute or relative paths, or the files to run must be defined in a trusted environment variable.

About scripts
Both scripts and batches belong to pseudo executables, and they only contain command-line code that can be interpreted and executed by a number of command-line interpreters.

Execute batch File
Batch processing is a text file with the extension ". Bat" that can contain any commands that the CMD console can handle. When a batch file is opened, the CMD console executes each command line by row. Can the PowerShell do the batch processing directly?
Save the following command as Ping.bat

@echo off
echo batch File Test
pause
Dir%windir%/system

And then do the ping.
The screen Prints ping command help, stating that the ping Cmd was invoked instead of Ping.bat.
To

PS c:\ps>./ping Batch File Test Press any key to continue ...
 Volume in Drive C has no label.   Volume serial number is 4e9b-d846 Directory of C:windowssystem 2009/06/11 05:21 69,584 avicap.dll 2009/06/11 05:21 109,456 avifile.dll 2009/07/14 05:41 32,816 Commdlg. DLL 2009/07/14 05:41 2,000 keyboard.drv 2009/06/11 05:42 9,936 lzexpand.dll 2009/06/11 05:21 73,376 mciavi.drv 200 9/06/11 05:21 25,264 mciseq.drv 2009/06/11 05:21 28,160 mciwave.drv 2009/07/14 05:41 68,992. DLL 2009/07/14 05:41 1,152 Mmtask.tsk 2009/07/14 05:41 2,032 mouse.drv 2009/06/11 05:21 126,912 msvideo.dll 2009/0 6/11 05:42 82,944 olecli.dll 2009/07/14 05:41. DLL 2009/07/14 05:41 5,120 SHELL. DLL 2009/07/14 05:41 1,744 sound.drv 2009/06/11 05:25 5,532 stdole.tlb 2009/07/14 05:41 3,360 system.drv 2009/07/ 05:41 4,048 TIMER. DRV 2009/06/11 05:42 9,008 ver.dll 2009/07/14 05:41 2,176 vga.drv 2009/07/14 05:41, 12,704 wfwnet. DRV File (s) 700,380 bytes 2 Dir (s) 75,927,420,928 bytes free
 

This is the batch process that is running.

Through cmd into the cmd console input ping found not to execute the ping command, but directly run Ping.bat, that is, you can use. bat override cmd command. This is a dangerous mechanism, and it would be a tragedy if someone hacked into the computer and tampered with the system's internal commands to their own batch. This confusion of commands and scripts does not occur in PowerShell because PowerShell has a more secure mechanism.

Execute VB Script file
Save the following command as Test.vbs

Set WMI = GetObject ("winmgmts:")
Set collection = WMI. ExecQuery ("SELECT * from Win32_Process") for each
Process in collection
WScript.Echo Process.getobjecttext_
Next

Execution. \test.vbs traverses the current WIN32 process and displays the details of each process through a window.
How to let VB Script through the console output it?
Wscript//H:CScript
How to restore the VB Script through the window output?
WScript//H:WScript
Executing VB Script in PowerShell

PS c:\ps> cscript.exe. Test.vbs Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation.

All rights reserved.
  Instance of Win32_Process {Caption = "System Idle Process";
  CreationClassName = "Win32_Process";
  Cscreationclassname = "Win32_ComputerSystem";
  CSName = "test-me-01";
  Description = "System Idle Process";
  Handle = "0";
  Handlecount = 0;
  Kernelmodetime = "484113379271";
  Name = "System Idle Process";
  Oscreationclassname = "Win32_OperatingSystem"; Osname = "Microsoft Windows 7 Enterprise | c:windows|
  Deviceharddisk0partition2 ";
  Otheroperationcount = "0";
  Othertransfercount = "0";
  PageFaults = 0;
  pagefileusage = 0;
  Parentprocessid = 0;
  PeakPageFileUsage = 0;
  PeakVirtualSize = "0";
  peakworkingsetsize = 0;
  Priority = 0;
  Privatepagecount = "0";
  ProcessID = 0;
  quotanonpagedpoolusage = 0;
  quotapagedpoolusage = 0;
  quotapeaknonpagedpoolusage = 0;
  quotapeakpagedpoolusage = 0;
  Readoperationcount = "0"; ReadtrAnsfercount = "0";
  SessionId = 0;
  ThreadCount = 2;
  Usermodetime = "0";
  VirtualSize = "0";
  WindowsVersion = "6.1.7601";
  WorkingSetSize = "24576";
  Writeoperationcount = "0";
Writetransfercount = "0";
 };

Execute PowerShell Script
PowerShell has its own script, with the extension ". PS1"

PS c:\ps> echo "DIR; Get-psprovider;help dir ">test.ps1
PS c:\ps> get-content./test.ps1
dir; Get-psprovider;help dir
PS c:\ps>./test.ps1 when the script is first executed, you may encounter an exception:
File "C:\ps\test.ps1″cannot be loaded Because the
execution of scripts is disabled to this system. Please
click "Get-help about_signing" for more details.
At Line:1 char:10
+. Test.ps1 <<<<

This is the default security setting for PowerShell the Execute script is disabled, and you need to have administrator privileges to enable this feature.

Open:set-executionpolicy remotesigned

Shut down:Set-ExecutionPolicy Restricted

Priority of PowerShell Call entry
Alias: the console first looks for whether the input is an alias, and if so, executes the command that the alias refers to. So we can overwrite any PowerShell command by alias, because the alias has the highest precedence.

function: If an alias is not found, it continues to look for the function, which is like an alias, except that it contains more PowerShell commands. So you can customize the function extension cmdlet to solidify the commonly used parameters.

Command: If the function is not found, the console continues to look for the command, which is the Cmdlet,powershell internal command.

Script: The command was not found to continue looking for the PowerShell script with the extension ". PS1".

File: Script not found, will continue to look for files, if no files are available, the console throws an exception.

The term ' now ' isn't recognized as the name of a cmdlet, function, script file, or operable program. Chec
g of the name, or if a path was included, verify this path is correct and try again.
At Line:1 Char:4
+ now <<<<
+ categoryinfo:objectnotfound: (now:string) [], Commandnotfoundexception
+ fullyqualifiederrorid:commandnotfoundexception

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.