无法将命令正确传递给Powershell中的远程计算机

时间:2015-04-15 01:18:40

标签: powershell citrix xenapp

我有一个Powershell脚本,当复制到本地远程服务器并使用PSExec远程执行时,它会正确运行并完成我需要它做的事情。我试图调整相同的脚本,以便能够远程运行而不依赖于使用PSExec远程复制和执行的脚本。这是让Citrix XenApp服务器加入其Farm的命令。

# Establish Variables
$ZoneName = "MyZone"
$OdbcUserName = "MyCitrixUserAcct"
$OdbcPassword = "MyPassword"
$LicenseServerName = "mylicenseserver.company.com"
$XAConfigConsoleExe = "C:\Progra~2\Citrix\XenApp\ServerConfig\XenAppConfigConsole.exe"
$Servers = Get-Content -path .\serverlist.txt
ForEach ($Server in $Servers)
{
# Creating mf20.dsn File
if ((Test-Path \\$Server\c$\temp\mf20.dsn) -eq $true)
{
remove-item \\$Server\c$\temp\mf20.dsn
}
add-Content \\$Server\c$\temp\mf20.dsn "[ODBC]"
add-content \\$Server\c$\temp\mf20.dsn "DRIVER=SQL Server"
add-content \\$Server\c$\temp\mf20.dsn "DATABASE=MyDB"
add-content \\$Server\c$\temp\mf20.dsn "APP=Citrix IMA"
add-content \\$Server\c$\temp\mf20.dsn "UID="
add-content \\$Server\c$\temp\mf20.dsn "SERVER=dbserver.company.com,1433" 
add-content \\$Server\c$\temp\mf20.dsn "Trusted_Connection=No"

# Setting setup arguments

[Array]$XAArguments = @()

$XAArguments += "/ExecutionMode:Join"
$XAArguments += "/ZoneName:$ZoneName"
$XAArguments += "/ImaWorkerMode:True"
$XAArguments += "/DsnFile:c:\temp\mf20.dsn"
$XAArguments += "/AuthenticationType:Sql"
$XAArguments += "/OdbcUserName:$OdbcUserName"
$XAArguments += "/OdbcPassword:$OdbcPassword"
$XAArguments += "/LicenseServerName:$LicenseServerName"
$XAArguments += "/LicenseServerPort:27000"
$XAArguments += "/LicenseModel:XA"
$XAArguments += "/CustomXMLServicePort:8080"

# running setup with arguments

Invoke-Command -ComputerName $Server {$XAConfigConsoleExe + $XAArguments}
}

我尝试了大量不同的排列和组合(即使用-scriptblock,使用pssessions)我通过搜索找到的东西。我知道我的命令行很好,因为如果我获取write-host $XAConfigConsoleExe $XAArguments的输出并将其传递给服务器的Powershell控制台,那就可以了。

我确定我错过了一些明显的东西。我没有写原始剧本。我无法为辩论的构建方式辩护,所以如果有更好的(更可靠的方式),那么我在这里所做的就是对任何想法持开放态度。

以下是我可以使用PSExec远程执行的原始脚本:

# Establish Variables
$ZoneName = "MyZone"
$OdbcUserName = "MyCitrixUserAcct"
$OdbcPassword = "MyPassword"
$LicenseServerName = "mylicenseserver.company.com"
$XAConfigConsoleExe = "C:\Program Files (x86)\Citrix\XenApp\ServerConfig\XenAppConfigConsole.exe"

# Creating mf20.dsn File
if ((Test-Path C:\temp\mf20.dsn) -eq $true)
{
remove-item C:\temp\mf20.dsn
}
add-Content C:\temp\mf20.dsn "[ODBC]"
add-content C:\temp\mf20.dsn "DRIVER=SQL Server"
add-content C:\temp\mf20.dsn "DATABASE=MyDB"
add-content C:\temp\mf20.dsn "APP=Citrix IMA"
add-content C:\temp\mf20.dsn "UID="
add-content C:\temp\mf20.dsn "SERVER=mydbserver.company.com,1433" 
add-content C:\temp\mf20.dsn "Trusted_Connection=No"

# Setting setup arguments
[Array]$XAArguments = @()

$XAArguments += "/ExecutionMode:Join"
$XAArguments += "/ZoneName:$ZoneName"
$XAArguments += "/ImaWorkerMode:True"
$XAArguments += "/DsnFile:C:\Temp\mf20.dsn"
$XAArguments += "/AuthenticationType:Sql"
$XAArguments += "/OdbcUserName:$OdbcUserName"
$XAArguments += "/OdbcPassword:$OdbcPassword"
$XAArguments += "/LicenseServerName:$LicenseServerName"
$XAArguments += "/LicenseServerPort:27000"
$XAArguments += "/LicenseModel:XA"
$XAArguments += "/CustomXMLServicePort:8080"

# running setup with arguments

& $XAConfigConsoleExe $XAArguments

1 个答案:

答案 0 :(得分:0)

  

我已编辑此答案以反映脚本I的最终版本   跟着去了。我没有改变任何逻辑或方法。我只是清理干净了   一些东西,并添加了更多的变量,使脚本更灵活   用于将来的编辑。

它不是最漂亮的解决方案,但它是我能找到的唯一可靠的解决方案,完全自包含(在服务器列表的输入.txt文件之外)。没有任何东西被复制到任何地方,一切都是动态创建的。任何有冒险精神的人都可以自由地查看并提出更好/更清洁的方法来实现我的目标。

# Establish Variables
$ZoneName = Read-Host 'Zone Name?'
$OdbcUserName = "Citrixuser"
$OdbcPassword = Read-Host 'Password for Citrixuser?'
$LicenseServerName = "MyLicenseServer.company.com"
$DataBase= "DBName"
$DBServer= "MyDBServer.company.com"
$EdgeSightServer = "MyEdgeSightServer.company.com"

$XAConfigConsoleExe = "C:\Program Files (x86)\Citrix\XenApp\ServerConfig\XenAppConfigConsole.exe"
$Servers = Get-Content -path .\serverlist.txt

# Setting setup arguments

[Array]$XAArguments = @()

$XAArguments += "/ExecutionMode:Join"
$XAArguments += "/ZoneName:$ZoneName"
$XAArguments += "/ImaWorkerMode:True"
$XAArguments += "/DsnFile:C:\temp\mf20.dsn"
$XAArguments += "/AuthenticationType:Sql"
$XAArguments += "/OdbcUserName:$OdbcUserName"
$XAArguments += "/OdbcPassword:$OdbcPassword"
$XAArguments += "/LicenseServerName:$LicenseServerName"
$XAArguments += "/LicenseServerPort:27000"
$XAArguments += "/LicenseModel:XA"
$XAArguments += "/CustomXMLServicePort:8080"
$XAArguments += "/EdgeSightCompanyName:CompanyName"
$XAArguments += "/EdgeSightServerName:$EdgeSightServer"
$XAArguments += "/EdgeSightServerPort:80"

# Running against server list

ForEach ($Server in $Servers)
{
# Creating joinfarm.bat file on the remote server
if ((Test-Path \\$Server\c$\temp\joinfarm.bat) -eq $true) {Remove-Item \\$Server\c$\temp\joinfarm.bat}
Add-Content \\$Server\c$\temp\joinfarm.bat "$([char]34)$XAConfigConsoleExe$([char]34) $XAArguments"
Add-Content \\$Server\c$\temp\joinfarm.bat "shutdown.exe /r /f /t 10"
Add-Content \\$Server\c$\temp\joinfarm.bat "del c:\temp\mf20.dsn /q"
Add-Content \\$Server\c$\temp\joinfarm.bat "del c:\temp\joinfarm.bat /q"

# Creating mf20.dsn file on the remote server
if ((Test-Path \\$Server\c$\temp\mf20.dsn) -eq $true) {remove-item \\$Server\c$\temp\mf20.dsn}
add-Content \\$Server\c$\temp\mf20.dsn "[ODBC]"
add-content \\$Server\c$\temp\mf20.dsn "DRIVER=SQL Server"
add-content \\$Server\c$\temp\mf20.dsn "DATABASE=$DataBase"
add-content \\$Server\c$\temp\mf20.dsn "APP=Citrix IMA"
add-content \\$Server\c$\temp\mf20.dsn "UID="
add-content \\$Server\c$\temp\mf20.dsn "SERVER=$DBServer,1433" 
add-content \\$Server\c$\temp\mf20.dsn "Trusted_Connection=No"

# running setup with arguments
Invoke-Command -ComputerName $Server -ScriptBlock {c:\temp\joinfarm.bat} -AsJob
}

我正在做的是编写一个批处理文件(我尝试编写PS1文件,但是当我运行PS1 -AsJob时,它在初始命令之后没有运行任何命令,即使它似乎如果我没有使用-AsJob)到远程服务器并使用-AsJob开关执行批处理,那么我可以同时加入多个服务器。没有-AsJob每个服务器必须在下一个处理之前完成。批处理清除自身,dsn文件,然后重新启动服务器。