Powershell脚本似乎使用通配符执行命令
我正在编写一个PowerShell脚本,它将在远程服务器上执行命令。我在脚本中使用一个变量作为该命令的参数Powershell脚本似乎使用通配符执行命令,powershell,vmware,Powershell,Vmware,我正在编写一个PowerShell脚本,它将在远程服务器上执行命令。我在脚本中使用一个变量作为该命令的参数 param( [Parameter(Mandatory=$true)][string]$strUser ) $strDomain = "company.com\\" $strFQusername = $strDomain + $strUser $strFQusername $strFQusername $strFQusername Invoke-Command -comput
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变量中使用单反斜杠和双反斜杠,但得到了相同的结果。您在Invoke命令行前后看到的$strFQusername的3行用于调试。它们精确地打印出我希望看到的存储在$strFQusername中的字符串
我遗漏了什么?如果你有ps3+,请使用$using修饰符;如果你有ps2+,请使用paramétrer-argumentlist修饰符
Ps3+:
Ps2
scriptblock不知道它之外的变量,因此需要通过-ArgumentList参数将它们传递到scriptblock:
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
...{Param ($user)....Get-RemoteSession -username $user} -arg $strFQusername
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