【问题标题】:Set-RunOnce run ps1 file from changing usb drive letterSet-RunOnce 通过更改 USB 驱动器号运行 ps1 文件
【发布时间】:2019-05-28 14:39:00
【问题描述】:

所以我正在使用这个 Powershell 脚本:Set-RunOnce https://www.powershellgallery.com/packages/WindowsImageConverter/1.0/Content/Set-RunOnce.ps1

当我将驱动器号硬编码到 (E:\ServerInstall.ps1) 中时,它就像一个魅力。 但我想确保此脚本可以从 USB 插入的任何驱动器号运行

如何在注册表中获取这个不断变化的驱动器号?

我第一次尝试使用 -ExecutionPolicy Bypass,但也没有太大变化。

我也试过这个:

$getusb = Get-WmiObject Win32_Volume -Filter "DriveType='2'" 。 .\Set-RunOnce.ps1 Set-RunOnce -Command

'%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe ` -ExecutionPolicy Unrestricted -File $getusb.Name\ServerInstall.ps1'

--> $getusb.Name\ServerInstall.ps1 结束在注册表中被硬编码,但它不知道是什么 $getusb.name 是,所以脚本没有启动。

. .\Set-RunOnce.ps1
Set-RunOnce -Command '%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe `
-ExecutionPolicy Unrestricted -File (wmic logicaldisk where drivetype=2)
ServerInstall.ps1'

【问题讨论】:

  • 您是否希望在每次将 USB 设备插入机器时运行某些东西?
  • 您没有向我们展示ServerInstall.ps1 脚本的作用。你应该在那里执行(wmic logicaldisk where drivetype=2) 的东西,而不是把它作为命令行的一部分。请参阅powershellgallery.com/packages/WindowsImageConverter/1.0/… 上的示例
  • 使用"$($getusb.DriveLetter)\ServerInstall.ps1" 而不是$getusb.Name\ServerInstall.ps1。还要考虑$getusb 可能是一个数组,如果插入更多的 USB 磁盘...
  • 为什么用cmd 标记的问题是用于处理Windows 批处理文件的Windows 命令处理器。 cmd.exe 无法执行 PowerShell 命令。 PowerShell.exe 是一个完全不同的脚本解释器。在使用批处理文件运行 PowerShell.exe 双击 USB 驱动器上的批处理文件时,可以将批处理文件的存储位置的驱动器作为参数传递给 PowerShell,%~d0 将执行扩展到驱动器号和冒号.在 cmd 窗口中运行 call /? 寻求帮助。参数 0 始终是批处理文件本身。
  • @Mofi 一旦进入注册表,它就会使用 cmd 代码,而不是 powershell 代码。一旦我运行它,它就会在注册表中进行硬编码,并且它保持“$($getusb.DriveLetter)\ServerInstall.ps1”,它没有得到驱动器号

标签: powershell cmd automation runonce


【解决方案1】:

Set-RunOnce 函数非常易于理解且易于调整。
我建议创建一个你自己的派生函数来做你想做的事情并使用它:

function Set-RunOnceForUSB {
    # get the driveletter from WMI for the first (possibly only) USB disk
    $firstUSB   = @(Get-WmiObject -Class Win32_LogicalDisk | Where-Object {$_.DriveType -eq 2} | Select-Object -ExpandProperty DeviceID)[0]
    # or use:
    # $firstUSB = @(Get-WmiObject Win32_Volume -Filter "DriveType='2'" | Select-Object -ExpandProperty DriveLetter)[0]

    # combine that with the name of your script file to create a complete path
    $scriptPath = Join-Path -Path $firstUSB -ChildPath 'ServerInstall.ps1'

    # create the command string. use double-quotes so the variable $scriptPath gets expanded
    $command = "%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -File $scriptPath"

    # next, add this to the registry same as the original Set-RunOnce function does
    if (-not ((Get-Item -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce).Run )) {
        New-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce' -Name 'Run' -Value $command -PropertyType ExpandString
    }
    else {
        Set-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce' -Name 'Run' -Value $command -PropertyType ExpandString
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多