首页 > 开发 > PowerShell > 正文

Windows Powershell 执行文件和脚本

2020-05-30 20:14:22
字体:
来源:转载
供稿:网友

象运行可执行文件一样,Powershell运行文件和脚本,也必须使用绝对路径或者相对路径,或者要运行的文件必须定义在可受信任的环境变量中。

关于脚本
脚本和批处理都属于伪可执行文件,它们只是包含了若干命令行解释器能够解释和执行的命令行代码。

执行批处理文件
批处理是扩展名为”.bat”的文本文件,它可以包含任何cmd控制台能够处理的命令。当批处理文件被打开,Cmd控制台会逐行执行每条命令。那Powershell能够直接执行批处理吗?
将下列命令保存为ping.bat

@echo offecho batch File TestpauseDir %windir%/system

然后执行ping
屏幕会打印ping命令帮助,说明调用的ping cmd 而不是ping.bat。
改为:

PS C:/PS> ./pingbatch File TestPress any key to continue . . . Volume in drive C has no label. Volume Serial Number is 4E9B-D846 Directory of C:Windowssystem2009/06/11 05:21   69,584 avicap.dll2009/06/11 05:21   109,456 avifile.dll2009/07/14 05:41   32,816 COMMDLG.DLL2009/07/14 05:41    2,000 keyboard.drv2009/06/11 05:42    9,936 lzexpand.dll2009/06/11 05:21   73,376 mciavi.drv2009/06/11 05:21   25,264 mciseq.drv2009/06/11 05:21   28,160 mciwave.drv2009/07/14 05:41   68,992 MMSYSTEM.DLL2009/07/14 05:41    1,152 mmtask.tsk2009/07/14 05:41    2,032 mouse.drv2009/06/11 05:21   126,912 msvideo.dll2009/06/11 05:42   82,944 olecli.dll2009/07/14 05:41   24,064 OLESVR.DLL2009/07/14 05:41    5,120 SHELL.DLL2009/07/14 05:41    1,744 sound.drv2009/06/11 05:25    5,532 stdole.tlb2009/07/14 05:41    3,360 system.drv2009/07/14 05:41    4,048 TIMER.DRV2009/06/11 05:42    9,008 ver.dll2009/07/14 05:41    2,176 vga.drv2009/07/14 05:41   12,704 WFWNET.DRV    22 File(s)  700,380 bytes    2 Dir(s) 75,927,420,928 bytes free

这时运行的是批处理。

通过cmd进入cmd控制台输入ping发现执行的不是ping命令,而是直接运行ping.bat ,也就是说可以通过.bat 覆盖cmd命令。这种机制很危险,如果有人侵入电脑,并将系统内部命令篡改成自己批处理,那就太悲剧了。 这种命令与脚本的混淆不会发生在powershell中,因为powershell有更安全的机制。

执行VB脚本文件
将下列命令保存为test.vbs

Set wmi = GetObject("winmgmts:")Set collection = wmi.ExecQuery("select * from Win32_Process")For Each process in collectionWScript.Echo process.getObjectText_Next

执行 ./test.vbs 会遍历当前Win32进程,并把每个进程的详细信息通过窗口显示出来。
怎样让VB脚本的通过控制台输出呢?
Wscript //H:CScript
怎样还原VB脚本通过窗口输出呢?
WScript //H:WScript
在powershell中执行VB脚本

PS C:/PS> cscript.exe .test.vbsMicrosoft (R) Windows Script Host Version 5.8Copyright (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";};            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表