如何在计算机启动时运行 PowerShell 脚本? [英] How do I run a PowerShell script when the computer starts?

查看:58
本文介绍了如何在计算机启动时运行 PowerShell 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个监控图像文件夹的 PowerShell 脚本.我需要想办法在电脑启动后自动运行这个脚本.

我已经尝试了以下方法,但无法正常工作.

  1. 使用 msconfig 并添加 PowerShell 脚本以启动,但我在该列表中找不到 PowerShell 脚本.

  2. 创建一个快捷方式并将其放到启动文件夹中.没有运气.

    %SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"

    这是我的 PowerShell 脚本:

    $folder = "C:\\Doc\\Files"$dest = "C:\\Doc\\Files\\images"$filter = "*.jpg"$fsw = 新对象 System.IO.FileSystemWatcher $folder, $filter -Property @{包含子目录=$falseNotifyFilter = [System.IO.NotifyFilters]'文件名,LastWrite'}$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {开始睡眠 -s 10Move-Item -Path C:\Doc\Files\*.jpg C:\Doc\Files\images}

  3. 我还尝试使用 taskschd.msc 添加一个基本任务.它仍然无法正常工作.

    这是我的发现,也许这将有助于调试它.

    如果我打开一个 PowerShell 窗口并在那里运行脚本,它就可以工作.但是如果我在命令提示符下运行它,

    powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"

    这是行不通的.我不确定这是权限问题还是其他问题.

    顺便说一句,我安装了 PowerShell 3.0,如果我输入 $host.version,它会在那里显示 3.但是我的powershell.exe好像还是v1.0.

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

解决方案

我终于让我的 PowerShell 脚本在每次启动时自动运行.您将需要创建两个文件:第一个是 Powershell 脚本(例如 script.ps1),第二个是 .cmd 文件,其中包含将在命令提示符下运行的命令(例如 startup.cmd).

第二个文件是计算机启动时需要执行的文件,简单地将 .ps1 复制粘贴到启动文件夹是行不通的,因为这实际上并没有执行脚本 - 它只是打开文件用记事本.您需要执行 .cmd,它本身将使用 PowerShell 执行 .ps1.好了,废话不多说,进入步骤:

  1. 创建您的 .ps1 脚本并将其放在一个文件夹中.为了简单起见,我把它放在我的桌面上.路径看起来像这样:

<块引用>

C:\Users\<user_name>\Desktop\script.ps1

  1. 创建一个.cmd文件并将其放入

<块引用>

C:\Users\<user_name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\startup.cmd

这样做会在每次启动时执行 cmd 文件.这里是如何创建的链接.cmd 文件(如果您需要帮助).

  1. 使用文本编辑器打开 .cmd 文件并输入以下几行:

PowerShell -Command "Set-ExecutionPolicy Unrestricted" >>"%TEMP%\StartupLog.txt" 2>&1PowerShell C:\Users\\Desktop\script.ps1 >>"%TEMP%\StartupLog.txt" 2>&1

这将做两件事:

  1. 将 PowerShell 的执行策略设置为不受限制.这是运行脚本所必需的,否则 PowerShell 不会执行此操作.
  2. 使用 PowerShell 执行在指定路径中找到的 .ps1 脚本.

此代码专门用于 PowerShell v1.0.如果您运行的是 PowerShell v2.0,它可能会有所不同.在任何情况下,请查看此来源以获取 .cmd 代码.

  1. 保存 .cmd 文件

既然您的 .ps1 和 .cmd 文件都位于各自的路径中,并且每个文件都有脚本,那么您就大功告成了.

I have a PowerShell script that monitors an image folder. I need to find a way to automatically run this script after the computer starts.

I already tried the following methods, but I couldn't get it working.

  1. Use msconfig and add the PowerShell script to startup, but I cannot find the PowerShell script on that list.

  2. Create a shortcut and drop it to startup folder. No luck.

    %SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
    

    or

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
    

    Here's my PowerShell script:

    $folder = "C:\\Doc\\Files"
    $dest = "C:\\Doc\\Files\\images"
    $filter = "*.jpg"
    
    $fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
        IncludeSubDirectories=$false
        NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
    }
    
    $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    
        Start-Sleep -s 10
        Move-Item -Path C:\Doc\Files\*.jpg C:\Doc\Files\images
    }
    

  3. I also tried to add a basic task using taskschd.msc. It is still not working.

    Here's what I found, and maybe that will help to debug it.

    If I open up a PowerShell window and run the script there, it works. But if I run it in a command prompt,

    powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
    

    It will not work. I am not sure it's a permission problem or something else.

    BTW, I have PowerShell 3.0 installed, and if I type $host.version, it will show 3 there. But my powershell.exe seems like it is still v1.0.

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
    

解决方案

I finally got my PowerShell script to run automatically on every startup. You will need to create two files: the first is the Powershell script (e.g. script.ps1) and the second is a .cmd file that will contain commands that will run on the command prompt (e.g. startup.cmd).

The second file is what needs to be executed when the computer starts up, and simply copy-pasting the .ps1 to the startup folder won't work, because that doesn't actually execute the script - it only opens the file with Notepad. You need to execute the .cmd which itself will execute the .ps1 using PowerShell. Ok, enough babbling and on to the steps:

  1. Create your .ps1 script and place it in a folder. I put it on my desktop for simplicity. The path would look something like this:

C:\Users\<user_name>\Desktop\script.ps1

  1. Create a .cmd file and place it in

C:\Users\<user_name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\startup.cmd

Doing this will execute the cmd file every time on startup. Here is a link of how to create a .cmd file if you need help.

  1. Open the .cmd file with a text editor and enter the following lines:

PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
PowerShell C:\Users\<user_name>\Desktop\script.ps1 >> "%TEMP%\StartupLog.txt" 2>&1

This will do two things:

  1. Set the Execution Policy of your PowerShell to Unrestricted. This is needed to run scripts or else PowerShell will not do it.
  2. Use PowerShell to execute the .ps1 script found in the path specified.

This code is specifically for PowerShell v1.0. If you're running PowerShell v2.0 it might be a little different. In any case, check this source for the .cmd code.

  1. Save the .cmd file

Now that you have your .ps1 and .cmd files in their respective paths and with the script for each, you are all set.

这篇关于如何在计算机启动时运行 PowerShell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆