【问题标题】:I have problem with putting quotes inside quotes我在引号内加上引号有问题
【发布时间】:2020-11-06 10:13:02
【问题描述】:

我正在尝试在 PowerShell 中创建一行命令来下载音乐文件并每 1 小时执行一次
Start-Process -WindowStyle Hidden PowerShell "Register-ScheduledTask Regedit -Action (New-ScheduledTaskAction -Execute PowerShell -Argument "Start-process -WindowStyle Hidden PowerShell '(New-Object System.Media.SoundPlayer C:\Windows\Jjgw.wav).PlaySync()'") -Trigger (New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Hours 1)) -f;(New-Object System.Net.WebClient).DownloadFile('https://github.com/User/Repository/raw/master/Music.wav','C:\Windows\Music.wav')"
但我无法设置单引号/双引号以转到最后一个引号,而不是最接近的引号。
我正在尝试实现这样的目标:
PowerShell ""command1";"command2""
只执行这两个命令。

【问题讨论】:

    标签: powershell double-quotes quoting single-quotes


    【解决方案1】:

    这是经常被问到的。最好的方法是使用EncodedCommand

    $command = {
        $action = New-ScheduledTaskAction -Execute PowerShell -Argument "Start-process -WindowStyle Hidden PowerShell '(New-Object System.Media.SoundPlayer C:\Windows\Jjgw.wav).PlaySync()'"
        $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Hours 1)
        Register-ScheduledTask Regedit -Action $action -Trigger $trigger -Force
        (New-Object System.Net.WebClient).DownloadFile('https://github.com/User/Repository/raw/master/Music.wav','C:\Windows\Music.wav')
    }
    $encodedCommand = [convert]::ToBase64String([System.Text.encoding]::Unicode.GetBytes($command.ToString())) 
    Start-Process -WindowStyle Hidden PowerShell -ArgumentList "-EncodedCommand", $encodedCommand
    

    【讨论】:

    • 我通过 Start-Process 执行此操作,因为我在这些命令之后执行了另外 2 个命令: Start-Process -WindowStyle Hidden PowerShell "Commands";(Get-PSReadlineOption).HistorySavePath | rm;退出
    • 我不能用 $variables 来做,因为我不能在 PowerShell 中从 Start-Process 开始创建它们,而且我对编码命令有问题,因为我仍然需要在双引号内加上双引号
    • 是否有某种第三引号之类的东西
    • ?只有一个 Start-Process,它的功能是创建一个新的、隐藏的 PowerShell 实例,它将在后台执行所有代码,而主 PowerShell 将关闭。
    • @Gyanbu 我明白了。好吧,我的回答应该完全符合您的要求。你试过了吗?
    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    相关资源
    最近更新 更多