我的 PS 脚本中的一小段代码返回错误

问题描述 投票:0回答:1

下面的代码是我正在编写的 PS 脚本的一部分。除了我复制的一小部分之外,一切正常,因为脚本功能对我来说是新的。下面的部分嵌入在菜单中。

行“$whatIfResult = Invoke-Command -ScriptBlock {& $args[0] -WhatIf} -ArgumentList $command ”,特别是“{& $args[0] -WhatIf}”返回错误:

“*管道元素中‘&’之后的表达式生成了无效的对象。它必须生成命令名称、脚本块或 CommandInfo 对象。 行:25 字符:53

  • ... $whatIfResult = Invoke-Command -ScriptBlock { & $args[0] -WhatIf ...
    • 类别信息:无效操作:(:) [],运行时异常
    • FullyQualifiedErrorId:BadExpression*”

完整部分如下所示。

     {
        $requiredColumns = "Zone","Computer","HostName","Name","TimeToLive"

        $csvPath = Read-Host "Enter full path to CSV file"

        $csv = Import-Csv $csvPath
            $missingColumns = $requiredColumns | Where-Object {$_ -notin $csv[0].PSObject.Properties.Name}

            if($missingColumns.Count -gt 0){
                Write-Output "The following required columns are missing from csv: $missingColumns"
            } else {
                Write-Output "All required columns are present in the csv file."
            }

        foreach ($record in $csv) {
            $zoneName = $record.Zone
            $computer = $record.Computer
            $NewHostName = $record.HostName
            $Name = $record.Name
            $ttl = $record.TimeToLive

        # Add the new PTR record with whatif and confirm changes
        $command = Add-DnsServerResourceRecordPtr -ComputerName $computer -ZoneName $zoneName -AllowUpdateAny -TimeToLive $ttl -AgeRecord -Name $Name -PtrDomainName $NewHostName

        # Run the command with -WhatIf
        $whatIfResult = Invoke-Command -ScriptBlock {& $args[0] -WhatIf} -ArgumentList $command

        # Prompt for confirmation
        if ($whatIfResult) {
            $confirmation = Read-Host -Prompt "Do you want to apply these changes? (y/n)"
                if ($confirmation -eq "y") {
                    # Execute the command without -WhatIf
                    Invoke-Expression $command
                    Write-Host "Changes applied."
                } else {
                    Write-Host "Changes not applied."
                }
            }
        }
        Write-Host "Option 1 selected"
    }

任何帮助将不胜感激。

powershell dns
1个回答
0
投票

我想我明白你想要实现的目标,但我认为你的问题是代码没有达到你期望的效果。线路:

# Add the new PTR record with whatif and confirm changes
$command = Add-DnsServerResourceRecordPtr -ComputerName $computer -ZoneName $zoneName -AllowUpdateAny -TimeToLive $ttl -AgeRecord -Name $Name -PtrDomainName $NewHostName

不会将该命令分配给

$command
,因此它可以稍后运行,它会立即运行它并将该命令的任何输出分配给变量。

最简单的选择是删除大部分代码,并将其替换为这个简化版本:

Add-DnsServerResourceRecordPtr -ComputerName $computer -ZoneName $zoneName -AllowUpdateAny -TimeToLive $ttl -AgeRecord -Name $Name -PtrDomainName $NewHostName -WhatIf
$confirmation = Read-Host -Prompt "Do you want to apply these changes? (y/n)"
if ($confirmation -eq "y") {
    # Execute the command without -WhatIf
    Add-DnsServerResourceRecordPtr -ComputerName $computer -ZoneName $zoneName -AllowUpdateAny -TimeToLive $ttl -AgeRecord -Name $Name -PtrDomainName $NewHostName
    Write-Host "Changes applied."
} else {
    Write-Host "Changes not applied."
}

显然,您最终将

Add-DnsServerResourceRecordPtr
行写了两次,但由于您无论如何都以编程方式生成所有参数,所以我不认为这是世界末日。

© www.soinside.com 2019 - 2024. All rights reserved.