将局部变量传递给调用命令

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

我正在从远程服务器执行以下 Powershell 脚本。但它失败如下:

PS C:\Windows\system32> Invoke-Command -Session $evServerSession -scriptblock {

\>> $ListArchiveNames = 获取内容“E:\Utilities\MyScripts\EXOmigration\ArchivePermissions\ArchivesList.txt”

\>> Foreach ($ListArchiveNames 中的 $ArchiveName) {Get-EVArchive -ArchiveName $ArchiveName |获取 EArchivePermission | Export-Csv -Path E:\Utilities\MyScripts\EXOmigration\ArchivePermissions\$NewTrusteesList.csv -Append -NoTypeInformation -Force}

\>>

\>> }

cls
#==============
$NewTrusteesList = Read-Host "Please enter the migration Transvault group name here " 
$NewTrusteesList=$NewTrusteesList+"-ArchiveTrustees"+$dateStr 
New-Item  E:\Utilities\MyScripts\EXOmigration\ArchivePermissions\$NewTrusteesList.csv -ItemType File
#------------
#--------------------Establish an EV PS session-----------------------------
$evServerSession = New-PSSession -computername cov-msv-prd009.morganlewis.net -ConfigurationName Microsoft.Powershell32
Invoke-Command -Session $evServerSession -scriptblock {Add-PSSnapin Symantec.EnterpriseVault.PowerShell.Snapin}
#--------------------Run pertinent PS scripts-------------------------------
Invoke-Command -Session $evServerSession -scriptblock {
$ListArchiveNames = Get-Content "E:\Utilities\MyScripts\EXOmigration\ArchivePermissions\ArchivesList.txt"
Foreach ($ArchiveName in $ListArchiveNames) {Get-EVArchive -ArchiveName $ArchiveName | Get-EVArchivePermission | Export-Csv -Path E:\Utilities\MyScripts\EXOmigration\ArchivePermissions\$NewTrusteesList.csv -Append -NoTypeInformation -Force}

}

#------------------------Clean up the session----------------------------
$evServerSession|Remove-PSSession
powershell invoke-command
1个回答
0
投票

您必须使用

$Using:variable_name
来包含
variable_name
在远程计算机上运行的命令中包含本地变量

示例:

$Log = 'PowerShellCore/Operational'
Invoke-Command -ComputerName Server01 -ScriptBlock {
    Get-WinEvent -LogName $Using:Log -MaxEvents 10
}
© www.soinside.com 2019 - 2024. All rights reserved.