powershell 设置-使用变量的内容

r1zhe5dt  于 2023-10-18  发布在  Shell
关注(0)|答案(3)|浏览(122)

我正在尝试使用PowerShell脚本生成PowerShell脚本。如何在新生成的脚本内容中获取变量的值?

foreach ($server in $testServers) {
    New-Item -ItemType File -Name "$($server)_wu.ps1" -Path "D:\tools\windows updates\uscripts"

    Set-Content -Path "D:\tools\windows updates\uscripts\$($server)_wu.ps1" -Value {
        $computer = $server

        Invoke-Command -ComputerName $computer -ScriptBlock {
            Set-ExecutionPolicy Bypass
            Find-Module PSWindowsUpdate | Install-Module -Force | Import-Module -Force
        }

        $updatesCount = (Get-WindowsUpdate -ComputerName $computer).Count

        while ($updatesCount -gt "0") {
            psexec \\$computer -s "powershell.exe "Install-WindowsUpdate -Confirm:`$false -IgnoreReboot""
            Restart-Computer -ComputerName $computer -Force -Wait
            $updatesCount = (Get-WindowsUpdate -ComputerName $computer).Count
        }
    }
}

结果如下:

$computer = $server

Invoke-Command -ComputerName $computer -ScriptBlock {
    Set-ExecutionPolicy Bypass
    Find-Module PSWindowsUpdate | Install-Module -Force | Import-Module -Force
}

$updatesCount = (Get-WindowsUpdate -ComputerName $computer).Count

while ($updatesCount -gt "0") {
    psexec \\$computer -s "powershell.exe "Install-WindowsUpdate -Confirm:`$false -IgnoreReboot""
    Restart-Computer -ComputerName $computer -Force -Wait
    $updatesCount = (Get-WindowsUpdate -ComputerName $computer).Count
}

但是,在新脚本中,我希望将$server替换为生成脚本的服务器的名称。

bz4sfanl

bz4sfanl1#

执行您所要求的操作的规范方法可能是使用here-string并通过format operator-f)插入特定值:

foreach ($server in $testServers) {
    @'
$computer = '{0}'

Invoke-Command -ComputerName $computer -ScriptBlock {
    Set-ExecutionPolicy Bypass
    Find-Module PSWindowsUpdate | Install-Module -Force | Import-Module -Force
}

$updatesCount = (Get-WindowsUpdate -ComputerName $computer).Count

while ($updatesCount -gt "0") {
    psexec \\$computer -s 'powershell.exe "Install-WindowsUpdate -Confirm:$false -IgnoreReboot"'
    Restart-Computer -ComputerName $computer -Force -Wait
    $updatesCount = (Get-WindowsUpdate -ComputerName $computer).Count
}
'@ -f $server | Set-Content -Path "D:\tools\windows updates\uscripts\${server}_wu.ps1"
}

然而,我认为编写一个参数化脚本并使用相应的计算机名称调用它可能比为每台计算机创建一个脚本更合适:

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$true)]
    [string]$Computer
)

Invoke-Command -ComputerName $Computer -ScriptBlock {
    Set-ExecutionPolicy Bypass
    Find-Module PSWindowsUpdate | Install-Module -Force | Import-Module -Force
}

$updatesCount = (Get-WindowsUpdate -ComputerName $Computer).Count

while ($updatesCount -gt "0") {
    psexec \\$Computer -s 'powershell.exe "Install-WindowsUpdate -Confirm:$false -IgnoreReboot"'
    Restart-Computer -ComputerName $Computer -Force -Wait
    $updatesCount = (Get-WindowsUpdate -ComputerName $Computer).Count
}

调用:

PS> **script.ps1 -Computer FOO**
n7taea2i

n7taea2i2#

好吧-所以我不知道如何操纵我的字符串,并最终解决它像这样.任何更短、更简单的答案都是绝对受欢迎的(请注意,脚本的其余部分也发生了一些变化)

foreach ($server in $testServers) {

    New-Item -ItemType File -Name "$($server)_wu.ps1" -Path "\\VILV12ICTSCRIPT\d$\windowsupdates\uscripts"

    $value = '

    $computer = $PSCommandPath
    $computer = $computer.Split("`\")[3]
    $computer = $computer.Split("_")[0]

    Invoke-Command -ComputerName $computer -ScriptBlock {

        Set-ExecutionPolicy Bypass
        Find-Module PSWindowsUpdate | Install-Module -Force | Import-Module -Force

    }

    $updatesCount = (PsExec.exe \\$computer -s "powershell.exe "Get-WindowsUpdate"").Count

    while ($updatesCount -gt "0") {

        psexec \\$computer -s "powershell.exe "Install-WindowsUpdate -Confirm:`$false -IgnoreReboot""
        Restart-Computer -ComputerName $computer -Force -Wait
        $updatesCount = (PsExec.exe \\$computer -s "powershell.exe "Get-WindowsUpdate"").Count

    }

    Write-Host "No more updates available for $($computer)" -BackgroundColor DarkGreen

    '

    Set-Content -Path "\\VILV12ICTSCRIPT\d$\windowsupdates\uscripts\$($server)_wu.ps1" -Value $value

}

要生成的脚本和生成的脚本都像charms(^^,)一样工作
欢呼

i7uq4tfw

i7uq4tfw3#

我只是用了一个刻度线来避开美元符号。当它写入文件时,它不带刻度线。

相关问题