连接变量以传递给PowerShell中的exe

时间:2017-09-21 17:31:36

标签: powershell

我正在编写一个PowerShell脚本,它使用输入参数调用exe。这是我的代码:

param([string]$sourceIP, [string]$destinationIP, [string]$prefix)
$ErrorActionPreference = "Stop"
try
{
    Write-Host "Starting Copy of Keyspaces to Dev"
    Write-Host "Copying One"
    .\CopyDatabase.exe -s $sourceIP -o $($prefix)_one -d $destinationIP -n $($prefix)_one -c
    Start-Sleep -s 2
    Write-Host "Copying Two"
    .\CopyDatabase.exe -s $sourceIP -o $($prefix)_two -d $destinationIP -n $($prefix)_two -c
    Start-Sleep -s 2
    Write-Host "Copying Three"
    .\CopyDatabase.exe -s $sourceIP -o $($prefix)_three -d $destinationIP -n $($prefix)_three -c
    Start-Sleep -s 2
    Write-Host "Copying Four"
    .\CopyDatabase.exe -s $sourceIP -o $($prefix)_four -d $destinationIP -n $($prefix)_four -c
    Write-Host "Finished Backup"
}
catch
{
    write-host "EXCEPTION:"
    write-host "Exception Type: $($_.Exception.GetType().FullName)"
    write-host "Exception Message: $($_.Exception.Message)"
    exit -1
}

它按照我的预期工作,但$($prefix)_keyspace给了我prefix _keyspace我想要prefix_keyspace的地方。我确信我可以为每个连接编写一个函数或创建一个新变量,但我想知道是否有我可以使用的命令,这样可以避免我不得不这么做那。谢谢!

1 个答案:

答案 0 :(得分:0)

将try块替换为:

try
{
    Write-Host "Starting Copy of Keyspaces to Dev"
    Write-Host "Copying One"
    .\CopyDatabase.exe -s $sourceIP -o ${prefix}_one -d $destinationIP -n ${prefix}_one -c
    Start-Sleep -s 2
    Write-Host "Copying Two"
    .\CopyDatabase.exe -s $sourceIP -o ${prefix}_two -d $destinationIP -n ${prefix}_two -c
    Start-Sleep -s 2
    Write-Host "Copying Three"
    .\CopyDatabase.exe -s $sourceIP -o ${prefix}_three -d $destinationIP -n ${prefix}_three -c
    Start-Sleep -s 2
    Write-Host "Copying Four"
    .\CopyDatabase.exe -s $sourceIP -o ${prefix}_four -d $destinationIP -n ${prefix}_four -c
    Write-Host "Finished Backup"
}

这应该做你的工作。未经测试。