2014-09-30 59 views
0

我正在写一个PowerShell脚本来执行远程服务器上的命令。我在脚本中使用单个变量作为该命令的参数。Powershell脚本似乎用通配符执行命令

param(
    [Parameter(Mandatory=$true)][string]$strUser 
) 

$strDomain = "company.com\\" 
$strFQusername = $strDomain + $strUser 

$strFQusername 
$strFQusername 
$strFQusername 

Invoke-Command -computername VMwareView.company.com -scriptblock {. $env:userprofile\document\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 ; Get-RemoteSession -username $strFQusername } -credential company.com\administrator 

$strFQusername 
$strFQusername 
$strFQusername 

当我执行这个脚本中,$ strFQusername变量似乎是由一个通配符来代替。 Get-RemoteSession cmdlet已正确执行,但它列出了所有用户的会话,而不仅仅是$ strFQusername变量指定的用户。这将等于我输入Get-RemoteSession -username *

如果我在脚本中硬编码用户名,我会得到预期的输出。也就是说,我只看到会话中只有用户名硬编码到脚本中。

我试过使用单反斜杠和双反斜杠$ strDomain变量,但得到了相同的结果。您在调用命令行前后看到的3行$ strFQusername用于调试。他们打印出我想要看到的字符串,存储在$ strFQusername

我错过了什么?

回答

0

,如果你有PS3 +或paramétrer-argumentlist如果PS2

PS3的+使用修改$using

..Get-RemoteSession -username $using:strFQusername 

Ps2的

...{Param ($user)....Get-RemoteSession -username $user} -arg $strFQusername 
0

的脚本块不知道它外面的变量,所以你需要通过-ArgumentList参数将它们传递到脚本块:

Invoke-Command -computername VMwareView.company.com -scriptblock { 
    . $env:userprofile\document\WindowsPowerShell\Microsoft.PowerShell_profile.ps1; 
    Get-RemoteSession -username $args[0] 
} -ArgumentList $strFQusername -credential company.com\administrator