在Pssession中从本地计算机传递到远程的变量值

时间:2020-12-16 12:15:57

I have a powershell script. Executing this will create a session with remote computer and execute some scriptblock inside remote computer. After that execution I need to send a mail.

我有一个powershell脚本。执行此操作将创建与远程计算机的会话并在远程计算机内执行一些脚本块。执行完毕后,我需要发送邮件。

So, I get the arguments required (like from, to, subject, body, smtp server, credentials) etc locally as shown below:

所以,我在本地得到所需的参数(如from,to,subject,body,smtp server,credentials)等,如下所示:

 $param = @{
SmtpServer = 'SMTPServer'
Port = 587
UseSsl = $true
Credential  = $crede
From = 'server@domain.in'
To = 'userv@domain.in'
Subject = 'Hi'
Body = "Hello"
}

$crede has value (username explicitly given, password reading from a text file).

$ crede具有值(明确给出用户名,从文本文件读取密码)。

And I call that param as shown below:

我称之为param,如下所示:

Send-MailMessage  $using:param

This is inside an Invoke-Command.

But when I run this program it asks me for the mail message details like from, to, smtp server etc.. Please note that these values are given on $param locally. I guess $param values are not being passed to the remote session.

但是当我运行这个程序时,它会询问我的邮件消息详细信息,如from,to,smtp server等。请注意,这些值是在$ param本地给出的。我猜$ param值没有传递给远程会话。

Can someone please support me. Any help would be really appreciated.

有人可以支持我吗任何帮助将非常感激。

2 个解决方案

#1


I just had a similar issue.

我刚才有类似的问题。

$processName = myProcess.exe
$session = New-PSSession -ComputerName $anycomputer -Credential $credentials

# powershell syntax requires -Scriptblock and { on this line
Invoke-Command -Session $session -ScriptBlock {
  param([string] $processName)
  Get-Process -Name $processName 
} -Args $processName

Remove-PSSession $session

#2


$processName = myProcess.exe $session = New-PSSession -ComputerName $anycomputer -Credential $credentials

$ processName = myProcess.exe $ session = New-PSSession -ComputerName $ anycomputer -Credential $ credentials

Invoke-Command -Session $session {Get-Process -Name $using:processName}

Invoke-Command -Session $ session {Get-Process -Name $ using:processName}

#1


I just had a similar issue.

我刚才有类似的问题。

$processName = myProcess.exe
$session = New-PSSession -ComputerName $anycomputer -Credential $credentials

# powershell syntax requires -Scriptblock and { on this line
Invoke-Command -Session $session -ScriptBlock {
  param([string] $processName)
  Get-Process -Name $processName 
} -Args $processName

Remove-PSSession $session

#2


$processName = myProcess.exe $session = New-PSSession -ComputerName $anycomputer -Credential $credentials

$ processName = myProcess.exe $ session = New-PSSession -ComputerName $ anycomputer -Credential $ credentials

Invoke-Command -Session $session {Get-Process -Name $using:processName}

Invoke-Command -Session $ session {Get-Process -Name $ using:processName}